ios網路學習------9 播放網路視訊

NS西北風發表於2014-07-02

IOS提供了叫做MPMoviePlayerController  MPMoviePlayerViewController兩個類,可以輕鬆用來實現視訊播放。MPMoviePlayerViewController只能全屏播放視訊。

#import "MainViewController.h"
#import <MediaPlayer/MediaPlayer.h>

@interface MainViewController ()
//視訊播放器
@property (strong, nonatomic) MPMoviePlayerController *player;
@property (strong, nonatomic) UIImageView *imageView;
@end

@implementation MainViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    //例項化視訊播放器
    NSURL *url = [[NSBundle mainBundle]URLForResource:@"promo_full" withExtension:@"mp4"];
    
    //視訊播放是流媒體的播放模式,所謂流媒體就是把視訊資料像流水一樣,變載入,變播放。
//    //提示:如果url中包含中文,需要新增百分號。
//    NSString *urlString = @"http:www.xxx.com/video/xxx.mp4";
//    NSURL *url = [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    
    self.player = [[MPMoviePlayerController alloc]initWithContentURL:url];
    //1設定播放器的大小
    [self.player.view setFrame:CGRectMake(0, 0, 320, 180)]; //16:9是主流媒體的樣式
    //2將播放器檢視新增到根檢視
    [self.view addSubview:self.player.view];
    
    //4播放
    [self.player play];
    //[self.player stop];
    //通過通知中心,以觀察者模式監聽視訊播放狀態
    //1 監聽播放狀態
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(stateChange) name:MPMoviePlayerPlaybackStateDidChangeNotification object:nil];
    //2 監聽播放完成
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(finishedPlay) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
    //3視訊截圖
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(caputerImage:) name:MPMoviePlayerThumbnailImageRequestDidFinishNotification object:nil];
    //3視訊截圖
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(caputerImage:) name:MPMoviePlayerThumbnailImageRequestDidFinishNotification object:nil];
    
    //4退出全屏通知
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(exitFullScreen) name:MPMoviePlayerDidExitFullscreenNotification object:nil];
    
    //非同步視訊截圖,可以在attimes指定一個或者多個時間。
    [self.player requestThumbnailImagesAtTimes:@[@10.0f, @20.0f] timeOption:MPMovieTimeOptionNearestKeyFrame];
    
    UIImageView *thumbnailImageView = [[UIImageView alloc]initWithFrame:CGRectMake(80, 200, 160, 90)];
    self.imageView = thumbnailImageView;
    [self.view addSubview:thumbnailImageView];
}

#pragma mark 退出全屏
- (void)exitFullScreen
{
    NSLog(@"退出全屏");
}

#pragma mark -播放器事件監聽
#pragma mark 視訊截圖 這個方法是非同步方法
- (void)caputerImage:(NSNotification *)notification
{
    NSLog(@"截圖 %@", notification);
    UIImage *image = notification.userInfo[@"MPMoviePlayerThumbnailImageKey"];
    [self.imageView setImage:image];
}

#pragma mark 播放器事件監聽
#pragma mark 播放完成
- (void)finishedPlay
{
    NSLog(@"播放完成");
}

#pragma mark 播放器視訊的監聽
#pragma mark 播放狀態變化
/*
 MPMoviePlaybackStateStopped,  //停止
 MPMoviePlaybackStatePlaying,  //播放
 MPMoviePlaybackStatePaused,   //暫停
 MPMoviePlaybackStateInterrupted,  //中斷
 MPMoviePlaybackStateSeekingForward, //快進
 MPMoviePlaybackStateSeekingBackward  //快退
 */
- (void)stateChange
{
    switch (self.player.playbackState) {
        case MPMoviePlaybackStatePaused:
            NSLog(@"暫停");
            break;
        case MPMoviePlaybackStatePlaying:
            //設定全屏播放
            [self.player setFullscreen:YES animated:YES];
            NSLog(@"播放");
            break;
        case MPMoviePlaybackStateStopped:
            //注意:正常播放完成,是不會觸發MPMoviePlaybackStateStopped事件的。
            //呼叫[self.player stop];方法可以觸發此事件。
            NSLog(@"停止");
            break;
        default:
            break;
    }
}

@end


相關文章