无法使用 UIWebView 自动播放嵌入式 YouTube 视频吗?
Is it not possible to autoplay embedded YouTube videos using UIWebView?
我在尝试自动播放我的 UIWebView 时遇到问题。
是的,我知道已经有很多关于它的问题了。
加油,这是我的简单代码:
override func viewDidLoad()
{
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let youtubePlayer:UIWebView = UIWebView(frame: CGRectMake(0, 0, UIScreen.mainScreen().bounds.width, UIScreen.mainScreen().bounds.height))
youtubePlayer.loadRequest(NSURLRequest(URL: NSURL(string: "https://www.youtube.com/embed/EzNinU0g-Q0")!) )
self.view.addSubview(youtubePlayer)
}
下面是我的发现:
- How to autoplay a YouTube video in a UIWebView
- Youtube video autoplay inside UIWebview
- How to get YouTube autoplay to work in UIWebView?
我按照以下代码的建议做了:
youtubePlayer.mediaPlaybackRequiresUserAction = false
youtubePlayer.allowsInlineMediaPlayback = true
那什么也没做。
此解决方案有效,但不适用于我正在使用的嵌入式视频。
所以,有没有办法使用UIWebView自动播放?
这些问题是几年前发布的,所以我不确定情况发生了怎样的变化。
任何人都可以提出替代方案吗?由于我有一个 UITableView
设置到哪里,当用户选择一个单元格(视频)时,它将显示视频的 UIWebView。
但是,如果可能的话,我希望在用户选择视频时立即开始播放视频,而不必再次按下播放键来播放。
我的布局设计与 TubiMusic 和 Tubex 等应用程序类似,但这些应用程序能够自动播放视频,但我不知道如何执行相同的操作。
如有任何帮助,我们将不胜感激。谢谢
自动播放参数在网络视图中不起作用。相反,您必须使用 JS api 来确定播放器的就绪状态,然后向其发送播放命令。
你可以使用这个pod
https://github.com/gilesvangruisen/Swift-YouTube-Player
或者您可以使用 google 提供的代码
https://developers.google.com/youtube/v3/guides/ios_youtube_helper
这是对 How to autoplay a YouTube video in a UIWebView 的最高评价 obj-c 答案的 Swift 化版本。
Apparently the problem was with the nil base url, when I changed it to resourceURL the autoplay worked.
代码如下:
var youTubeVideoHTML: String = "<!DOCTYPE html><html><head><style>body{margin:0px 0px 0px 0px;}</style></head> <body> <div id=\"player\"></div> <script> var tag = document.createElement('script'); tag.src = \"http://www.youtube.com/player_api\"; var firstScriptTag = document.getElementsByTagName('script')[0]; firstScriptTag.parentNode.insertBefore(tag, firstScriptTag); var player; function onYouTubePlayerAPIReady() { player = new YT.Player('player', { width:'%0.0f', height:'%0.0f', videoId:'%@', events: { 'onReady': onPlayerReady, } }); } function onPlayerReady(event) { event.target.playVideo(); } </script> </body> </html>"
func playVideoWithId(videoId: String) {
var html: String = String(format: youTubeVideoHTML, self.frame.size.width, self.frame.size.height, videoId)
}
我在尝试自动播放我的 UIWebView 时遇到问题。
是的,我知道已经有很多关于它的问题了。
加油,这是我的简单代码:
override func viewDidLoad()
{
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let youtubePlayer:UIWebView = UIWebView(frame: CGRectMake(0, 0, UIScreen.mainScreen().bounds.width, UIScreen.mainScreen().bounds.height))
youtubePlayer.loadRequest(NSURLRequest(URL: NSURL(string: "https://www.youtube.com/embed/EzNinU0g-Q0")!) )
self.view.addSubview(youtubePlayer)
}
下面是我的发现:
- How to autoplay a YouTube video in a UIWebView
- Youtube video autoplay inside UIWebview
- How to get YouTube autoplay to work in UIWebView?
我按照以下代码的建议做了:
youtubePlayer.mediaPlaybackRequiresUserAction = false
youtubePlayer.allowsInlineMediaPlayback = true
那什么也没做。
此解决方案有效,但不适用于我正在使用的嵌入式视频。
所以,有没有办法使用UIWebView自动播放?
这些问题是几年前发布的,所以我不确定情况发生了怎样的变化。
任何人都可以提出替代方案吗?由于我有一个 UITableView
设置到哪里,当用户选择一个单元格(视频)时,它将显示视频的 UIWebView。
但是,如果可能的话,我希望在用户选择视频时立即开始播放视频,而不必再次按下播放键来播放。
我的布局设计与 TubiMusic 和 Tubex 等应用程序类似,但这些应用程序能够自动播放视频,但我不知道如何执行相同的操作。
如有任何帮助,我们将不胜感激。谢谢
自动播放参数在网络视图中不起作用。相反,您必须使用 JS api 来确定播放器的就绪状态,然后向其发送播放命令。
你可以使用这个pod https://github.com/gilesvangruisen/Swift-YouTube-Player
或者您可以使用 google 提供的代码
https://developers.google.com/youtube/v3/guides/ios_youtube_helper
这是对 How to autoplay a YouTube video in a UIWebView 的最高评价 obj-c 答案的 Swift 化版本。
Apparently the problem was with the nil base url, when I changed it to resourceURL the autoplay worked.
代码如下:
var youTubeVideoHTML: String = "<!DOCTYPE html><html><head><style>body{margin:0px 0px 0px 0px;}</style></head> <body> <div id=\"player\"></div> <script> var tag = document.createElement('script'); tag.src = \"http://www.youtube.com/player_api\"; var firstScriptTag = document.getElementsByTagName('script')[0]; firstScriptTag.parentNode.insertBefore(tag, firstScriptTag); var player; function onYouTubePlayerAPIReady() { player = new YT.Player('player', { width:'%0.0f', height:'%0.0f', videoId:'%@', events: { 'onReady': onPlayerReady, } }); } function onPlayerReady(event) { event.target.playVideo(); } </script> </body> </html>"
func playVideoWithId(videoId: String) {
var html: String = String(format: youTubeVideoHTML, self.frame.size.width, self.frame.size.height, videoId)
}