Search code examples
iosobjective-cmpmovieplayercontroller

MPMoviePlayerController notification not working in ios6


I have build one demo in which i simply play video from live url and if url not work properly then MPMoviePlayerController fire the notification MPMoviePlayerPlaybackDidFinishNotification with MPMoviePlayerPlaybackDidFinishReasonUserInfoKey.This thing work perfectly in ios7 and greater but when same thing i use in ios6, it not working. My Code snipet is following

NSLog(@"URL::%@",url.absoluteString);
        UIInterfaceOrientation interface = [UIApplication sharedApplication].statusBarOrientation;
        videoPlayer = [[MPMoviePlayerController alloc] init];
        [videoPlayer setContentURL:url];
        [videoPlayer setMovieSourceType:MPMovieSourceTypeStreaming];
        [videoPlayer setControlStyle:MPMovieControlStyleNone];
        [videoPlayer setScalingMode:MPMovieScalingModeNone];
        [videoPlayer.view setFrame:CGRectMake(0.0, viewTopbar.frame.size.height, self.view.frame.size.width, self.view.frame.size.height - (viewTopbar.frame.size.height+[self getBannerHeight:interface]))];
        [self.view addSubview:self.videoPlayer.view];
        [self.videoPlayer play];
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(moviePlayBackDidFinish:)
                                                     name:MPMoviePlayerPlaybackDidFinishNotification
                                                   object:videoPlayer];
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(moviePlayBackDidFinishWithReson:)
                                                     name:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey
                                                   object:videoPlayer];

#pragma mark
#pragma mark - movie player delegate methods
- (void) moviePlayBackDidFinish:(NSNotification*)notification
{
    int reason = [[[notification userInfo] valueForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey] intValue];
    if (reason == MPMovieFinishReasonPlaybackError) {
        //error
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:localize(@"strTitle") message:localize(@"strMsg") delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alertView show];
    }
}
-(void) moviePlayBackDidFinishWithReson:(NSNotification *)notification
{
    int reason = [[[notification userInfo] valueForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey] intValue];
    if (reason == MPMovieFinishReasonPlaybackError) {
        //error
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:localize(@"strTitle") message:localize(@"strMsg") delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alertView show];
    }
}

Solution

  • After R&D on lot of thing i am not find any solution from delegate methods so for this issue i implement one thing i start one timer and check the current head of the movie player if it is grater then 0. it means video is load and play successfully.

    My code snippet is following.

    write this code in initialisation of the MPMoviePlayerController

    if (iOS6 && APPDELEGATE.isNetAvailable)
            {
                [NSTimer scheduledTimerWithTimeInterval:10.0 target:self selector:@selector(checkVideo) userInfo:nil repeats:NO];
            }
    

    and write this method in your view controller

    -(void)checkVideo
    {
        NSLog(@"%f",videoPlayer.currentPlaybackTime);
        if (videoPlayer.currentPlaybackTime == 0.0)
        {
            //error
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:localize(@"strTitle") message:localize(@"strMsg") delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [alertView show];
            [videoPlayer stop];
        }
    }
    

    Please used this solution for ios6 only.