iOS的影片播放 MediaPlayer 和 AVPlayer

本文对比了iOS开发中MediaPlayer与AVPlayer在影片播放方面的应用。MediaPlayer简便易用,适合基本需求;AVPlayer则提供了更多控制选项,适用于需要精细调整的场景。文章还提供了两种方式播放影片的具体代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

iOS的影片播放 MediaPlayer 和 AVPlayer

分类: Iphone 10488人阅读 评论(0) 收藏 举报

在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來播放影片

  1. NSString *filePath = [[NSBundle mainBundle] pathForResource:@"backspace" ofType:@"mov"];  
  2. NSURL *sourceMovieURL = [NSURL fileURLWithPath:filePath];  
  3.   
  4. moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:sourceMovieURL];  
  5. moviePlayer.view.frame=CGRectMake(0, 0, 1024, 768);  
  6. moviePlayer.controlStyle=MPMovieControlStyleNone;  
  7.   
  8. // Play the movie!   
  9. [self.view addSubview:moviePlayer.view];  
    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來播放影片

  1. NSString *filePath = [[NSBundle mainBundle] pathForResource:@"backspace" ofType:@"mov"];  
  2. NSURL *sourceMovieURL = [NSURL fileURLWithPath:filePath];  
  3.   
  4. AVAsset *movieAsset = [AVURLAsset URLAssetWithURL:sourceMovieURL options:nil];  
  5. AVPlayerItem *playerItem = [AVPlayerItem playerItemWithAsset:movieAsset];  
  6. AVPlayer *player = [AVPlayer playerWithPlayerItem:playerItem];  
  7. AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:player];  
  8. playerLayer.frame = self.view.layer.bounds;  
  9. playerLayer.videoGravity = AVLayerVideoGravityResizeAspect;  
  10.   
  11. [self.view.layer addSublayer:playerLayer];  
  12. [player play];  
    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];
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值