MPMovieplayercontroller

播放視頻文件通常使用 MPMoviePlayerViewController 和 MPMoviePlayerController。這二者的區別就是MPMoviePlayerViewController裏面包含了一個MPMoviePlayerController
先說MPMoviePlayerController
首先要包含 #import <MediaPlayer/MediaPlayer.h>頭文件和MediaPlayer.framework。

- (void)createMPPlayerController:(NSString *)sFileNamePath {
  NSURL *movieURL = [NSURL fileURLWithPath:sFileNamePath];
 MPMoviePlayerController *movewController =[[MPMoviePlayerController alloc] initWithContentURL:movieURL];
 [movewController prepareToPlay];
 [self.view addSubview:movewController.view];//設置寫在添加以後   // 這裏是addSubView
 movewController.shouldAutoplay=YES;
 [movewController setControlStyle:MPMovieControlStyleDefault];
 [movewController setFullscreen:YES];
 [movewController.view setFrame:self.view.bounds];
 這裏註冊相關操做的通知

 [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(movieFinishedCallback:)
                                                 name:MPMoviePlayerPlaybackDidFinishNotification
                                               object:moveViewController.movewController]; //播放完後的通知

 [movewController release];
}

-(void)movieFinishedCallback:(NSNotification*)notify {
    MPMoviePlayerController* theMovie = [notifyobject];
    [[NSNotificationCenterdefaultCenter] removeObserver:self
                                                    name:MPMoviePlayerPlaybackDidFinishNotification
                                                  object:theMovie];
    [theMovie.view removeFromSuperview];
    [theMovie release];
}
//////////////////////////////// 
2.介紹下MPMoviePlayerViewController。 
注意:MPMoviePlayerViewController 必須 presentMoviePlayerViewControllerAnimated方式添加,不然Done按鈕是不會響應通知MPMoviePlayerPlaybackDidFinishNotification事件的;

- (void)createMPPlayerController:(NSString *)sFileNamePath {
    MPMoviePlayerViewController *moviePlayer =[[MPMoviePlayerViewControlleralloc] initWithContentURL:[NSURLfileURLWithPath:sFileNamePath]];
    [moviePlayer.moviePlayerprepareToPlay];
    [selfpresentMoviePlayerViewControllerAnimated:moviePlayer]; // 這裏是presentMoviePlayerViewControllerAnimated
    [moviePlayer.moviePlayersetControlStyle:MPMovieControlStyleFullscreen];
    [moviePlayer.view setBackgroundColor:[UIColor clearColor]];
    [moviePlayer.view setFrame:self.view.bounds];
    [[NSNotificationCenterdefaultCenter] addObserver:self
                                             selector:@selector(movieFinishedCallback:)
                                                 name:MPMoviePlayerPlaybackDidFinishNotification
                                               object:moviePlayer.moviePlayer];
    [moviePlayer release];
}

-(void)movieStateChangeCallback:(NSNotification*)notify  {
   //點擊中的播放/ 暫停按鈕響應的通知
}
-(void)movieFinishedCallback:(NSNotification*)notify{
   // 視頻播放完或者在presentMoviePlayerViewControllerAnimated下的Done按鈕被點擊響應的通知。
    MPMoviePlayerController* theMovie = [notifyobject];
    [[NSNotificationCenterdefaultCenter] removeObserver:self
                                                    name:MPMoviePlayerPlaybackDidFinishNotification
                                                  object:theMovie];
    [selfdismissMoviePlayerViewControllerAnimated];
}
相關文章
相關標籤/搜索