本文主要是介绍iOS 加载网络视频之(AVFoundation.framewor)的MPMoviePlayerController,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
//引用 AVFoundation.framework
#import "FirstViewController.h"
#import <MediaPlayer/MediaPlayer.h>
@interfaceFirstViewController ()
//视频播放器
@property (strong,nonatomic) MPMoviePlayerController *player;
@property (strong,nonatomic) UIImageView *imageView;
@end
@implementation FirstViewController
- (void)viewDidLoad {
[superviewDidLoad];
}
- (void)viewWillAppear:(BOOL)animated {
}
- (IBAction)media:(UIButton *)sender {
// 本地视频
// NSURL *url = [[NSBundle mainBundle]URLForResource:@"1" withExtension:@"mp4"];
// url = [NSURL URLWithString:@"http://devimages.apple.com/iphone/samples/bipbop/gear1/prog_index.m3u8"];
// 网络视频
// 提示:如果url中包含中文,需要添加百分号。
NSString *urlString =@"http://devimages.apple.com/iphone/samples/bipbop/gear1/prog_index.m3u8";
NSURL *url = [NSURLURLWithString:[urlStringstringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
//实例化视频播放器
self.player = [[MPMoviePlayerControlleralloc]initWithContentURL:url];
//1设置播放器的大小
[self.player.viewsetFrame:CGRectMake(0,200, self.view.frame.size.width,180)]; //16:9是主流媒体的样式
//2将播放器视图添加到根视图
[self.viewaddSubview:self.player.view];
//异步视频截图,可以在attimes指定一个或者多个时间。
[self.playerrequestThumbnailImagesAtTimes:@[@10.0f,@20.0f] timeOption:MPMovieTimeOptionNearestKeyFrame];
// UIImageView *thumbnailImageView = [[UIImageView alloc]initWithFrame:CGRectMake(80, 200, 160, 90)];
// self.imageView = thumbnailImageView;
// [self.view addSubview:thumbnailImageView];
//视频播放是流媒体的播放模式,所谓流媒体就是把视频数据像流水一样,边加载,边播放。
//4播放
// [self.player play];
//暂停
// [self.player stop];
//通过通知中心,以观察者模式监听视频播放状态
//1 监听播放状态
[[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(stateChange)name:MPMoviePlayerPlaybackStateDidChangeNotificationobject:nil];
//2 监听播放完成
[[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(finishedPlay)name:MPMoviePlayerPlaybackDidFinishNotificationobject:nil];
//3视频截图
[[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(caputerImage:)name:MPMoviePlayerThumbnailImageRequestDidFinishNotificationobject:nil];
//3视频截图
[[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(caputerImage:)name:MPMoviePlayerThumbnailImageRequestDidFinishNotificationobject:nil];
//4退出全屏通知
[[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(exitFullScreen)name:MPMoviePlayerDidExitFullscreenNotificationobject:nil];
}
#pragma mark 退出全屏
- (void)exitFullScreen {
NSLog(@"退出全屏");
}
#pragma mark -播放器事件监听
#pragma mark 视频截图这个方法是异步方法
- (void)caputerImage:(NSNotification *)notification {
NSLog(@"截图 %@", notification);
UIImage *image = notification.userInfo[@"MPMoviePlayerThumbnailImageKey"];
[self.imageViewsetImage: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.playersetFullscreen:YESanimated:YES];
NSLog(@"播放");
break;
case MPMoviePlaybackStateStopped:
//注意:正常播放完成,是不会触发MPMoviePlaybackStateStopped事件的。
//调用[self.player stop];方法可以触发此事件。
NSLog(@"停止");
break;
default:
break;
}
}
@end
这篇关于iOS 加载网络视频之(AVFoundation.framewor)的MPMoviePlayerController的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!