iOS9 视频播放器 AVPlayerViewController的简单使用

iOS9之后视频播放器废弃了MPMidMediuPlayerController,现在使用AVPlayerViewController也可实现视频播放的功能,而且在iPad上带有画中画的效果。

在使用前在plist文件中把网络的一个属性改一下,不然http的请求拿不到数据,具体操作可以看上一篇http://blog.csdn.net/cool_bear_xx/article/details/51452432


效果如图

       iOS9 视频播放器 AVPlayerViewController的简单使用_第1张图片



iOS9 视频播放器 AVPlayerViewController的简单使用_第2张图片



ViewController.m文件



#import "ViewController.h"

#import

#import



@interface ViewController ()

{


    AVPlayerViewController * avPlayer;


}


@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    

    

    UIButton * playVideoButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 40)];

    playVideoButton.center = self.view.center;

    playVideoButton.backgroundColor = [UIColor grayColor];

    [playVideoButton setTitle:@"点击播放" forState:UIControlStateNormal];

    [playVideoButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

    [playVideoButton addTarget:self action:@selector(playeVideo) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:playVideoButton];

    

}


- (void)playeVideo

{

    //http://v1.mukewang.com/57de8272-38a2-4cae-b734-ac55ab528aa8/L.mp4

    NSURL * videoURL = [NSURL URLWithString:@"http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4"];

    avPlayer = [[AVPlayerViewController alloc] init];

    avPlayer.player = [[AVPlayer alloc] initWithURL:videoURL];

    /*

     可以设置的值及意义如下:

     AVLayerVideoGravityResizeAspect   不进行比例缩放 以宽高中长的一边充满为基准

     AVLayerVideoGravityResizeAspectFill 不进行比例缩放 以宽高中短的一边充满为基准

     AVLayerVideoGravityResize     进行缩放充满屏幕

     */

    avPlayer.videoGravity = AVLayerVideoGravityResizeAspect;

    

    [self presentViewController:avPlayer animated:YES completion:nil];

}


@end


你可能感兴趣的:(IOS-UI)