iOS视频播放

1.iOS8.0的实现方法

#import <MediaPlayer/MediaPlayer.h>
@interface ViewController ()
@property (nonatomic, strong) MPMoviePlayerController *mpc;
@end

- (void)viewDidLoad {
    [super viewDidLoad];
   //  控制工具栏的样式
//  MPMovieControlStyleNone 没有控制栏
//  MPMovieControlStyleEmbedded 嵌入的 默认
//  MPMovieControlStyleFullscreen 全屏的
//    mpc.controlStyle = MPMovieControlStyleEmbedded;

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayerPlaybackDidFinishNotification:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];

    NSURL *URL = [[NSBundle mainBundle] URLForResource:@"abc.mp4" withExtension:nil];
    MPMoviePlayerController *mpc = [[MPMoviePlayerController alloc] initWithContentURL:URL];

//  必须的在添加之前设置大小
    mpc.view.frame = CGRectMake(50, 50, 200, 200);

//  把view添加到控制器上
    [self.view addSubview:mpc.view];

//  播放
    [mpc play];

//  强引用
    self.mpc = mpc;
}


- (void) moviePlayerPlaybackDidFinishNotification:(NSNotification *) notification {

    MPMovieFinishReason reason = [notification.userInfo[MPMoviePlayerPlaybackDidFinishReasonUserInfoKey] intValue];
//  reason结束的原因,正常结束就下一曲
    if (reason == MPMovieFinishReasonPlaybackEnded) {
        NSURL *URL = [[NSBundle mainBundle] URLForResource:@"高清.mp4" withExtension:nil];
        self.mpc.contentURL = URL;

        [self.mpc play];
    }

}

2.iOS9.0的实现方法,可实现画中画的效果

/// 需要导入两个框架
#import <AVKit/AVKit.h>
#import <AVFoundation/AVFoundation.h>
//实现代理<AVPlayerViewControllerDelegate>
//  创建播放控制器
    AVPlayerViewController *pvc = [[AVPlayerViewController alloc] init];
    NSURL *URL = [[NSBundle mainBundle] URLForResource:@"Alizee_La_Isla_Bonita.mp4" withExtension:nil];
    //  必须设置AVPlayer
    pvc.player = [[AVPlayer alloc] initWithURL:URL];

//  设置代理
    pvc.delegate = self;
    //  展示
    [self presentViewController:pvc animated:YES completion:^{
        // 播放
        [pvc.player play];
    }];
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值