iOS的影片播放 MediaPlayer 和 AVPlayer
2015-10-14 14:29
585 查看
comefrom:http://blog.csdn.net/think12/article/details/8549438
在iOS開發上,如果遇到需要播放影片,如開機動畫…,我們很習慣地會使用MediaPlayer來播放影片,因為很方便使用,所以就一直使用下去。但是 隨著客戶的要求越來越嚴苛,尤其是過場動畫或互動效果上的表現。所以如果在一些動畫中還挾帶影片一起運算,那勢必機器會跑不動。所以在iOS 4之後,我們可以使用AVPlayer這個類別來進行更細微的操作。
備註:
MediaPlayer的影片是放在UIView 裡面,而AVPlayer是放在AVPlayerLayer裡面,AVPlayerLayer是CALayer 的子類別。
使用MediaPlayer前,要記得加入MediaPlayer.framework及#import <MediaPlayer/MediaPlayer.h>
使用AVPlayer前,要記得加入AVFoundation.frameworkk及#import <AVFoundation/AVFoundation.h>
請參考以下的範例:
使用MediaPlayer來播放影片
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"backspace" ofType:@"mov"];
NSURL *sourceMovieURL = [NSURL fileURLWithPath:filePath];
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:sourceMovieURL];
moviePlayer.view.frame=CGRectMake(0, 0, 1024, 768);
moviePlayer.controlStyle=MPMovieControlStyleNone;
// Play the movie!
[self.view addSubview:moviePlayer.view];
使用AVPlayer來播放影片
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"backspace" ofType:@"mov"];
NSURL *sourceMovieURL = [NSURL fileURLWithPath:filePath];
AVAsset *movieAsset = [AVURLAsset URLAssetWithURL:sourceMovieURL options:nil];
AVPlayerItem *playerItem = [AVPlayerItem playerItemWithAsset:movieAsset];
AVPlayer *player = [AVPlayer playerWithPlayerItem:playerItem];
AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:player];
playerLayer.frame = self.view.layer.bounds;
playerLayer.videoGravity = AVLayerVideoGravityResizeAspect;
[self.view.layer addSublayer:playerLayer];
[player play];
在iOS開發上,如果遇到需要播放影片,如開機動畫…,我們很習慣地會使用MediaPlayer來播放影片,因為很方便使用,所以就一直使用下去。但是 隨著客戶的要求越來越嚴苛,尤其是過場動畫或互動效果上的表現。所以如果在一些動畫中還挾帶影片一起運算,那勢必機器會跑不動。所以在iOS 4之後,我們可以使用AVPlayer這個類別來進行更細微的操作。
備註:
MediaPlayer的影片是放在UIView 裡面,而AVPlayer是放在AVPlayerLayer裡面,AVPlayerLayer是CALayer 的子類別。
使用MediaPlayer前,要記得加入MediaPlayer.framework及#import <MediaPlayer/MediaPlayer.h>
使用AVPlayer前,要記得加入AVFoundation.frameworkk及#import <AVFoundation/AVFoundation.h>
請參考以下的範例:
使用MediaPlayer來播放影片
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"backspace" ofType:@"mov"];
NSURL *sourceMovieURL = [NSURL fileURLWithPath:filePath];
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:sourceMovieURL];
moviePlayer.view.frame=CGRectMake(0, 0, 1024, 768);
moviePlayer.controlStyle=MPMovieControlStyleNone;
// Play the movie!
[self.view addSubview:moviePlayer.view];
使用AVPlayer來播放影片
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"backspace" ofType:@"mov"];
NSURL *sourceMovieURL = [NSURL fileURLWithPath:filePath];
AVAsset *movieAsset = [AVURLAsset URLAssetWithURL:sourceMovieURL options:nil];
AVPlayerItem *playerItem = [AVPlayerItem playerItemWithAsset:movieAsset];
AVPlayer *player = [AVPlayer playerWithPlayerItem:playerItem];
AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:player];
playerLayer.frame = self.view.layer.bounds;
playerLayer.videoGravity = AVLayerVideoGravityResizeAspect;
[self.view.layer addSublayer:playerLayer];
[player play];
相关文章推荐
- iOS --小知识点
- IOS-13-XML解析代码(标准版)_另附XML和JSON均解析为字典数据源代码
- iOS---生成四位验证码
- NSThread简介
- iOS --一个倒计时类
- GCD简介
- iOS中使用NSJSONSerialization转换成json字符串后传递到后台的引号转义处理
- 关于在Xcode7.0.1上使用CocoaPods
- IOS 使用IconFont 图标
- 库
- (转)项目适配iOS9遇到的一些问题及解决办法
- Nagios:企业级系统监控方案
- iOS 8创建交互式通知
- ios-cell多行文字高度自适应
- IOS中使用手机号注册
- iOS开发-正则表达式的使用方法
- iOS开发-应用之间的跳转及通信
- iOS开发-应用之间的跳转及通信
- iOS 带有震荡效果的动画
- ARC下循环引用的问题 ++ 整理