如何使用AVPlayer快速播放youtube视频?

时间:2023-01-23 22:52:54

How to play a youtube video in swift using AVPlayer?

如何使用AVPlayer快速播放youtube视频?

I am using the below code to play a youtube video but nothing happens in avplayer. It remains blank.

我正在使用下面的代码播放一个youtube视频,但是在avplayer中什么都没有发生。仍然是空白。

var urlstring = "https://www.youtube.com/watch?v=hMba_c-VSkI"
var url = NSURL(string: urlstring)
var av:AVPlayerViewController = segue.destinationViewController as AVPlayerViewController
av.player = AVPlayer(URL: url)

3 个解决方案

#1


2  

The url you are using is not the url to the video, it is the url to a webpage which has the video embedded. There are tools out there that can help you get the actual url of the video file.

你使用的url不是视频的url,而是嵌入视频的网页的url。有一些工具可以帮助您获得视频文件的实际url。

#2


1  

AVPlayer only plays movie files, and YouTube videos aren't directly exposed as movie files at their URL. You can handle youtube videos in your own WebView.

AVPlayer只播放电影文件,YouTube视频不会在其URL上直接显示为电影文件。你可以在自己的网站上播放youtube视频。

You can use IFrame Player API.

您可以使用IFrame Player API。

#3


0  

You will need to use a like YoutubeParser to get it to work. But please note this will go against Youtube's T&C

您将需要使用like YoutubeParser来让它工作。但请注意,这将与Youtube的T&C背道而驰。

#1


2  

The url you are using is not the url to the video, it is the url to a webpage which has the video embedded. There are tools out there that can help you get the actual url of the video file.

你使用的url不是视频的url,而是嵌入视频的网页的url。有一些工具可以帮助您获得视频文件的实际url。

#2


1  

AVPlayer only plays movie files, and YouTube videos aren't directly exposed as movie files at their URL. You can handle youtube videos in your own WebView.

AVPlayer只播放电影文件,YouTube视频不会在其URL上直接显示为电影文件。你可以在自己的网站上播放youtube视频。

You can use IFrame Player API.

您可以使用IFrame Player API。

#3


0  

You will need to use a like YoutubeParser to get it to work. But please note this will go against Youtube's T&C

您将需要使用like YoutubeParser来让它工作。但请注意,这将与Youtube的T&C背道而驰。