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

iOS 視頻播放詳解:MPMoviePlayerViewController 播放網路與本機影片

編輯:iOS 開發綜合

使用 MediaPlayer 播放影片之前,記得先匯入對應框架:

#import <MediaPlayer/MediaPlayer.h>

並在專案設定中加入 MediaPlayer.framework。

一、播放網路影片

- (void)openmovie
{
    MPMoviePlayerViewController *movie = [[MPMoviePlayerViewController alloc]
                                          initWithContentURL:[NSURL URLWithString:@"視頻網絡地址"]];

    [movie.moviePlayer prepareToPlay];
    [self presentMoviePlayerViewControllerAnimated:movie];
    [movie.moviePlayer setControlStyle:MPMovieControlStyleFullscreen];

    [movie.view setBackgroundColor:[UIColor clearColor]];
    [movie.view setFrame:self.view.bounds];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                            selector:@selector(movieFinishedCallback:)
                                                name:MPMoviePlayerPlaybackDidFinishNotification
                                              object:movie.moviePlayer];
}

- (void)movieFinishedCallback:(NSNotification *)notify
{
    // 影片播完,或 fullscreen 模式下 Done 按鈕被點擊時會收到此通知
    MPMoviePlayerController *theMovie = [notify object];

    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                   name:MPMoviePlayerPlaybackDidFinishNotification
                                                 object:theMovie];

    [self dismissMoviePlayerViewControllerAnimated:YES];
}

二、播放本機影片

- (void)openmovie
{
    NSString *url = [[NSBundle mainBundle] pathForResource:@"IMG_0322" ofType:@"mp4"];
    MPMoviePlayerViewController *playerViewController =
        [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL fileURLWithPath:url]];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                            selector:@selector(movieFinishedCallback:)
                                                name:MPMoviePlayerPlaybackDidFinishNotification
                                              object:[playerViewController moviePlayer]];

    [self.view addSubview:playerViewController.view];

    MPMoviePlayerController *player = [playerViewController moviePlayer];
    [player play];
}

- (void)movieFinishedCallback:(NSNotification *)aNotification
{
    MPMoviePlayerController *player = [aNotification object];
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                   name:MPMoviePlayerPlaybackDidFinishNotification
                                                 object:player];
    [player stop];
    [player.view removeFromSuperview];
}

三、幾個實務重點

播放網路與本機影片的唯一差別在於 URL 的建立方式:網路來源用 URLWithString,本機檔案用 fileURLWithPath。

無論哪一種,一定要註冊 MPMoviePlayerPlaybackDidFinishNotification,並在回呼中移除 observer,否則重複播放會累積多個監聽而導致崩潰或重複觸發。

另外要提醒的是,MPMoviePlayerController 與 MPMoviePlayerViewController 這一整組 API 從 iOS 9 起已被標記為 deprecated,新專案建議改用 AVPlayer 搭配 AVPlayerViewController,才能拿到更完整的播放控制與 AirPlay 支援。

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