views:

39

answers:

1

Hi I have an NSArray containing many 'multimedia action' as I like to call them, objects that encapsulate some multimedia content like movies and photos. I'd like to 'play' them in sequence waiting each one finished before calling the other.

What is the best way to manage the duration of each operation before calling the next one? (for a photo imagine a UIAlertView that dismiss after some seconds delay, for a movie imagine a MPMoviePlayerController instead)

I'm already calling the method that scans the array with an NSOperation.

IMPORTANT: It appears that the MPMovieController does not play (the interface doesn't even appear) whether called inside a NSOperation. Has anyone else experienced this issue?

UPDATE Using performSelectorOnMainThread: makes the MPMoviePlayer work as expected

+1  A: 

This is the solution I've managed to find. I'm posting it in the case anyone is interested.

First I put all my 'multimedia actions' in a NSOperationQueue and i set the maxConcurrentOperationCount to 1, so I am certain that they will be run asynchronously from the main thread but only one at a time in a FIFO order (this is very important you you want that your content does not overlap on the screen).

Then for each action I activate it in a particular way depending on its class/kind. Let's suppose there are three kinds of actions:

  1. UIAlertViews subclasses
  2. MPMoviePlayerController-like
  3. Background actions (actions that do not involve multimedia content to appear instantly on the current view

For the first two I use NSNotifications to manage the queue since I do not know when the content will stop playing or will be dismissed by the user (suppose by touch). So for each of those I:

  • Pause the queue execution with setSuspended:YES
  • Add an observer for the corrisponding NSNotification
  • Play the content
  • Post a notification when they finish playing
  • Remove the observer and start the queue again with setSuspended:NO

For an UIAlertView the notification is posted with a performSelector:withObject:afterDelay: saying the amount of time it shall be displayed or after a double tap. For A MPMovieController instead it is launched automatically and is named MPMoviePlayerPlaybackDidFinishNotification. Both the UIAlertView and the MP kind shall be played on the main thread.

The third kind can be a set of instruction simply executed in the NSInvocationOperation put in the queue. So I can even create actions like 'wait' that suspend the queue eecution for some seconds

rano