iOS MPMoviePlayerController 播放在线视频

 

在iOS中播放视频可以使用MediaPlayer.framework种的MPMoviePlayerController类来完成,它支持本地视频和网络视频播放。这个类实现了MPMediaPlayback协议,因此具备一般的播放器控制功能,例如播放、暂停、停止等。下面列出了MPMoviePlayerController的常用属性和方法:

属性 说明
@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 做好播放准备后

二 使用方法:设置contentUrl ,view的frame,屏幕模式

 

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# import ViewController.h
# import <mediaplayer mediaplayer.h= "" >
 
@interface ViewController ()
 
@property (nonatomic,strong) MPMoviePlayerController *moviePlayer;
 
@end
 
@implementation ViewController
 
- ( void )viewDidLoad {
     [ super viewDidLoad];
     // Do any additional setup after loading the view, typically from a nib.
     
     [self.moviePlayer play];
     [self addNofi];
}
 
- ( void )didReceiveMemoryWarning {
     [ super didReceiveMemoryWarning];
     // Dispose of any resources that can be recreated.
}
 
/**
  *  视频播放url
  *
  *  @return 返回视频url
  */
- (NSURL *)url
{
     NSString *urlStr = @http : //7xawdc.com2.z0.glb.qiniucdn.com/o_19p6vdmi9148s16fs1ptehbm1vd59.mp4;
     urlStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
     NSURL *url1 = [NSURL URLWithString:urlStr];
     return url1;
}
 
/**
  *  初始化MPMoviePlayerController
  *
  *  @return 返回一个MPMoviePlayerController的实例
  */
- (MPMoviePlayerController *)moviePlayer
{
     if (!_moviePlayer) {
         _moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[self url]];
         _moviePlayer.view.frame = self.view.frame;
         _moviePlayer.scalingMode = MPMovieScalingModeAspectFit; //固定缩放比例并且尽量全部
         [self.view addSubview:_moviePlayer.view];
     }
     return _moviePlayer;
}
 
/**
  *  添加视频播放的通知
  */
- ( void )addNofi
{
     [[NSNotificationCenter defaultCenter] addObserver:self
                                              selector: @selector (playbackstateDidChange:)
                                                  name:MPMoviePlayerPlaybackStateDidChangeNotification   //播放状态改变,可配合playbakcState属性获取具体状态
                                                object:nil];
     [[NSNotificationCenter defaultCenter] addObserver:self
                                              selector: @selector (playDidFinish:) //媒体播放完成或用户手动退出,具体完成原因可以通过通知userInfo中的key为MPMoviePlayerPlaybackDidFinishReasonUserInfoKey的对象获取
                                                  name:MPMoviePlayerPlaybackDidFinishNotification
                                                object:nil];
}
 
 
- ( void )playbackstateDidChange:(NSNotification *)noti
{
     switch (self.moviePlayer.playbackState) {
         case MPMoviePlaybackStateInterrupted:
             //中断
             NSLog(@中断);
             break ;
         case MPMoviePlaybackStatePaused:
             //暂停
             NSLog(@暂停);
             break ;
         case MPMoviePlaybackStatePlaying:
             //播放中
             NSLog(@播放中);
             break ;
         case MPMoviePlaybackStateSeekingBackward:
             //后退
             NSLog(@后退);
             break ;
         case MPMoviePlaybackStateSeekingForward:
             //快进
             NSLog(@快进);
             break ;
         case MPMoviePlaybackStateStopped:
             //停止
             NSLog(@停止);
             break ;
             
         default :
             break ;
     }
}
 
- ( void )playDidFinish:(NSNotification *)noti
{
     //播放完成
}
 
@end

你可能感兴趣的:(ios,在线视频)