gpt4 book ai didi

ios - 尝试播放 url 视频文件时,Swift AVPlayer 不断加载

转载 作者:行者123 更新时间:2023-11-28 11:45:26 25 4
gpt4 key购买 nike

我正在尝试从 YouTube 网址播放视频文件。我已经创建了 AVPlayer 并将 url 链接到播放器。当我单击按钮时播放器打开,但视频播放器只显示文件不断加载。我已经更改了 App Transport Security Settings -> Allow Arbitrary Loads on the plist。

代码如下:

@IBAction func playVideo(_ sender: Any) {


let movieId = "xLCn88bfW1o"
let path = URL(string: "https://www.youtube.com/watch?v=\(movieId)")
let video = AVPlayer(url: path!)
let videoPlayer = AVPlayerViewController()
videoPlayer.player = video

present(videoPlayer, animated: true, completion: {
video.play()
})

}

Loading Video Player

编辑:我也尝试过使用 URL 来分享视频; https://youtu.be/xLCn88bfW1o .

最佳答案

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

例如: YoutubeDirectLinkExtractor

YoutubeDirectLinkExtractor allows you to obtain the direct link to a YouTube video, which you can easily use with AVPlayer. It uses type safety and optionals to guarantee that you won't crash while extracting the link no matter what. There are popular alternatives, which use more straightforward and risky approach, though:

使用 AVPlayer 提取的视频链接:

let y = YoutubeDirectLinkExtractor()
y.extractInfo(for: .urlString("https://www.youtube.com/watch?v=HsQvAnCGxzY"), success: { info in
let player = AVPlayer(url: URL(string: info.highestQualityPlayableLink!)!)
let playerViewController = AVPlayerViewController()
playerViewController.player = player

self.present(playerViewController, animated: true) {
playerViewController.player!.play()
}
}) { error in
print(error)
}

关于ios - 尝试播放 url 视频文件时,Swift AVPlayer 不断加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52674853/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com