iOS用MPMoviePlayerViewController 播放MP4视频

iOS播放视频 

iOS sdk为播放视频提供了非常简便方法,提供的MPMoviePlayerViewController类作为开发使用,在iOS4以前的版本是MPMoviePlayerController。

在iPhone开发规范中禁止使用私有API播放视频,因此播放画面的控制的控件都是有iPhone提供好的,我们没有别的选择。我们能做的:

  加载URL中视频

  播放、暂停视频

  用户控制行为和缩放模式

  产生通知

MPMoviePlayerController还可以播放互联网上的视频文件。但是建议你先将视频文件下载到本地,然后播放。如果你不这样做,iOS可能会拒绝播放很大的视频文件。

 这个类定义在MediaPlayer framework中。在你的应用程序中,先添加这个引用,然后修改MediaPlayerViewController.h文件。

ViewController.h

#import <UIKit/UIKit.h>

#import <MediaPlayer/MediaPlayer.h>

@interface ViewController : UIViewController

{

    MPMoviePlayerViewController *moviePlayerView;

}

- (IBAction)playMovies:(id)sender;

-(void)playingDone;

@end


ViewController.m

#import "ViewController.h"

@interface ViewController ()

@end


@implementation ViewController

- (void)viewDidLoad

{

    [super viewDidLoad];

    //初始化通知

    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(playingDone) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];

}

- (IBAction)playMovies:(id)sender {

        //初始化播放器并且设定播放模式

    moviePlayerView = [[MPMoviePlayerViewController alloc]initWithContentURL:[NSURL fileURLWithPath:      [[NSBundle mainBundle]pathForResource:@"po" ofType:@"mp4"]]];

    //设定播放模式

    moviePlayerView.moviePlayer.controlStyle = MPMovieControlStyleFullscreen;

    //控制模式(触摸)

    moviePlayerView.moviePlayer.scalingMode = MPMovieScalingModeAspectFill;

    // MPMovieControlStyleNone

    //MPMovieControlStyleEmbedded

    //MPMovieControlStyleDefault

    

     //开始播放

    [[[UIApplication sharedApplication]keyWindow]addSubview:moviePlayerView.view];

    /*视频文件可以播放资源目录、沙箱目录和网络播放。本例中我们采用资源目录。

     

     moviePlayerView.moviePlayer属性是MPMoviePlayerController类型,它有的controlStyle属性     

     可以控制播放行为,它的取值有:

          MPMovieControlStyleFullscreen

          MPMovieControlStyleNone没有播放控件

          MPMovieControlStyleEmbedded

         MPMovieControlStyleDefault

         MPMoviePlayerController类还有scalingMode属性用于控制影片的尺寸,它的取值有:

         MPMovieScalingModeNone原始尺寸

          MPMovieScalingModeAspectFit缩放到一个填充方向

          MPMovieScalingModeAspectFill填充两边可能会切除一部分

          MPMovieScalingModeFill填充两边可能会改变比例

     */}


-(void)playingDone

{

    NSLog(@"播放完成");

    [moviePlayerView.view removeFromSuperview];

    moviePlayerView = nil;

}


-(void)dealloc

{

    //移除通知

    [[NSNotificationCenter defaultCenter]removeObserver:self];

}

@end


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值