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];
}

当前题目:iOS短视频源码开发MPMoviePlayerController-创新互联
URL分享:https://www.cdcxhl.com/article48/csihep.html

成都网站建设公司_创新互联,为您提供自适应网站域名注册网站维护企业建站定制网站网站营销

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

手机网站建设