iOS中有多种方式可以实现视频播放功能:MPMoviePlayerController、MPMoviePlayerViewController、AVPlayer。这篇文章介绍前两个。
MPMoviePlayerController
MPMoviePlayerController支持MOV、MP4、M4V、3GP等常用格式的视频,它可以进行播放、暂停、停止、全屏的操作,虽然它是个视图控制器,但使用时需要把它的view添加到父视图上才能显示。 #import "ViewController.h" #import <MediaPlayer/MediaPlayer.h>
代码语言:javascript复制@interface ViewController ()
@property (nonatomic, strong) MPMoviePlayerController *player;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
// 需要把播放器的view添加到父视图才能显示
[self.view addSubview:self.player.view];
[self addNotification];
}
- (IBAction)playButtonClick:(id)sender {
[self.player play];
}
// 懒加载
- (MPMoviePlayerController *)player {
if (!_player) {
_player = [[MPMoviePlayerController alloc] initWithContentURL:[self getNetworkUrl]];
_player.view.frame = CGRectMake(0, 64, self.view.bounds.size.width, 200);
}
return _player;
}
// 本地视频
- (NSURL *)getLocalUrl{
NSString *urlString = [[NSBundle mainBundle] pathForResource:@"xxx.mp4" ofType:nil];
NSURL *url = [NSURL fileURLWithPath:urlString];
return url;
}
// 网络视频
- (NSURL *)getNetworkUrl{
NSString *urlString = @"http://svideo.spriteapp.com/video/2016/0914/4cb325f6-7a09-11e6-a458-d4ae5296039d_wpd.mp4";
NSURL *url = [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
return url;
}
// 播放状态改变
- (void)mediaPlayerPlaybackStateChange:(NSNotification *)noti{
switch (self.player.playbackState) {
case MPMoviePlaybackStatePlaying:
NSLog(@"正在播放");
break;
case MPMoviePlaybackStatePaused:
NSLog(@"暂停播放");
break;
case MPMoviePlaybackStateStopped:
NSLog(@"停止播放");
break;
case MPMoviePlaybackStateInterrupted:
NSLog(@"播放中断");
break;
case MPMoviePlaybackStateSeekingForward:
NSLog(@"向前");
break;
case MPMoviePlaybackStateSeekingBackward:
NSLog(@"向后");
break;
default:
break;
}
}
// 播放完成
- (void)mediaPlayerPlaybackFinished:(NSNotification *)noti{
NSLog(@"播放结束");
}
// 添加观察者监听视频状态
- (void)addNotification{
NSNotificationCenter *noti = [NSNotificationCenter defaultCenter];
[noti addObserver:self selector:@selector(mediaPlayerPlaybackStateChange:) name:MPMoviePlayerPlaybackStateDidChangeNotification object:self.player];
[noti addObserver:self selector:@selector(mediaPlayerPlaybackFinished:) name:MPMoviePlayerPlaybackDidFinishNotification object:self.player];
}
- (void)dealloc{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
@end
使用上面的代码,一个视频播放器就创建完成了,点击播放按钮即可播放视频。它能够播放本地和网络视频,播放信息是通过通知发出的,我们只需要添加观察者,就能监听视频的播放状态。效果图如下:
MPMoviePlayerViewController
使用上面的播放控制器,可以自己设置播放器的frame,把它添加到任意位置,而有时候我们的项目只需要全屏播放视频,不需要小屏播放,这时就可以用MPMoviePlayerViewController来实现了。 #import "ViewController.h" #import <MediaPlayer/MediaPlayer.h>
代码语言:javascript复制@interface ViewController ()
@property (nonatomic, strong) MPMoviePlayerViewController *playerViewController;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 点击播放按钮进行播放
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
// 每次退出播放界面都清空,防止再次点击不播放
[[NSNotificationCenter defaultCenter] removeObserver:self];
self.playerViewController = nil;
}
// 播放按钮
- (IBAction)playButtonClick:(id)sender {
[self presentMoviePlayerViewControllerAnimated:self.playerViewController];
}
// 懒加载
- (MPMoviePlayerViewController *)playerViewController{
if (!_playerViewController) {
_playerViewController = [[MPMoviePlayerViewController alloc]initWithContentURL:[self getNetworkUrl]];
[self addNotification];
}
return _playerViewController;
}
// 本地视频
- (NSURL *)getLocalUrl{
NSString *urlString = [[NSBundle mainBundle] pathForResource:@"xxx.mp4" ofType:nil];
NSURL *url = [NSURL fileURLWithPath:urlString];
return url;
}
// 网络视频
- (NSURL *)getNetworkUrl{
NSString *urlString = @"http://svideo.spriteapp.com/video/2016/0914/4cb325f6-7a09-11e6-a458-d4ae5296039d_wpd.mp4";
NSURL *url = [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
return url;
}
// 播放状态改变
-(void)mediaPlayerPlaybackStateChange:(NSNotification *)notification{
switch (self.playerViewController.moviePlayer.playbackState) {
case MPMoviePlaybackStatePlaying:
NSLog(@"正在播放");
break;
case MPMoviePlaybackStatePaused:
NSLog(@"暂停播放");
break;
case MPMoviePlaybackStateStopped:
NSLog(@"停止播放");
break;
case MPMoviePlaybackStateInterrupted:
NSLog(@"播放中断");
break;
case MPMoviePlaybackStateSeekingForward:
NSLog(@"向前");
break;
case MPMoviePlaybackStateSeekingBackward:
NSLog(@"向后");
break;
default:
break;
}
}
// 播放完成
-(void)mediaPlayerPlaybackFinished:(NSNotification *)notification{
NSLog(@"播放结束");
}
// 添加观察者监听视频状态
-(void)addNotification{
NSNotificationCenter *noti = [NSNotificationCenter defaultCenter];
[noti addObserver:self selector:@selector(mediaPlayerPlaybackStateChange:) name:MPMoviePlayerPlaybackStateDidChangeNotification object:self.playerViewController.moviePlayer];
[noti addObserver:self selector:@selector(mediaPlayerPlaybackFinished:) name:MPMoviePlayerPlaybackDidFinishNotification object:self.playerViewController.moviePlayer];
}
-(void)dealloc{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
@end
效果图如下:
播放控制器是在正常控制器上模态弹出的,点击播放控制器左上角的Done按钮可退出播放控制器。