MediaPlayer

在iOS开发上,如果遇到需要播放影片,如开机动画…,我們很习惯地会使用 MediaPlayer 来播放影片,因为很方便使用,所以就一直使用下去。但是随着客户的要求越來越苛刻,尤其是过场动画或互动效果上的表現。所以如果在一些动画中还夹带影片一起运算,那势必机器会跑不动。所以在iOS 4之后,我们可以使用 AVPlayer 这个类别來进行更细微的操作。

备注:

MediaPlayer 的影片是放在 UIView 里面,而 AVPlayer 是放在 AVPlayerLayer 里面,AVPlayerLayer 是 CALayer 的子类别。

使用 MediaPlayer 前,要记得加入  MediaPlayer.framework 及  #import <MediaPlayer/MediaPlayer.h>

使用 AVPlayer 前,要记得加入  AVFoundation.framework 及  #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];  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值