IOS AVPlayer 不会播放视频

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16509142/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 23:33:02  来源:igfitidea点击:

IOS AVPlayer won't play video

iosvideoios6avplayer

提问by Ashraf Hussein

I'm trying to play a video usingso I got some code from a tutorial but it won't work.

我正在尝试使用播放视频,所以我从教程中获得了一些代码,但它不起作用。

Here's the code I use :

这是我使用的代码:

- (void) viewDidAppear:(BOOL)animated
{
    avAsset = [AVAsset assetWithURL:[NSURL URLWithString:filePath]];
    avPlayerItem =[avPlayerItem initWithAsset:avAsset];
    avPlayer = [AVPlayer playerWithPlayerItem:avPlayerItem];
    avPlayerLayer = [AVPlayerLayer playerLayerWithPlayer:avPlayer];
    [avPlayerLayer setFrame:self.view.frame];
    [self.view.layer addSublayer:avPlayerLayer];
    [avPlayer seekToTime:kCMTimeZero];
    [avPlayer play];

}

I don't know what's wrong it doesn't give ny errors it just gives black screen. Thanks.

我不知道出了什么问题,它没有给出任何错误,只是给出了黑屏。谢谢。

回答by Ashraf Hussein

Thanks s1mon and matt for your help.

感谢 s1mon 和 matt 的帮助。

Here's the new code it might help someone:

这是可能对某人有所帮助的新代码:

- (void) viewDidAppear:(BOOL)animated
{
    avAsset = [AVAsset assetWithURL:[NSURL fileURLWithPath:filePath]];
    avPlayerItem =[[AVPlayerItem alloc]initWithAsset:avAsset];
    avPlayer = [[AVPlayer alloc]initWithPlayerItem:avPlayerItem];
    avPlayerLayer =[AVPlayerLayer playerLayerWithPlayer:avPlayer];
    [avPlayerLayer setFrame:self.view.frame];
    [self.view.layer addSublayer:avPlayerLayer];
    //[avPlayerLayer setBackgroundColor:[[UIColor redColor]CGColor]];
    [avPlayer seekToTime:kCMTimeZero];
    [avPlayer play];

}

Thank you.

谢谢你。

回答by matt

This might not be the actual problem, but this code is certainly wrong:

这可能不是实际问题,但这段代码肯定是错误的:

[NSURL URLWithString:filePath]

First of all, try not to start with a file path; start with a file URL. That way, there is no conversion needed. But if you mustperform a conversion from a file path to a URL, use fileURLWithPath:isDirectory:. A file URL is a special kind of animal and you must ask for one specifically when you are converting from a path.

首先,尽量不要以文件路径开头;以文件 URL 开头。这样,就不需要转换。但是,如果您必须执行从文件路径到 URL 的转换,请使用fileURLWithPath:isDirectory:. 文件 URL 是一种特殊的动物,当您从路径转换时,您必须特别要求一个。

回答by s1m0n

I think you forgot to create an instance of AVPlayerItemin this line:

我想你忘了AVPlayerItem在这一行中创建一个实例:

avPlayerItem =[avPlayerItem initWithAsset:avAsset];

This just sends initWithAsset:to avPlayerItem(which is probably nil when this method is called). Try changing it to instantiate an new AVPlayerItemwhich you can then assign to avPlayerItem, like this:

这只是发送initWithAsset:avPlayerItem(调用此方法时可能为零)。尝试更改它以实例化一个 new AVPlayerItem,然后您可以将其分配给avPlayerItem,如下所示:

avPlayerItem = [[AVPlayerItem alloc] initWithAsset:avAsset];

回答by kemicofa ghost

For those looking for a swift 2.0 version, here is what worked for me. Cheers.

对于那些寻找swift 2.0 version,这对我有用。干杯。

import Foundation
import UIKit
import AVKit

class VideoViewController: AVPlayerViewController {

    override func viewDidAppear(animated: Bool) {
        self.setupVideoPlayerView()
    }
    private func setupVideoPlayerView()
    {
        let path = "HTTP_VIDEO_URL_HERE"
        let nsURL = NSURL(string: path)
        let avAsset = AVAsset(URL: nsURL!)
        let avPlayerItem = AVPlayerItem(asset: avAsset)

        let avPlayer = AVPlayer(playerItem: avPlayerItem)
        let avPlayerLayer = AVPlayerLayer(layer: avPlayer)

        avPlayerLayer.frame = self.view.frame
        self.view.layer.addSublayer(avPlayerLayer)

        self.player = avPlayer

        self.player!.seekToTime(kCMTimeZero)
        self.player!.play()

    }

}