视频播放2
2015-10-16 04:54 真实16 阅读(207) 评论(0) 编辑 收藏 举报//
// ViewController.m
// UI-NO.21视频
//
// Created by Bruce on 15/8/20.
// Copyright (c) 2015年 Bruce. All rights reserved.
//
/*
@property (nonatomic, copy) NSURL *contentURL 播放媒体URL,这个URL可以是本地路径,也可以是网络路径
@property (nonatomic, readonly) UIView *view 播放器视图,如果要显示视频必须将此视图添加到控制器视图中
@property (nonatomic, readonly) UIView *backgroundView 播放器背景视图
@property (nonatomic, readonly) MPMoviePlaybackState playbackState 媒体播放状态,枚举类型:
MPMoviePlaybackStateStopped:停止播放
MPMoviePlaybackStatePlaying:正在播放
MPMoviePlaybackStatePaused:暂停
MPMoviePlaybackStateInterrupted:中断
MPMoviePlaybackStateSeekingForward:向前定位
MPMoviePlaybackStateSeekingBackward:向后定位
@property (nonatomic, readonly) MPMovieLoadState loadState 网络媒体加载状态,枚举类型:
MPMovieLoadStateUnknown:位置类型
MPMovieLoadStatePlayable:
MPMovieLoadStatePlaythroughOK:这种状态如果shouldAutoPlay为YES将自动播放
MPMovieLoadStateStalled:停滞状态
@property (nonatomic) MPMovieControlStyle controlStyle 控制面板风格,枚举类型:
MPMovieControlStyleNone:无控制面板
MPMovieControlStyleEmbedded:嵌入视频风格
MPMovieControlStyleFullscreen:全屏
MPMovieControlStyleDefault:默认风格
@property (nonatomic) MPMovieRepeatMode repeatMode; 重复播放模式,枚举类型:
MPMovieRepeatModeNone:不重复,默认值
MPMovieRepeatModeOne:重复播放
@property (nonatomic) BOOL shouldAutoplay 当网络媒体缓存到一定数据时是否自动播放,默认为YES
@property (nonatomic, getter=isFullscreen) BOOL fullscreen 是否全屏展示,默认为NO,注意如果要通过此属性设置全屏必须在视图显示完成后设置,否则无效
@property (nonatomic) MPMovieScalingMode scalingMode 视频缩放填充模式,枚举类型:
MPMovieScalingModeNone:不进行任何缩放
MPMovieScalingModeAspectFit:固定缩放比例并且尽量全部展示视频,不会裁切视频
MPMovieScalingModeAspectFill:固定缩放比例并填充满整个视图展示,可能会裁切视频
MPMovieScalingModeFill:不固定缩放比例压缩填充整个视图,视频不会被裁切但是比例失衡
@property (nonatomic, readonly) BOOL readyForDisplay 是否有相关媒体被播放
@property (nonatomic, readonly) MPMovieMediaTypeMask movieMediaTypes 媒体类别,枚举类型:
MPMovieMediaTypeMaskNone:未知类型
MPMovieMediaTypeMaskVideo:视频
MPMovieMediaTypeMaskAudio:音频
@property (nonatomic) MPMovieSourceType movieSourceType 媒体源,枚举类型:
MPMovieSourceTypeUnknown:未知来源
MPMovieSourceTypeFile:本地文件
MPMovieSourceTypeStreaming:流媒体(直播或点播)
@property (nonatomic, readonly) NSTimeInterval duration 媒体时长,如果未知则返回0
@property (nonatomic, readonly) NSTimeInterval playableDuration 媒体可播放时长,主要用于表示网络媒体已下载视频时长
@property (nonatomic, readonly) CGSize naturalSize 视频实际尺寸,如果未知则返回CGSizeZero
@property (nonatomic) NSTimeInterval initialPlaybackTime 起始播放时间
@property (nonatomic) NSTimeInterval endPlaybackTime 终止播放时间
@property (nonatomic) BOOL allowsAirPlay 是否允许无线播放,默认为YES
@property (nonatomic, readonly, getter=isAirPlayVideoActive) BOOL airPlayVideoActive 当前媒体是否正在通过AirPlay播放
@property(nonatomic, readonly) BOOL isPreparedToPlay 是否准备好播放
@property(nonatomic) NSTimeInterval currentPlaybackTime 当前播放时间,单位:秒
@property(nonatomic) float currentPlaybackRate 当前播放速度,如果暂停则为0,正常速度为1.0,非0数据表示倍率
对象方法 说明
- (instancetype)initWithContentURL:(NSURL *)url 使用指定的URL初始化媒体播放控制器对象
- (void)setFullscreen:(BOOL)fullscreen animated:(BOOL)animated 设置视频全屏,注意如果要通过此方法设置全屏则必须在其视图显示之后设置,否则无效
- (void)requestThumbnailImagesAtTimes:(NSArray *)playbackTimes timeOption:(MPMovieTimeOption)option 获取在指定播放时间的视频缩略图,第一个参数是获取缩略图的时间点数组;第二个参数代表时间点精度,枚举类型:
MPMovieTimeOptionNearestKeyFrame:时间点附近
MPMovieTimeOptionExact:准确时间
- (void)cancelAllThumbnailImageRequests 取消所有缩略图获取请求
- (void)prepareToPlay 准备播放,加载视频数据到缓存,当调用play方法时如果没有准备好会自动调用此方法
- (void)play 开始播放
- (void)pause 暂停播放
- (void)stop 停止播放
- (void)beginSeekingForward 向前定位
- (void)beginSeekingBackward 向后定位
- (void)endSeeking 停止快进/快退
通知 说明
MPMoviePlayerScalingModeDidChangeNotification 视频缩放填充模式发生改变
MPMoviePlayerPlaybackDidFinishNotification 媒体播放完成或用户手动退出,具体完成原因可以通过通知userInfo中的key为MPMoviePlayerPlaybackDidFinishReasonUserInfoKey的对象获取
MPMoviePlayerPlaybackStateDidChangeNotification 播放状态改变,可配合playbakcState属性获取具体状态
MPMoviePlayerLoadStateDidChangeNotification 媒体网络加载状态改变
MPMoviePlayerNowPlayingMovieDidChangeNotification 当前播放的媒体内容发生改变
MPMoviePlayerWillEnterFullscreenNotification 将要进入全屏
MPMoviePlayerDidEnterFullscreenNotification 进入全屏后
MPMoviePlayerWillExitFullscreenNotification 将要退出全屏
MPMoviePlayerDidExitFullscreenNotification 退出全屏后
MPMoviePlayerIsAirPlayVideoActiveDidChangeNotification 当媒体开始通过AirPlay播放或者结束AirPlay播放
MPMoviePlayerReadyForDisplayDidChangeNotification 视频显示状态改变
MPMovieMediaTypesAvailableNotification 确定了媒体可用类型后
MPMovieSourceTypeAvailableNotification 确定了媒体来源后
MPMovieDurationAvailableNotification 确定了媒体播放时长后
MPMovieNaturalSizeAvailableNotification 确定了媒体的实际尺寸后
MPMoviePlayerThumbnailImageRequestDidFinishNotification 缩略图请求完成之后
MPMediaPlaybackIsPreparedToPlayDidChangeNotification 做好播放准备后
.mp4、.mov、.m4v、.m2v、.3gp、.3g2
视频播放使用<MediaPlayer/MediaPlayer.h>
不仅可以播放本地视频 也可以直接播放网络视频
MPMoviePlayerController 播放视频的类
*****
MPMoviePlayerController 上面播放视频的view 需要给它坐标
还需要添加在 某个视图上面
*/
#import "ViewController.h"
#import <MediaPlayer/MediaPlayer.h>
#import <AVFoundation/AVFoundation.h>
@interface ViewController ()
{
MPMoviePlayerController *mpviemController;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(back) name:MPMoviePlayerPlaybackStateDidChangeNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(finish) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
NSString *movpath =[[NSBundle mainBundle] pathForResource:@"IMG_8431" ofType:@"m4v"];
UIImage *image = [self getImage:[NSURL fileURLWithPath:movpath]];
self.view.backgroundColor = [UIColor colorWithPatternImage:image];
mpviemController =[[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:movpath]];
mpviemController.view.frame = self.view.frame;
mpviemController.scalingMode = MPMovieScalingModeAspectFit;
// MPMovieControlStyleFullscreen 全屏
// MPMovieControlStyleEmbedded 嵌入 设置尺寸多大 显示多大 默认
// MPMovieControlStyleNone 只显示视频 无工具栏
mpviemController.controlStyle = MPMovieControlStyleEmbedded;
// MPMovieRepeatModeNone, 不循环
// MPMovieRepeatModeOne 一直循环
mpviemController.repeatMode = MPMovieRepeatModeNone;
// 网络恢复的时候是否自动播放
mpviemController.shouldAutoplay = NO;
mpviemController.fullscreen = YES;
[self.view addSubview:mpviemController.view];
NSLog(@"媒体类型%ld",mpviemController.movieMediaTypes);
NSLog(@"媒体资源类型%ld",mpviemController.movieSourceType);
NSLog(@"总时长%f",mpviemController.duration);
NSLog(@"正常的尺寸width:%f height:%f",mpviemController.naturalSize.width,mpviemController.naturalSize.height);
// 是否允许使用AirPlay
mpviemController.allowsAirPlay = YES;
NSLog(@"是否使用了AirPlay%d",mpviemController.isAirPlayVideoActive);
[mpviemController play];
}
- (UIImage *)getImage:(NSURL *)url
{
// 通过URL 得到 视频文件资源
AVAsset *urlAsset = [AVAsset assetWithURL:url];
// AVURLAsset *urlAsset = [AVURLAsset URLAssetWithURL:url options:nil];//
// 初始化截图对象
AVAssetImageGenerator *generator = [AVAssetImageGenerator assetImageGeneratorWithAsset:urlAsset];
NSError *error = nil;
// 拷贝指定 时间 的截图
CGImageRef img = [generator copyCGImageAtTime:CMTimeMake(10, 10) actualTime:NULL error:&error];
UIImage *image = [UIImage imageWithCGImage: img];
return image;
}
- (void)back
{
NSLog(@"播放状态%ld",mpviemController.playbackState);
}
- (void)finish
{
NSLog(@"fffff");
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end