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

人已赞赏
iOS文章

iOS使用 TestFlight 分发测试你的 iOS app

2020-2-20 21:36:37

iOS文章

iOS NSFetchedResultsController查询结果控制器

2020-2-20 22:44:32

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索