2013年4月10日 星期三

[iOS]使用MPMoviePlayerViewController來播放mp4,在iOS 6上無法順利播放問題解決方法。

在ios5的版本中,使用以下函式就可以順利播放mp4,但是這個函式在ios6卻會crash掉,最後終於找到在ios5與ios6可以使用的方法,詳情請參考該網址

原本在ios5上的寫法如下:(ios6將無法使用)


- (void)openMP4:(NSString *)s_url
{
    NSURL *fileURL = [NSURL URLWithString:s_url];
    [player.moviePlayer setControlStyle:MPMovieControlStyleFullscreen];
    [player.moviePlayer setFullscreen:NO];
    player = [[MPMoviePlayerViewController alloc] initWithContentURL:fileURL];
    //弹出播放器窗口
    [self  presentMoviePlayerViewControllerAnimated:player ];
    [player.moviePlayer setMovieSourceType:MPMovieSourceTypeFile];
    [player.moviePlayer prepareToPlay];
    [self.view addSubview:player.view];
    //播放
    [player.moviePlayer play];
    //增加一個觀察者
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(moviePlayBackDidFinish:)
                                                 name:MPMoviePlayerPlaybackDidFinishNotification
                                               object:nil];
}
- (void) moviePlayBackDidFinish:(NSNotification*) aNotification {
    //移除觀察者
    [[NSNotificationCenter     defaultCenter] removeObserver:self
                                                        name:MPMoviePlayerPlaybackDidFinishNotification
                                                      object:nil];
    //播放器消失
    [player dismissMoviePlayerViewControllerAnimated];
    player=NULL;
}

相容ios5、ios6的寫法如下(來源):

- (void)openMP4:(NSString*)s_url
{
    // Initialize the movie player view controller with a video URL string
    NSURL *fileURL = [NSURL URLWithString:s_url];
    
    MPMoviePlayerViewController *playerVC = [[MPMoviePlayerViewController alloc] initWithContentURL:fileURL];
    
    // Remove the movie player view controller from the "playback did finish" notification observers
    [[NSNotificationCenter defaultCenter] removeObserver:playerVC
                                                    name:MPMoviePlayerPlaybackDidFinishNotification
                                                  object:playerVC.moviePlayer];
    
    // Register this class as an observer instead
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(movieFinishedCallback:)
                                                 name:MPMoviePlayerPlaybackDidFinishNotification
                                               object:playerVC.moviePlayer];
    
    // Set the modal transition style of your choice
    playerVC.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    
    // Present the movie player view controller
    [self presentModalViewController:playerVC animated:YES];
    
    // Start playback
    [playerVC.moviePlayer prepareToPlay];
    [playerVC.moviePlayer play];
}
- (void)movieFinishedCallback:(NSNotification*)aNotification
{
    // Obtain the reason why the movie playback finished
    NSNumber *finishReason = [[aNotification userInfo] objectForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey];
    
    // Dismiss the view controller ONLY when the reason is not "playback ended"
    if ([finishReason intValue] != MPMovieFinishReasonPlaybackEnded)
    {
        MPMoviePlayerController *moviePlayer = [aNotification object];
        
        // Remove this class from the observers
        [[NSNotificationCenter defaultCenter] removeObserver:self
                                                        name:MPMoviePlayerPlaybackDidFinishNotification
                                                      object:moviePlayer];
        
        // Dismiss the view controller
        [self dismissModalViewControllerAnimated:YES];
    }
}

沒有留言:

張貼留言