您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 開發綜合

iOS:使用 MPMoviePlayerViewController 播放影片

編輯:iOS 開發綜合

以下示範如何使用 MediaPlayer 框架播放本地影片。

在 .h 檔中匯入類別庫

#import <MediaPlayer/MPMoviePlayerViewController.h>

在 .m 實作檔中

NSString *path = [[NSBundle mainBundle] pathForResource:@"name" ofType:@"mp4"];
NSURL *URL = [[NSURL alloc] initFileURLWithPath:path];

MPMoviePlayerViewController *_moviePlayerController =
    [[MPMoviePlayerViewController alloc] initWithContentURL:URL];
[[self GetViewController:self.view] presentMoviePlayerViewControllerAnimated:_moviePlayerController];
[_moviePlayerController.moviePlayer play];

重點說明

整體流程為:先用 NSBundle 取得打包在 App 內的影片檔路徑,轉成 NSURL,再以此建立 MPMoviePlayerViewController。接著將播放器以模態方式呈現,最後呼叫其 moviePlayer 的 play 方法開始播放。這個類別把播放與控制介面都封裝好了,使用起來相當簡便。

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved