iOS视频功能模块的开发
一、使用MPMoviePlayerController进行视频播放
MPMoviePlayerController是iOS中进行视频播放开发的一个控制类,里面涵盖了视频播放中大部分的需求功能,在使用这个框架时,需要导入头文件<MediaPlayer/MediaPlayer.h>。
1、初始化方法
MPMoviePlayerController可以播放网络视频,也可以播放本地视频,通过不同的URL来进行初始化,例如本地视频的初始化如下:
1
2
3
4
5
6
|
NSString *path = [[NSBundle mainBundle] pathForResource:fileName ofType:@ "mp4" ];
NSURL *url = [NSURL fileURLWithPath:path];
MPMoviePlayerController * movie = [[MPMoviePlayerController alloc] initWithContentURL:url];
|
初始化和完成相关配置后,我们需要将MPMoviePlayerController对象的View添加在我们需要的UI视图上,这个控制器只提供的控制的相关功能,外部的UI并没有为我们提供好。
2、相关属性与方法
@property (nonatomic, copy) NSURL *contentURL;
视频文件的url地址
@property (nonatomic, readonly) UIView *view;
播放器view,在使用之前,必须设置frame大小,然后将其添加在我们的UI视图上
@property (nonatomic, readonly) UIView *backgroundView;
播放器背景颜色
@property (nonatomic, readonly) MPMoviePlaybackState playbackState;
播放器的当前播放状态,枚举定义如下:
1
2
3
4
5
6
7
8
|
typedef NS_ENUM(NSInteger, MPMoviePlaybackState) {
MPMoviePlaybackStateStopped,
MPMoviePlaybackStatePlaying,
MPMoviePlaybackStatePaused,
MPMoviePlaybackStateInterrupted,
MPMoviePlaybackStateSeekingForward,
MPMoviePlaybackStateSeekingBackward
};
|
@property (nonatomic, readonly) MPMovieLoadState loadState;
播放器的网络缓存状态,枚举定义如下:
1
2
3
4
5
6
|
typedef NS_OPTIONS(NSUInteger, MPMovieLoadState) {
MPMovieLoadStateUnknown = 0,
MPMovieLoadStatePlayable = 1 << 0,
MPMovieLoadStatePlaythroughOK = 1 << 1,
MPMovieLoadStateStalled = 1 << 2,
};
|
@property (nonatomic) MPMovieControlStyle controlStyle;
播放器风格,枚举如下:
1
2
3
4
5
6
7
|
typedef NS_ENUM(NSInteger, MPMovieControlStyle) {
MPMovieControlStyleNone,
MPMovieControlStyleEmbedded,
MPMovieControlStyleFullscreen,
MPMovieControlStyleDefault = MPMovieControlStyleEmbedded
};
|
@property (nonatomic) MPMovieRepeatMode repeatMode;
播放器的循环模式,枚举如下:
1
2
3
4
|
typedef NS_ENUM(NSInteger, MPMovieRepeatMode) {
MPMovieRepeatModeNone,
MPMovieRepeatModeOne
};
|
@property (nonatomic) BOOL shouldAutoplay;
是否开启自动播放
@property (nonatomic, getter=isFullscreen) BOOL fullscreen;
设置是否充满屏幕
- (void)setFullscreen:(BOOL)fullscreen animated:(BOOL)animated;
设置是否充满屏幕,带动画效果
@property (nonatomic) MPMovieScalingMode scalingMode;
设置播放器的填充方式,枚举定义如下:
1
2
3
4
5
6
|
typedef NS_ENUM(NSInteger, MPMovieScalingMode) {
MPMovieScalingModeNone,
MPMovieScalingModeAspectFit,
MPMovieScalingModeAspectFill,
MPMovieScalingModeFill
};
|
@property (nonatomic, readonly) BOOL readyForDisplay NS_AVAILABLE_IOS(6_0);
返回YES说明数据栈已经缓存好数据,返回NO则没有缓存好
@property (nonatomic, readonly) MPMovieMediaTypeMask movieMediaTypes;
数据文件的格式,枚举如下:
1
2
3
4
5
|
typedef NS_OPTIONS(NSUInteger, MPMovieMediaTypeMask) {
MPMovieMediaTypeMaskNone = 0,
MPMovieMediaTypeMaskVideo = 1 << 0,
MPMovieMediaTypeMaskAudio = 1 << 1
};
|
@property (nonatomic) MPMovieSourceType movieSourceType;
视频的数据类型,枚举如下:
1
2
3
4
5
|
typedef NS_ENUM(NSInteger, MPMovieSourceType) {
MPMovieSourceTypeUnknown,
MPMovieSourceTypeFile,
MPMovieSourceTypeStreaming
};
|
@property (nonatomic, readonly) NSTimeInterval duration;
视频文件的时长
@property (nonatomic, readonly) NSTimeInterval playableDuration;
缓存完成能够播放的时长
@property (nonatomic, readonly) CGSize naturalSize;
视频的原始大小
@property (nonatomic) NSTimeInterval initialPlaybackTime;
播放器开始播放的时间
@property (nonatomic) NSTimeInterval endPlaybackTime;
播放器结束播放的时间
@property (nonatomic) BOOL allowsAirPlay;
是否允许云端播放
- (void)requestThumbnailImagesAtTimes:(NSArray *)playbackTimes timeOption:(MPMovieTimeOption)optio;
获取视频某一些时间点的缩略图,参数枚举如下,生成缩略图的数据回调在后面的通知中详说:
1
2
3
4
|
typedef NS_ENUM(NSInteger, MPMovieTimeOption) {
MPMovieTimeOptionNearestKeyFrame,
MPMovieTimeOptionExact
};
|
与播放控制相关的方法如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
- ( void )prepareToPlay;
@property(nonatomic, readonly) BOOL isPreparedToPlay;
- ( void )play;
- ( void )pause;
- ( void )stop;
@property(nonatomic) NSTimeInterval currentPlaybackTime;
@property(nonatomic) float currentPlaybackRate;
- ( void )beginSeekingForward;
- ( void )beginSeekingBackward;
- ( void )endSeeking;
|
3、系统相关通知
MPMoviePlayerController的系统回调并没有采用代理的设计模式,而是采用的系统发通知,我们注册观察者,接收我们需要的通知。举例几种常用通知如下:
NSString * const MPMoviePlayerScalingModeDidChangeNotification;
播放器缩放产生改变时发送的通知
NSString * const MPMoviePlayerPlaybackDidFinishNotification;
播放结束时发送的通知
NSString * const MPMoviePlayerPlaybackStateDidChangeNotification;
播放状态改变时发送的通知
NSString * const MPMoviePlayerLoadStateDidChangeNotification;
缓冲状态改变时发送的通知
NSString * const MPMoviePlayerNowPlayingMovieDidChangeNotification;
当前播放的视频改变时发送的通知
NSString * const MPMoviePlayerWillEnterFullscreenNotification;
将要进入全屏模式时发送的通知
NSString * const MPMoviePlayerDidEnterFullscreenNotification;
已经进入全屏时发送的通知
NSString * const MPMoviePlayerWillExitFullscreenNotification;
将要退出全屏时发送的通知
NSString * const MPMoviePlayerDidExitFullscreenNotification;
已经退出全屏时发送的通知
NSString * const MPMoviePlayerThumbnailImageRequestDidFinishNotification;
获取缩略图完成时发送的通知
二、MPMoviePlayerViewController视频视图控制器
如果你很熟悉MVC,你可能会觉得MPMoviePlayerController的设计模式非常蹩脚,强行要求你将控制器的视图分离出来加在另外的UI上,徒增的代码逻辑的混乱,那么你想的没错,MPMoviePlayerViewController可能就是为了解决这个问题。
MPMoviePlayerViewController将视图封装在了一起,其中有一个成员对象是MPMoviePlayerController类型,类似C++中的has-a逻辑,我们只需要对MPMoviePlayerViewController进行的简单的初始化后,对其中MPMoviePlayerController进行其他配置,之后通过模态跳转切换控制器即可。
方法如下:
- (instancetype)initWithContentURL:(NSURL *)contentURL;
初始化方法,和上面类似
@property (nonatomic, readonly) MPMoviePlayerController *moviePlayer;
播放器对象
- (void)presentMoviePlayerViewControllerAnimated:(MPMoviePlayerViewController *)moviePlayerViewController;
- (void)dismissMoviePlayerViewControllerAnimated;
viewController的模态跳转方法,也可以通过导航push与pop
代码示例如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
@interface ViewController2 ()
@property(nonatomic,strong)MPMoviePlayerController * movie;
@property(nonatomic,strong)MPMoviePlayerViewController * viewController;
@end
@implementation ViewController2
- ( void )viewDidLoad {
[super viewDidLoad];
[self playMovie:@ "111" ];
}
-( void )playMovie:(NSString *)fileName{
NSString *path = [[NSBundle mainBundle] pathForResource:fileName ofType:@ "mp4" ];
NSURL *url = [NSURL fileURLWithPath:path];
_viewController = [[MPMoviePlayerViewController alloc]initWithContentURL:url];
_movie=_viewController.moviePlayer;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(myMovieFinishedCallback:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:_movie];
_movie.fullscreen=YES;
}
-( void )touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
[_movie play];
[self presentViewController:_viewController animated:YES completion:nil];
}
-( void )myMovieFinishedCallback:(NSNotification*)notify
{
MPMoviePlayerController* theMovie = [notify object];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:theMovie];
[theMovie.view removeFromSuperview];
}
|