博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iOS: ios视频播放(MPMediaPlayerController,AVPlayer,AVPlayerViewcontroller、ffmpeg-AVPlayer)...
阅读量:5965 次
发布时间:2019-06-19

本文共 7792 字,大约阅读时间需要 25 分钟。

介绍:

和音频播放一样,ios也提供个很多的API。如mediaPlayer.framework下的MPMediaPlayerController、AVFounditon.framework下的AVPlayer和AVKit下的AVPlayerViewcontroller。MPMovieplayerController已经在ios9.0中被废弃了,用来替代的是AVPlayerViewcontroller。尽管如此,但还是说一下比较熟悉的MPMovieplayerController.

 

1、MPMovieplayerController

<1>初始化MPMovieplayerController

// 创建本地URL(也可创建基于网络的URL)NSURL* movieUrl = [[NSBundlemainBundle]URLForResource:@"movie"withExtension:@"mp4"];// 使用指定URL创建MPMoviePlayerController// MPMoviePlayerController将会播放该URL对应的视频MPMoviePlayerController *moviePlayer = [[MPMoviePlayerControlleralloc]initWithContentURL:movieUrl];

<2>设置属性并添加到当前控制器的View上

// 设置该播放器的控制条风格。moviePlayer.controlStyle =MPMovieControlStyleEmbedded;// 设置该播放器的缩放模式moviePlayer.scalingMode =MPMovieScalingModeAspectFit;//设置播放视图大小 _movieplayer.view.frame = self.view.bounds;//设置播放视图宽高自适应调整_movieplayer.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
//添加到当前控制器的View上[self.View  addSubview:moviePlayer.view];

 <3>播放选择

//播放缓冲[self.movieplayer prepareToPlay];//播放开始[moviePlayer play];//播放暂停[moviePlayer pause];//播放停止[moviePlayer stop];

由于属性太多,这里只列出能完成简单的播放器的属性,其他属性可在头文件中查找,注意:播放的状态的监听和截图功能都是通过通知实现的。

这里给一个具体的代码:

ViewController类

#import "ViewController.h"#import "PlayViewController.h"@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *imageView;@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; self.navigationItem.title = @"播放器";}-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{ PlayViewController *playContronller = segue.destinationViewController; playContronller.delegate = self;}#pragma mark
-(void)dismissPlayViewController{ [self dismissViewControllerAnimated:self completion:nil];}-(void)setImageWithThumbnailImage:(UIImage *)image{ self.imageView.image = image;}@end
View Code

PlayViewController类

#import 
@protocol PlayViewControllerDelegate
//关闭模态窗体-(void)dismissPlayViewController;//设置截图-(void)setImageWithThumbnailImage:(UIImage *)image;@end@interface PlayViewController : UIViewController@property (assign,nonatomic)id
delegate;@end
View Code
#import "PlayViewController.h"#import 
#import
@interface PlayViewController ()@property (strong,nonatomic)MPMoviePlayerController *movieplayer;@end@implementation PlayViewController/** 创建视频播放器 */-(MPMoviePlayerController *)movieplayer{ if (!_movieplayer) { //设置播放器的属性 NSURL *URL = [[NSBundle mainBundle]URLForResource:@"风景" withExtension:@".mp4"]; _movieplayer = [[MPMoviePlayerController alloc]initWithContentURL:URL]; _movieplayer.view.frame = self.view.bounds; _movieplayer.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; //添加通知 [self addNotify]; } return _movieplayer;}- (void)viewDidLoad { [super viewDidLoad]; [self.view addSubview:self.movieplayer.view]; [self.movieplayer prepareToPlay]; [self.movieplayer play];}//添加通知-(void)addNotify{ //注册播放完成的通知 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayBackDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:_movieplayer]; //注册播放状态改变的通知 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayBackStateChanged) name:MPMoviePlayerPlaybackStateDidChangeNotification object:_movieplayer]; //注册全屏通知 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayDidEnterFullScreen) name:MPMoviePlayerDidEnterFullscreenNotification object:_movieplayer]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayDidExitFullScreen) name:MPMoviePlayerDidExitFullscreenNotification object:_movieplayer]; //注册请求截屏的通知 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayerRequestThumbnailImage) name:MPMoviePlayerThumbnailImageRequestDidFinishNotification object:_movieplayer]; //发送截图请求 [self.movieplayer requestThumbnailImagesAtTimes:@[@(5.0f),@(10.0f)] timeOption:MPMovieTimeOptionNearestKeyFrame];}//截屏-(void)moviePlayerRequestThumbnailImage{ //获取图片 UIImage *image = [self.movieplayer thumbnailImageAtTime:5.0f timeOption:MPMovieTimeOptionNearestKeyFrame]; NSLog(@"%@",image); if ([self.delegate respondsToSelector:@selector(setImageWithThumbnailImage:)]) { [self.delegate setImageWithThumbnailImage:image]; }}//播放完成-(void)moviePlayBackDidFinish:(NSNotification *)notification{ NSLog(@"播放完成");}//播放状态改变-(void)moviePlayBackStateChanged{ switch (self.movieplayer.playbackState) { case MPMoviePlaybackStateStopped: NSLog(@"播放停止"); break; case MPMoviePlaybackStatePlaying: NSLog(@"播放中"); break; case MPMoviePlaybackStatePaused: NSLog(@"播放暂停"); break; case MPMoviePlaybackStateInterrupted: NSLog(@"播放中断"); break; case MPMoviePlaybackStateSeekingForward: NSLog(@"快进播放"); break; case MPMoviePlaybackStateSeekingBackward: NSLog(@"回退播放"); break; }}//进入全屏-(void)moviePlayDidEnterFullScreen{ NSLog(@"进入全屏");}//离开全屏-(void)moviePlayDidExitFullScreen{ NSLog(@"离开全屏"); //关闭播放器 [self.movieplayer.view removeFromSuperview]; self.movieplayer = nil; if ([self.delegate respondsToSelector:@selector(dismissPlayViewController)]) { [self.delegate dismissPlayViewController]; } [[NSNotificationCenter defaultCenter]removeObserver:self];}@end
View Code

演示结果截图: 

播放前:                                             播放中:                          

  

  播放过程中一个指定时间点的视频截图

 

2、AVPlayer

AVPlayer既可以播放音乐又可以播放视频;使用AVPlayer不能直接显示视频,必须要加入AVPlayerLayer中,并添加到其他能显示的layer中。

//获取播放源的urlNSString *filePath = [[NSBundlemainBundle] pathForResource:@"backspace"ofType:@"mov"];NSURL *sourceMovieURL = [NSURLfileURLWithPath: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];

 

3、AVPlayerViewcontroller

AVPlayerViewcontroller继承自UIViewController,一般适用于点击一个视频缩略图,modal出一个新的界面来进行播放的情况。

用法:

//创建播放器AVPlayerViewController *player = [[AVPlayerViewController alloc]init];player.player = [[AVPlayer alloc]initWithURL:movieUrl];//模态出播放器[self presentViewController:player animated:YES completion:nil];

 

4、ffmpeg-AVPlayer-for-iOS——强大的iOS视频播放框架

ffmpeg-avplayer-for-ios是一个微小但是强大的iOS视频播放框架,能够播放大多数的音频和视频格式文件(包括网络音频和视频流),没有格式转换的烦恼,不依赖VLC和SDL。支持播放1080P视频,但是建议只在iPad Air & iPhone 5s设备上播放1080P视频。

 
ffmpeg-avplayer-for-ios是专门针对iOS开发者编写的AVPlayer SDK,只需几行代码便可在应用中加入强大的视频播放器。
ffmpeg-avplayer-for-ios依赖于iOS OpenGL ES 2.0框架和基于LGPL协议开源的FFmpeg类库。
 
截图:
 
特性:
1、支持ARC;
2、支持armv7, armv7s, arm64, i386和x86-64;
3、支持播放本地文件;
4、支持Windows SMB/CIFS文件 (PRO version only);
5、支持http, https和rtmp协议;
6、解析音频和视频持续时间、帧尺寸信息;
7、获取视频缩略图;
8、实时位&帧率和网络缓冲进度计算;
9、查询当前播放时间信息;
10、播放速度控制;
11、亮度,对比度,饱和度控制;
12、支持音频、视频播放背景;
13、支持全屏模式;
14、系统音量控制;
15、从指定的事件位置播放;
16、支持播放、暂停、停止、重播、快进、快退。
17、支持音频会话中断处理程序;
18、自定义字幕字体;
19、支持嵌入字幕格式;
20、支持多个音频、字幕流;
21、支持SRT, ASS, SSA, SMI格式;
 
Github托管地址:
 
 

转载地址:http://lmvax.baihongyu.com/

你可能感兴趣的文章
mysqld -install命令时出现install/remove of the service denied错误的原因和解决办法
查看>>
玩家游戏状态
查看>>
Android 小技巧-- TextView与EditText 同步显示
查看>>
苹果企业版帐号申请记录
查看>>
C++ Error: error LNK2019: unresolved external symbol
查看>>
Bitmap 和Drawable 的区别
查看>>
Java操作mongoDB2.6的常见API使用方法
查看>>
信息熵(Entropy)究竟是用来衡量什么的?
查看>>
如何给服务器设置邮件警报。
查看>>
CEF js调用C#封装类含注释
查看>>
麦克劳林
查看>>
延时接通电路
查看>>
周爱民:真正的架构师是没有title的(图灵访谈)
查看>>
Eclipse SVN修改用户名和密码
查看>>
架构师的职责都有哪些?
查看>>
Cool!15个超炫的 CSS3 文本特效【上篇】
查看>>
SVN: bdb: BDB1538 Program version 5.3 doesn't match environment version 4.7
查看>>
jsp内置对象作业3-application用户注册
查看>>
android115 自定义控件
查看>>
iOS uuchart 用法
查看>>