AVPlayerLayer播放网络音视频

#import "ViewController.h"
#import 

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UISegmentedControl *segmentControl;
//播放对象
@property (nonatomic, strong) AVPlayer *player;
//视频再声明层属性
@property (nonatomic, strong) AVPlayerLayer *videoLayer;
@end

@implementation ViewController

- (IBAction)play:(id)sender {
    //音频地址
    NSString *audioPath = @"http://fdfs.xmcdn.com/group1/M00/01/3B/wKgDrVCYca7Sf6VzADfjEnQrWdU600.mp3";
    //视频地址
    NSString *videoPath = @"http://flv2.bn.netease.com/videolib3/1510/25/bIHxK3719/SD/bIHxK3719-mobile.mp4";
    if (self.segmentControl.selectedSegmentIndex == 0) {
        //视频播放
        //1.创建播放对象,并赋值
        self.player = [AVPlayer playerWithURL:[NSURL URLWithString:videoPath]];
        //2.创建layer对象,指定layer的frame
        self.videoLayer = [AVPlayerLayer playerLayerWithPlayer:self.player];
        self.videoLayer.frame = CGRectMake(0, 150, 300, 250);
        //3.把layer添加到view.layer
        [self.view.layer addSublayer:self.videoLayer];
    } else {
        //音频
        self.player = [AVPlayer playerWithURL:[NSURL URLWithString:audioPath]];
    }
    //执行播放动作(音频或者视频)
    [self.player play];
}

- (IBAction)pause:(id)sender {
    [self.player pause];
}
//继续播放
- (IBAction)resume:(id)sender {
    [self.player play];
}

@end

你可能感兴趣的:(AVPlayerLayer播放网络音视频)