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

來源:互聯網
上載者:User

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

#import "MainViewController.h"#import @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


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.