views:

39

answers:

2

Hi guys, I have a UITableView populated with a bunch of music players that I've made using a custom UITableViewCell. (as shown in the screenshot linked here: screenshot).

The problem with the way I have it coded is that since each cell is completely independent, there's no way for one tableviewcell to check if any other the other cells have audio playing. This means that when I run my app, I am able to play more than one audio file at the same time.

Ultimately, how would I go about putting some sort of check in place so that when the user hits the play button, the app first checks to see if any other cell is playing audio, and, if so, stops them before playing its own audio file?

Please let me know if you would like me to post my project.

Thank you!

A: 

It would probably be easiest to use notifications (NSNotification and NSNotificationCenter).

Use a common notification name for starting and stopping.

MusicPlayerWillPlay MusicPlayerWillStop

When you create each player, register for both notifications. Then when playerA is going to play, it posts a MusicPlayerWillPlay notification. playerB, playerC and playerD, will get this notification, and if they are playing, will stop.

One caveat is that each player is unaware of the others, so you have to register with a nil object, which means playerA will get it's own notification. So in your notification method, you'll probably want to do something like


{
    //...
    if (sender == self)
        return;
    //...
}

Jerry Jones
+1  A: 

For starters I can't think of any good reason the cells themselves should be responsible for playing the audio. They should be responsible for telling some other object (like whatever controller is responsible for the UITableView) to play the audio. That controller would obviously then know to stop whatever it was already playing before playing something else.

In general, it's not a great idea to be putting that much logic into a 'view.'

imaginaryboy
Even though I posted an answer, I totally agree with imaginaryboy.
Jerry Jones
I can only echo this. Do not put non-view logic in your view. Encapsulate functionality like this in service classes that are invoked by the controller. The controller is responsible for telling a specific view to represent itself as playing or not playing.
Luke Redpath
Thank you both for your solutions :) I'm a new developer and I think the biggest challenge right now is logic, and thinking about it like this makes way more sense.
Jack Lawrence