AVPlayer与AVPlayerLayer不播放视频

时间:2017-05-19 09:17:45

标签: ios iphone avplayer

我有一个名为Intro.m4v的视频文件,该视频文件在我的应用目标上具有目标会员资格(视频本身直接来自iPhone,所以我很确定它的iPhone - 支持)。

我尝试按如下方式播放视频(self.videoContainer是我的视图控制器内的全屏视图,并经过双重检查,它不是nil。此外,播放器和播放器层是我的视图控制器类中强烈引用的实例变量):

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    player = [AVPlayer playerWithURL:[NSURL fileURLWithPath:@"Intro.m4v"]];
    playerLayer = [AVPlayerLayer playerLayerWithPlayer:player];
    playerLayer.frame = self.videoContainer.layer.frame;
    [self.videoContainer.layer addSublayer:playerLayer];

}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [player play];
}

-(void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
    playerLayer.frame = self.videoContainer.layer.frame;
}

但是,什么都没有播放。我所看到的只是我的视频容器视图的背景颜色(以及那种方式前面的所有视图)。我做错了什么?

3 个答案:

答案 0 :(得分:2)

如果您的视频是捆绑在一起,您的网址应该是这样的:

NSString *path = [[NSBundle mainBundle] pathForResource:@"Intro" ofType:@"m4v"];
NSURL *yourVideoUrl = [NSURL fileURLWithPath:path];

答案 1 :(得分:1)

哎呀自己发现了这个问题。我使用[NSURL fileURLWithPath:@"Intro.m4v"]而我必须使用[NSBundle mainBundle] URLForResource:@"Intro" withExtension:@"m4v"]

修复网址解决了问题。

答案 2 :(得分:-1)

试试这个

func playVideo(urlString: String, location : Int) {        
        let videoURL = URL(string: urlString)
        let player = AVPlayer(url: videoURL!)
        let playerViewController = AVPlayerViewController()
        playerViewController.player = player
        self.present(playerViewController, animated: true) {
            playerViewController.player!.play()
        }

    }
相关问题