视频播放器

首先  添加MediaPlayer.framework 

在 ViewController.h 中导入 #import  <MediaPlayer/MediaPlayer.h>;

并在.h中建立属性  @property  ( nonatomic strong MPMoviePlayerController  *movie;

以上就是在开始和在.h中做的准备工作


接下来是.m中的代码 

我们首先定义三个按钮 一个是播放视频按钮 一个是暂停/停止按钮 一个是控制屏幕大小的按钮

或者还有一些其他的按钮  大家可以自己开发,我也验证过;

  按钮:

在 viewDidLoad中
UIButton  *movie = [ UIButton   buttonWithType : UIButtonTypeCustom ];
    movie. backgroundColor  = [ UIColor   colorWithRed : 0 / 255.0   green : 0 / 255.0   blue : 0 / 255.0   alpha : 0.5 ];
    [movie 
setTitle : @"MPMoviePlayerController"   forState : UIControlStateNormal ];
    [movie 
setTitleColor :[ UIColor   whiteColor forState : UIControlStateNormal ];
    movie.
frame  =  CGRectMake ( 50 100 280 50 );
    [movie 
addTarget : self   action : @selector (playMovie:)  forControlEvents : UIControlEventTouchUpInside ];
    movie.
layer . masksToBounds  =  YES ;
    movie.
layer . cornerRadius  =  10 ;
    movie.
layer . borderWidth  =  1 ;
    [
self . view   addSubview :movie];
    
//  停止按钮
    
UIButton  *stop = [ UIButton   buttonWithType : UIButtonTypeCustom ];
    stop.
backgroundColor  = [ UIColor   colorWithRed : 0 / 255.0   green : 0 / 255.0   blue : 0 / 255.0   alpha : 0.5 ];
    [stop 
setTitle : @" 停止 "   forState : UIControlStateNormal ];
    [stop 
setTitleColor :[ UIColor   whiteColor forState : UIControlStateNormal ];
    stop.
frame  =  CGRectMake ( 50 500 280 50 );
    [stop 
addTarget : self   action : @selector (stop)  forControlEvents : UIControlEventTouchUpInside ];
    stop.
layer . masksToBounds  =  YES ;
    stop.
layer . cornerRadius  =  10 ;
    stop.
layer . borderWidth  =  1 ;
    [
self . view   addSubview :stop];
    
//  横屏播放
    
UIButton  *heng = [ UIButton   buttonWithType : UIButtonTypeCustom ];
    heng.
backgroundColor  = [ UIColor   colorWithRed : 0 / 255.0   green : 0 / 255.0   blue : 0 / 255.0   alpha : 0.5 ];
    [heng 
setTitle : @" 横屏 "   forState : UIControlStateNormal ];
    [heng 
setTitleColor :[ UIColor   whiteColor forState : UIControlStateNormal ];
    heng.
frame  =  CGRectMake ( 50 600 280 50 );
    [heng 
addTarget : self   action : @selector (setupMovie)  forControlEvents : UIControlEventTouchUpInside ];
    heng.
layer . masksToBounds  =  YES ;
    heng.
layer . cornerRadius  =  10 ;
    heng.
layer . borderWidth  =  1 ;
    [
self . view   addSubview :heng];

三个按钮创建好了

接下来就是要实现视频播放这一功能

上面三个按钮  自带三个方法 想必大家都知道

一 丶 
我们就能在播放按钮中实现他的播放功能 


#warning  很重要的一点是在头文件里已经把 player 变为属性了, @property nonamaic,strong ),如果不写为属性就会黑屏;

1. 视频文件路径

播放本地
NSString *path = [[NSBundle mainBundle] pathForResource:@" 程序上传 -- 最新 " ofType:@"mp4"];
    
//  视频 URL
//    NSURL *url = [NSURL fileURLWithPath:path];
播放网络视频(就是给他一个视频网址再将网址转化为url)


1.视频播放对象
self . movie  = [[ MPMoviePlayerController   alloc initWithContentURL :url];

2.播放器样式
MPMovieControlStyleFullscreen 显示播放/暂停丶音量和时间控制
MPMovieControlStyleEmbedded 只显示音量控制
MPMovieControlStyleNone 没有控制器
self . movie . controlStyle  =  MPMovieControlStyleFullscreen ;
3.播放器大小
self . movie . view . frame  =  CGRectMake ( 20 200 335 200 );
(self.movie.view.frame = CGRectMake(20,200,335,200);)
4.
     //  宽高比值
    
// MPMovieScalingModeNone             不做任何缩放

    
// MPMovieScalingModeAspectFit        适应屏幕大小,保持宽高比
    
// MPMovieScalingModeAspectFill       适应屏幕大小,保持宽高比,可裁剪
    
// MPMovieScalingModeFill             充满屏幕,不保持宽高比
    
self . movie . scalingMode  =  MPMovieScalingModeAspectFit ;

[ self . view   addSubview : self . movie . view ];


//     你的程序可以配置电影播放器在何时候发送通知,包括结束加载内容、技术播放、改变宽高比等。电影播放器会将事件发送到  Cocoa  的通知中心,你可以对其进行配置,指定将这些事件转发到你的应用程序的一个对象。要接收这些通知,需要使用  NSNotificationCenter  类,为电影播放器添加一个观察者 (observer): 
    
    
//  注册一个播放结束的通知
    [[
NSNotificationCenter   defaultCenter addObserver : self
                                             
selector : @selector (myMovieFinishedCallback:)
                                                 
name : MPMoviePlayerPlaybackDidFinishNotification
                                               
object : self . movie ];
    [
self . movie   play ];
    
    
}
// 在注册观察者模式中的函数实现
-( void )myMovieFinishedCallback:( NSNotification *)notify
{
    
// 视频播放对象
    
MPMoviePlayerController * theMovie = [notify  object ];
    
// 销毁播放通知
    [[
NSNotificationCenter   defaultCenter removeObserver : self
                                                    
name : MPMoviePlayerPlaybackDidFinishNotification
                                                  
object :theMovie];
    [theMovie.
view   removeFromSuperview ];
    
NSLog ( @" 自动播放下一个 " );

}
在停止/暂停按钮中
//  视频停止
- (
void )stop
{
    [
self . movie   stop ];
}
控制横屏的按钮中
//  横屏播放
-(
void )setupMovie
{
    
CGRect  aFrame = [[ UIScreen   mainScreen applicationFrame ];
    aFrame = 
CGRectMake ( 0.0f 0.0f , aFrame. size . width , aFrame. size . height );
    
self . movie . controlStyle  =  MPMovieControlStyleDefault ;
    
self . movie . view . frame  = aFrame;
    
self . movie . view . center  =  CGPointMake (aFrame. size . width / 2 , aFrame. size . height / 2 );
    
CGAffineTransform  transform =  CGAffineTransformMakeRotation ( M_PI / 2 );
    [
self . movie . view   setTransform :transform];
    [[[[
UIApplication   sharedApplication delegate window addSubview : self . movie . view ];
    
}


#pragma mark ------------------- 视频播放结束委托 --------------------

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值