iOS開發之視頻播放

1如何播放視頻git

iOS提供了MPMoviePlayerController、MPMoviePlayerViewController兩個類,能夠用來輕鬆播放視頻和網絡流媒體\網絡音頻。github

提示:網絡音頻一樣使用此控制器播放。YouTobe就是用MPMoviePlayerController實現的。數組

MPMoviePlayerViewController只能全屏播放視頻。網絡

上述兩個類都定義在了MediaPlayer框架中,必須手動添加app

2MPMoviePlayerController支持的格式框架

MPMoviePlayerControlleride

繼承自NSObject測試

內部有個view能夠展現視頻內容atom

將該視圖添加其餘控制器的view上,便可顯示視頻內容url

MPMoviePlayerController能夠播放的視頻格式包括:

H.26四、MPEG-4等

支持的文件擴展名包括:avi,mkv,mov,m4v,mp4等

能夠從蘋果官網:http://support.apple.com/kb/HT1425下載一些用來測試的視頻文件,文件都比較小

提示:MPMoviePlayerController並不支持全部的視頻格式,若是要播放不支持的視頻格式,須要藉助第三方框架進行解碼,如VLC

https://github.com/videolan/vlc

3MPMoviePlayerController的使用

加載視頻資源(注意,若是urlnil一樣能夠加載)

NSAssert(self.url, @"URL不能爲空");

[[MPMoviePlayerController alloc] initWithContentURL:self.url];

顯示:

[self.view addSubview:self.moviePlayer.view];

經過設置AutoresizingMask屬性能夠在橫豎屏轉換時自動調整視圖大小

播放:

[self.moviePlayer play];

全屏:

[self.moviePlayer setFullscreen:YES animated:YES];

 

【備註】MPMoviePlayerController的播放狀態是經過通知中心監聽的

例如:

#import "MJMoviePlayerViewController.h"
#import <MediaPlayer/MediaPlayer.h>
 
@interface  MJMoviePlayerViewController ()
@property (nonatomic, strong) MPMoviePlayerController *moviePlayer;
@end
 
@implementation MJMoviePlayerViewController
 
- (MPMoviePlayerController *)moviePlayer
{
    if (!_moviePlayer) {
        // 負責控制媒體播放的控制器
              NSURL *url =
[[NSBundle mainBundle] URLForResource:@"promo_full.mp4" withExtension:nil];
    _moviePlayer =
[[MPMoviePlayerController alloc] initWithContentURL:url];
_moviePlayer.view.frame = self.view.bounds;
_moviePlayer.view.autoresizingMask =
UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
    [self.view addSubview:_moviePlayer.view];
    }
    return _moviePlayer;
}
 
- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.moviePlayer play];
}
 
@end

運行結果:

4經常使用監聽通知事件

狀態變化:

MPMoviePlayerPlaybackStateDidChangeNotification

 

播放結束:

MPMoviePlayerPlaybackDidFinishNotification

 

退出全屏:

MPMoviePlayerDidExitFullscreenNotification

 

截屏完成:

MPMoviePlayerThumbnailImageRequestDidFinishNotification

 

截屏方法:

-requestThumbnailImagesAtTimes:timeOption:

例如:

#import "MJMoviePlayerViewController.h"
#import <MediaPlayer/MediaPlayer.h>
 
@interface MJMoviePlayerViewController ()
@property (nonatomic, strong) MPMoviePlayerController *moviePlayer;
@end
 
@implementation MJMoviePlayerViewController
 
- (MPMoviePlayerController *)moviePlayer
{
    if (!_moviePlayer) {
        // 負責控制媒體播放的控制器
        _moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:self.movieURL];
        _moviePlayer.view.frame = self.view.bounds;
        _moviePlayer.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        [self.view addSubview:_moviePlayer.view];
    }
    return _moviePlayer;
}
 
- (void)viewDidLoad
{
    [super viewDidLoad];
   
    [self.moviePlayer play];
    [self addNotification];
}
 
- (void)viewDidAppear:(BOOL)animated
{
    self.moviePlayer.fullscreen = YES;
}
 
#pragma mark - 添加通知
- (void)addNotification
{
    // 1. 添加播放狀態的監聽
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
 
[nc addObserver:self selector:@selector(stateChanged) name:
MPMoviePlayerPlaybackStateDidChangeNotification object:nil];
    // 2. 播放完成
[nc addObserver:self selector:@selector(finished) name:
MPMoviePlayerPlaybackDidFinishNotification object:nil];
    // 3. 全屏
[nc addObserver:self selector:@selector(finished) name:
MPMoviePlayerDidExitFullscreenNotification object:nil];
    // 4. 截屏完成通知
[nc addObserver:self selector:@selector(captureFinished:) name:
MPMoviePlayerThumbnailImageRequestDidFinishNotification object:nil];
   
    // 數組中有多少時間,就通知幾回
    // MPMovieTimeOptionExact 精確
    // MPMovieTimeOptionNearestKeyFrame 大概
    [self.moviePlayer requestThumbnailImagesAtTimes:@[@(5.0), @(20.0)] timeOption:MPMovieTimeOptionNearestKeyFrame];
}
 
- (void)captureFinished:(NSNotification *)notification
{
    NSLog(@"%@", notification);
    [self.delegate moviePlayerDidCapturedWithImage:notification.userInfo[MPMoviePlayerThumbnailImageKey]];
}
 
- (void)finished
{
    // 1. 刪除通知監聽
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    // 2. 返回上級窗體
    // 誰申請,誰釋放!
    [self.delegate moviePlayerDidFinished];
    NSLog(@"完成");
}
 
/**
 MPMoviePlaybackStateStopped,           中止
 MPMoviePlaybackStatePlaying,           播放
 MPMoviePlaybackStatePaused,            暫停
 MPMoviePlaybackStateInterrupted,       中斷
 MPMoviePlaybackStateSeekingForward,    下一個
 MPMoviePlaybackStateSeekingBackward    前一個
 */
- (void)stateChanged
{
    switch (self.moviePlayer.playbackState) {
        case MPMoviePlaybackStatePlaying:
            NSLog(@"播放");
            break;
        case MPMoviePlaybackStatePaused:
            NSLog(@"暫停");
            break;
        case MPMoviePlaybackStateStopped:
            // 執行[self.moviePlayer stop]或者前進後退不工做時會觸發
            NSLog(@"中止");
            break;
        default:
            break;
    }
}
 
@end
相關文章
相關標籤/搜索