tags:

views:

29

answers:

1

Hi friends , I made an app which plays the song on clicking on the image of artist.(see image attached). Each artist image is implemented on button and on clicking this button, a function is being called which first downloads and then plays the song. I passed this method(function) in a thread but problem is that every time when I click on the image of artist(button) new threads starts running and then multiple songs gets started playing concurrently. How can I use "NSOperation and NSOperationQueue" so that only one song will run at a time . Please help.

Thanks in advance

alt text

+2  A: 

NSOperation and NSOperationQueue aren't going to directly solve your problem.

If I were pursuing a dead simple approach, I would have a global AudioPlayer object that has a method startPlaying: whose argument is the song to play (represented however needed; URL, NSData, whatever you need).

In that method, I'd stop playing whatever is currently playing and start the new track.

If I remember correctly, I don't think you even need a thread for this; the audio APIs are generally quite adept at taking care of playback in the background.

In any case, if you do need a thread, then I'd hide that thread in my AudioPlayer object and let it take care of telling the music to stop/start playing in said thread. A queue of some kind -- operation or GCD -- could be used for that, yes.

bbum
@bbum: Earlier i was doing the same but the problem was when I click the button (which starts downloading of song and then plays it), my UITable becomes still and until download of song is completed, it remains in that state. so I put the downloading in a separate thread. From this point problem started because as I said earlier every time I click on the button new threads created and the all the songs started playing concurrently. That is why I used NSOperation so that downloading can occur in a queue not concurrently. Please tell me some solution having this point in your view. Thanks
Harsh