MPMoviePlayerPlaybackDidFinishNotification is called again in iPhone 4.3 simulator when setting contentURL

NOTE. See updates below.


I have an application for playing video in one of the list. So, to test this functionality, I created a simple application with one controller. I referenced this blog before using this view controller. The view controller has a name TNViewControllerand its implementation is as follows:

#import <UIKit/UIKit.h>
#import <MediaPlayer/MediaPlayer.h>

@interface TNViewController : UIViewController {
  @private
    NSMutableArray *_videoArray;
    int _currentVideo;

    MPMoviePlayerController *_moviePlayer;
    NSURL *_movieUrl;
}

@end

Its implementation:

#import "TNViewController.h"

@implementation TNViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    [[UIApplication sharedApplication] setStatusBarHidden:YES animated:NO];
    [self.view setFrame:CGRectMake(0, 0, 480, 320)];

    [self initVideos];
    [self initPlayer];
}

- (void) initVideos {
    _videoArray = [[NSMutableArray alloc] init];

    NSString *path = [[NSBundle mainBundle] pathForResource:@"sintel_trailer" ofType:@"mp4" inDirectory:nil];
    [_videoArray addObject:path];
    path = [[NSBundle mainBundle] pathForResource:@"elephants_dream_trailer" ofType:@"mp4" inDirectory:nil];
    [_videoArray addObject:path];
    path = [[NSBundle mainBundle] pathForResource:@"big_buck_bunny_trailer" ofType:@"mp4" inDirectory:nil];
    [_videoArray addObject:path];

    _currentVideo = -1;
}

- (NSString*) nextVideo {
    _currentVideo++;
    if (_currentVideo >= _videoArray.count) {
        _currentVideo = 0;
    }
    return [_videoArray objectAtIndex:_currentVideo];
}

- (void) initPlayer {
    _moviePlayer = [[MPMoviePlayerController alloc]init];

    [self readyPlayer];
    [self.view addSubview:_moviePlayer.view];

    // Register to receive a notification when the movie has finished playing. 
    [[NSNotificationCenter defaultCenter] addObserver:self 
                                             selector:@selector(moviePlayBackDidFinish:) 
                                                 name:MPMoviePlayerPlaybackDidFinishNotification 
                                               object:_moviePlayer];
}

- (void) readyPlayer {
    _movieUrl    = [NSURL fileURLWithPath:[self nextVideo]];
    [_movieUrl retain];

    _moviePlayer.contentURL = _movieUrl;

    // For 3.2 devices and above
    if ([_moviePlayer respondsToSelector:@selector(loadState)]) {
        // Set movie player layout
        [_moviePlayer setControlStyle:MPMovieControlStyleNone];
        [_moviePlayer setFullscreen:YES];

        // May help to reduce latency
        [_moviePlayer prepareToPlay];

        // Register that the load state changed (movie is ready)
        [[NSNotificationCenter defaultCenter] addObserver:self 
                                                 selector:@selector(moviePlayerLoadStateChanged:) 
                                                     name:MPMoviePlayerLoadStateDidChangeNotification 
                                                   object:nil];
    } else {
        // Register to receive a notification when the movie is in memory and ready to play.
        [[NSNotificationCenter defaultCenter] addObserver:self 
                                                 selector:@selector(moviePreloadDidFinish:) 
                                                     name:MPMoviePlayerContentPreloadDidFinishNotification 
                                                   object:nil];
    }
}

/*---------------------------------------------------------------------------
 * For 3.1.x devices
 *--------------------------------------------------------------------------*/
- (void) moviePreloadDidFinish:(NSNotification*)notification {
    // Remove observer
    [[NSNotificationCenter  defaultCenter]  removeObserver:self 
                                                      name:MPMoviePlayerContentPreloadDidFinishNotification 
                                                    object:nil];

    // Play the movie
    [_moviePlayer play];
}

/*---------------------------------------------------------------------------
 * For 3.2 and 4.x devices
 *--------------------------------------------------------------------------*/
- (void) moviePlayerLoadStateChanged:(NSNotification*)notification {
    NSLog(@"moviePlayerLoadStateChanged");
    // Unless state is unknown, start playback
    if ([_moviePlayer loadState] != MPMovieLoadStateUnknown) {
        // Remove observer
        [[NSNotificationCenter defaultCenter]  removeObserver:self
                                                         name:MPMoviePlayerLoadStateDidChangeNotification 
                                                       object:nil];

        // Set frame of movie player
        [[_moviePlayer view] setFrame:CGRectMake(0, 0, 480, 320)];
        // Play the movie
        [_moviePlayer play];
    }
}

- (void) moviePlayBackDidFinish:(NSNotification*)notification {    
    NSLog(@"playback finished...");
    NSLog(@"End Playback Time: %f", _moviePlayer.endPlaybackTime);
    int reason = [[[notification userInfo] valueForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey] intValue];
    if (reason == MPMovieFinishReasonPlaybackEnded) {
        NSLog(@"Reason: movie finished playing");
    }else if (reason == MPMovieFinishReasonUserExited) {
        NSLog(@"Reason: user hit done button");
    }else if (reason == MPMovieFinishReasonPlaybackError) {
        NSLog(@"Reason: error");
    }

    [self playNextVideo];
}

- (void) playNextVideo {
    NSString *filePath = [self nextVideo];

    [_movieUrl release];
    _movieUrl = [NSURL fileURLWithPath:filePath];    
    [_movieUrl retain];

    _moviePlayer.contentURL = _movieUrl;
    [_moviePlayer play];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}

- (void) dealloc {
    [_moviePlayer release];
    [_movieUrl release];
    [_videoArray release];

    [super dealloc];
}

@end

Now my problem is that the notification MPMoviePlayerPlaybackDidFinishNotificationis called twice. As you can see from the above code, I registered it only once in viewDidLoad(in initPlayer, called from viewDidLoad). Here is the log output:

2012-07-02 12:29:17.661 DemoApp[1191:ef03] moviePlayerLoadStateChanged
2012-07-02 12:30:11.470 DemoApp[1191:ef03] playback finished...
2012-07-02 12:30:11.471 DemoApp[1191:ef03] End Playback Time: -1.000000
2012-07-02 12:30:11.472 DemoApp[1191:ef03] Reason: movie finished playing
2012-07-02 12:30:11.474 DemoApp[1191:ef03] playback finished...
2012-07-02 12:30:11.475 DemoApp[1191:ef03] End Playback Time: -1.000000
2012-07-02 12:30:11.476 DemoApp[1191:ef03] Reason: movie finished playing

2012-07-02 12:31:03.821 DemoApp[1191:ef03] playback finished...
2012-07-02 12:31:03.822 DemoApp[1191:ef03] End Playback Time: -1.000000
2012-07-02 12:31:03.824 DemoApp[1191:ef03] Reason: movie finished playing
2012-07-02 12:31:03.826 DemoApp[1191:ef03] playback finished...
2012-07-02 12:31:03.827 DemoApp[1191:ef03] End Playback Time: -1.000000
2012-07-02 12:31:03.827 DemoApp[1191:ef03] Reason: movie finished playing

, . , . (, , , nextVideo , , nil.). sintel_trailer.mp4. elephants_dream_trailer.mp4 big_buck_bunny_trailer.mp4. , , . , MPMoviePlayerPlaybackDidFinishNotification ? , . ?

1:

moviePlayBackDidFinish:, , :

if (!_playNextVideo) {
    _playNextVideo = YES;
    return;
}
_playNextVideo = NO;
// code to play video....

, , . , .

2:

iPhone 4.3. , iPhone 5.0 iPhone 5.1, . , . ( 1) ( 4.3, 5.0 5.1). Xcode 4.3.2, MacOSX Lion - 10.7.4. , ? 4.3?

3:

, . playNextVideo. _moviePlayer.contentURL = _movieUrl;. MPMoviePlayerPlaybackDidFinishNotification. iPhone 4.3. ?

4:

, . , , UPDATE 1, moviePlayBackDidFinish:

NSTimeInterval currentCallback = [NSDate timeIntervalSinceReferenceDate];
NSTimeInterval difference      = currentCallback - _lastCallback;
_lastCallback                  = currentCallback;
if (difference < 5.0) {
    return;
}
// code to play video....
+5
2

. :

- (void) skipVideo {
    [_moviePlayer stop];
}

skipVideo MPMovieFinishReasonPlaybackEnded ( ). contentUrl MPMovieFinishReasonPlaybackEnded , moviePlayBackDidFinish ,

( playNextVideo)

[_moviePlayer prepareToPlay];

!

+4

:

MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL: _movieUrl];
if (player)
{
    [self setMoviePlayer:player];
}
[self.moviePlayer play];

self.moviePlayer.contentURL = _movieUrl;

MPMoviePlayerPlaybackDidFinishNotification .

+1

All Articles