视频播放源码
#import "MainViewController.h"
#import <MediaPlayer/MediaPlayer.h>
@interfaceMainViewController ()
// 视频播放器
@property (strong, nonatomic) MPMoviePlayerController *player;
@property (strong, nonatomic) UIImageView *imageView;
@end
@implementation MainViewController
- (void)viewDidLoad
{
[superviewDidLoad];
// 实例化视频播放器
// NSURL *url = [[NSBundle mainBundle]URLForResource:@"promo_full" withExtension:@"mp4"];
// 视频播放是流媒体的播放模式,所谓流媒体,就是把视频数据像流水一样,边加载,边播放!
// 提示,url中如果包含中文,需要添加百分号
NSString *urlString = @"http://localhost/~apple/itcast/videos/01.C语言-语法预览.mp4";
NSURL *url = [NSURLURLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
self.player = [[MPMoviePlayerControlleralloc]initWithContentURL:url];
// 1. 设置播放器的大小(16:9)
[self.player.view setFrame:CGRectMake(0, 0, 320, 180)];
// 2. 将播放器视图添加到根视图
[self.view addSubview:self.player.view];
// 3. 播放
[self.player play];
// 通过通知中心,以观察者模式监听视频播放状态
// 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];
// 4) 退出全屏通知
[[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(exitFullScreen) name:MPMoviePlayerDidExitFullscreenNotificationobject:nil];
// 异步的视频截图,可以在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];
}
#pragma mark - 播放器事件监听
#pragma mark 退出全屏
- (void)exitFullScreen
{
NSLog(@"退出全屏");
}
#pragma mark 视频截图
- (void)caputerImage:(NSNotification *)notification
{
NSLog(@"截图 %@", notification);
UIImage *image = notification.userInfo[@"MPMoviePlayerThumbnailImageKey"];
[self.imageView setImage:image];
}
#pragma mark 播放完成
- (void)finishedPlay
{
NSLog(@"播放完成");
}
#pragma mark 播放状态变化
/*
MPMoviePlaybackStateStopped, 停止
MPMoviePlaybackStatePlaying, 播放
MPMoviePlaybackStatePaused, 暂停
MPMoviePlaybackStateInterrupted, 中断
MPMoviePlaybackStateSeekingForward, 快进
MPMoviePlaybackStateSeekingBackward 快退
*/
- (void)stateChange
{
switch (self.player.playbackState) {
caseMPMoviePlaybackStatePaused:
NSLog(@"暂停");
break;
caseMPMoviePlaybackStatePlaying:
// 设置全屏播放
[self.playersetFullscreen:YESanimated:YES];
NSLog(@"播放");
break;
caseMPMoviePlaybackStateStopped:
// 注意:正常播放完成,是不会触发MPMoviePlaybackStateStopped事件
// 调用[self.player stop]方法可以触发此事件
NSLog(@"停止");
break;
default:
break;
}
}
@end