使用MPMoviePlayerController播放視頻

MPMoviePlayerController播放視頻

基本的視頻播放,應該都是用MPMoviePlayerController吧,SDK文檔裏給出的例子也是這樣的。小程序

通常的步驟:緩存

1. 準備好視頻路徑,建立MPMoviePlayerController,即便是本地視頻文件,也要生成NSURL路徑。網絡

 

[cpp] view plain copyapp

 

 

  1. NSString *file = [[NSBundle mainBundle] pathForResource:@"test_movie" ofType:@"mp4"];  
  2. NSURL *url = [NSURL fileURLWithPath:file];  
  3. if (_moviePlayer == nil) {  
  4.     _moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];  
  5. }else {  
  6.     [_moviePlayer setContentURL:url];  
  7. }  

 

 

2. MPMoviePlayerController各類設置,拉伸、控制模式、循環模式、自動播放等等,本身看着API文檔挨個兒試吧,總有一款適合你大笑!最後一步就是別忘了播放唄。測試

 

[cpp] view plain copyurl

 

 

  1. _moviePlayer.controlStyle = MPMovieControlStyleNone;    
  2. _moviePlayer.shouldAutoplay = YES;    
  3. _moviePlayer.repeatMode = MPMovieRepeatModeOne;  
  4. [_moviePlayer setFullscreen:YES animated:YES];    
  5. _moviePlayer.scalingMode = MPMovieScalingModeAspectFit;  
  6.        [_moviePlayer play];  

  

 

3. 在適當的位置開始/中止監聽播放狀態事件。spa

 

[cpp] view plain copy.net

 

 

  1. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayBackDidFinish:)    
  2.                                              name:MPMoviePlayerPlaybackDidFinishNotification    
  3.                                            object:_moviePlayer];   


 監聽視頻播放完成的事件,播放完成將執行moviePlayBackDidFinish方法,在該方法中執行本身想要的操做。調試

 

由於我以前設置了循環播放、並且控制模式也是不可控制,所以播放器上是不會有「完成」按鈕的。也就不會正常的經過完成播放來進入這個方法。視頻

相似的,還能夠監聽MPMoviePlayerPlaybackStateDidChangeNotification等不少關於播放期間狀態改變的事件,能夠根據本身的須要去選擇。

別忘了不須要時,記得removeObserver。

 

至此,簡單的播放本地視頻文件的操做就完成了,我尚未嘗試過播放網絡視頻,不過應該也是一樣的道理。這只是最基本的操做,若是深刻使用應該還會涉及緩存處理、下載處理、播放控制等不少方面的細節技術,若是之後有機會作視頻、音頻方面的應用,再好好研究一下吧。

 

改進應用

以前給別人作了一個展會播放視頻的小程序,就是用了上面的代碼,結果遇到點兒小問題。

由於是展會播放視頻,因此不容許用戶控制,就放在那兒重複重複再重複 的播放,可是測試最後忽然發現個問題,雖然播放器上沒有「完成」按鈕了,可是仍是能對手勢操做進行響應的,在播放器上「兩指捏合」,播放器就退出了,並且 調試發現退出以前也不會走到moviePlayBackDidFinish方法裏,呃!!!應該是在本身的viewcontroller裏沒有對手勢操做 行處理。這可咋辦呢?

後來在sdk裏發現了系統提供的MPMoviePlayerViewController類!!!太好了!!

代碼改爲:

1. new一個MPMoviePlayerViewController對象,將其view添加到咱們本身的viewcontroller裏面;

2. 不須要本身建立MPMoviePlayerController,使用MPMoviePlayerViewController裏面的MPMoviePlayerController執行實際的播放操做;

3. 監聽的對象改爲2中所述的MPMoviePlayerController就好了。

 

[cpp] view plain copy

 

  1.    // create MPMoviePlayerViewController  
  2.    MPMoviePlayerViewController *playerViewController = [[MPMoviePlayerViewController alloc] initWithContentURL:url];    
  3.    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayBackDidFinish:)    
  4.                                                 name:MPMoviePlayerPlaybackDidFinishNotification    
  5.                                               object:[playerViewController moviePlayer]];    
  6.    // add to view  
  7.    [self.view addSubview:playerViewController.view];   
  8.   
  9.    // play movie   
  10.    MPMoviePlayerController *player = [playerViewController moviePlayer];    
  11. player.controlStyle = MPMovieControlStyleNone;    
  12. player.shouldAutoplay = YES;    
  13. player.repeatMode = MPMovieRepeatModeOne;  
  14. [player setFullscreen:YES animated:YES];    
  15. player.scalingMode = MPMovieScalingModeAspectFit;  
  16.    [player play];   
相關文章
相關標籤/搜索