今天给大家分享iOS中的视频播放,当然这个视频播放只是简单的系统的视频播放,只要有视频地址就可进行播放,但必须连网才行哦下面代码:
引入头文件
#import
//视频播放器
@property (strong, nonatomic) MPMoviePlayerController *player;
@property (strong, nonatomic) UIImageView *imageView;
//视频播放是流媒体的播放模式,所谓流媒体就是把视频数据像流水一样,变加载,变播放。
//提示:如果url中包含中文,需要添加百分号。
NSString *str = @"http://krtv.qiniudn.com/【氪TV视频】盖茨喝排泄物转化成的水,就从这台机器里出来 -- Janicki Omniprocessor.mp4";
NSURL *url = [NSURL URLWithString:[str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
self.player = [[MPMoviePlayerController alloc]initWithContentURL:url];
//1设置播放器的大小
[self.player.view setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.width/1.875)]; //16:9是主流媒体的样式
//2将播放器视图添加到根视图
[self.view addSubview:self.player.view];
//4播放
[self.player play];
//[self.player stop];
//通过通知中心,以观察者模式监听视频播放状态
//1 监听播放状态
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(stateChange) name:MPMoviePlayerPlaybackStateDidChangeNotification object:nil];
//2 监听播放完成
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(finishedPlay) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
//3视频截图
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(caputerImage:) name:MPMoviePlayerThumbnailImageRequestDidFinishNotification object:nil];
//4退出全屏通知
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(exitFullScreen) name:MPMoviePlayerDidExitFullscreenNotification object:nil];
//异步视频截图,可以在attimes指定一个或者多个时间。
// [self.player requestThumbnailImagesAtTimes:@[@100.0f, @200.0f] timeOption:MPMovieTimeOptionNearestKeyFrame];
UIImageView *thumbnailImageView = [[UIImageView alloc]initWithFrame:CGRectMake(80, 200, 160, 90)];
self.imageView = thumbnailImageView;
[self.view addSubview:thumbnailImageView];
下面的都是方法
#pragma mark 退出全屏
- (void)exitFullScreen
{
NSLog(@"退出全屏");
}
#pragma mark -播放器事件监听
#pragma mark 视频截图 这个方法是异步方法
- (void)caputerImage:(NSNotification *)notification
{
NSLog(@"截图 %@", notification);
UIImage *image = notification.userInfo[@"MPMoviePlayerThumbnailImageKey"];
[self.imageView setImage:image];
}
#pragma mark 播放器事件监听
#pragma mark 播放完成
- (void)finishedPlay
{
NSLog(@"播放完成");
}
#pragma mark 播放器视频的监听
#pragma mark 播放状态变化
/*
MPMoviePlaybackStateStopped, //停止
MPMoviePlaybackStatePlaying, //播放
MPMoviePlaybackStatePaused, //暂停
MPMoviePlaybackStateInterrupted, //中断
MPMoviePlaybackStateSeekingForward, //快进
MPMoviePlaybackStateSeekingBackward //快退
*/
- (void)stateChange
{
switch (self.player.playbackState) {
case MPMoviePlaybackStatePaused:
NSLog(@"暂停");
break;
case MPMoviePlaybackStatePlaying:
//设置全屏播放
[self.player setFullscreen:YES animated:YES];
NSLog(@"播放");
break;
case MPMoviePlaybackStateStopped:
//注意:正常播放完成,是不会触发MPMoviePlaybackStateStopped事件的。
//调用[self.player stop];方法可以触发此事件。
NSLog(@"停止");
break;
default:
break;
}
}