iOS 短視頻源碼開發MPMoviePlayerController

文:布穀惠澤/來源:山東布穀鳥網絡

  MPMoviePlayerController用來播放視頻,在iOS9之後被棄用(iOS9之後蘋果推薦我們使用AVPlayer,AVPlayer相對複雜但靈活),由於APP往往要兼容iOS9之前的版本,所有MPMoviePlayerController還是很重要的。

在我的另一篇文章中分享了一個基於MPMoviePlayerController的播放器,大家可以看看,目前還不完整。小夥伴們可以關注一下我的簡書。謝謝

MPMoviePlayerController的簡單使用
需要添加這個框架MediaPlayer.framework
#import <MediaPlayer/MediaPlayer.h>
#pragma mark - 本地
    
    NSString* _moviePath=[[NSBundle mainBundle]pathForResource:@"popeye" ofType:@"mp4"];
    self.player=[[MPMoviePlayerController alloc]initWithContentURL:[NSURL fileURLWithPath:_moviePath]];
    [self.view addSubview:self.player.view];
    
    self.player.view.frame=CGRectMake(0, 0, self.view.frame.size.width, CGRectGetWidth(self.view.frame)*(9.0/16.0));
    self.player.movieSourceType = MPMovieSourceTypeFile;// 播放本地視頻時需要這句
//    self.player.controlStyle = MPMovieControlStyleNone;// 不需要進度條
    self.player.shouldAutoplay = YES;// 是否自動播放(默認爲YES)
//    self.player.scalingMode=MPMovieScalingModeAspectFill;
    [self.player prepareToPlay];//緩存
//    [self.player play];//可以不加這句
#pragma mark - 網絡

    NSURL* url = [NSURL URLWithString:@"https://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4"];
    _player = [[MPMoviePlayerController alloc] initWithContentURL:url];
    [self.view addSubview:self.player.view];

    self.player.view.frame=CGRectMake(0, 0, self.view.frame.size.width, CGRectGetWidth(self.view.frame)*(9.0/16.0));
    [self.player prepareToPlay];
    [self.player play];
#pragma mark - 直播
    NSURL* url = [NSURL URLWithString:@"http://live.hkstv.hk.lxdns.com/live/hks/playlist.m3u8"];
    _player = [[MPMoviePlayerController alloc] initWithContentURL:url];
    [self.view addSubview:self.player.view];
    
    self.player.view.frame=CGRectMake(0, 0, self.view.frame.size.width, CGRectGetWidth(self.view.frame)*(9.0/16.0));
    self.player.controlStyle=MPMovieSourceTypeStreaming;//直播
    [self.player prepareToPlay];
//    [self.player play];
MPMoviePlayerController提供了很多通知,這裏我就簡單的監聽2個。我們可以通過監聽到的信息做相應的處理。
#pragma mark - Notification
    
    //監聽視頻播放結束
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(endPlay) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
    
    //監聽當前視頻播放狀態
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(loadStateDidChange:) name:MPMoviePlayerLoadStateDidChangeNotification object:nil];

#pragma mark - Notification function

-(void)endPlay
{
    NSLog(@"播放結束");
}

-(void)loadStateDidChange:(NSNotification*)sender
{
    switch (self.player.loadState) {
        case MPMovieLoadStatePlayable:
        {
            NSLog(@"加載完成,可以播放");
        }
            break;
        case MPMovieLoadStatePlaythroughOK:
        {
            NSLog(@"緩衝完成,可以連續播放");
        }
            break;
        case MPMovieLoadStateStalled:
        {
            NSLog(@"緩衝中");
        }
            break;
        case MPMovieLoadStateUnknown:
        {
            NSLog(@"未知狀態");
        }
            break;
        default:
            break;
    }
}

#pragma mark - dealloc

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章