我正在制作一个iOS应用程序来包装我的JavaScript游戏。在移动浏览器中,它可以正常工作,因为在ontouchstart上播放声音后,我可以随时播放任何音频,也可以设置其音量。
问题1:在WKWebView中,我只能播放在ontouchstart中播放的特定声音,而不能播放其余声音。因此,我会在第一次点击时播放游戏中的每个音频。听起来真的很糟糕。否则,在audio.play()的javascript中,我得到
Unhandled Promise Rejection: NotAllowedError: The request is not allowed by the user agent or the platform in the current context, possibly because the user denied permission.
问题2:我也无法降低WKWebView中的声音音量。如果我将myAudio.volume = .5设置为立即为1。这意味着用户必须实际听到它们,才能使它们进入readyState = 4
任何好的解决方案还是黑客?现在,我在第一次轻按时播放每个声音,并且所有声音都已满。
<html>
<body style='bakcground:#DDD;height:333px'>
<div id=debug style='font-size:20px' onclick="playSound(myAudio)">
Debug<br />
Tap here to play the sound.<br />
</div>
<script>
var context = new webkitAudioContext()
var myAudio = new Audio("sound/upgrade.wav")
myAudio.addEventListener('canplaythrough', function(){log('canplaythrough1')}, false)
var myAudio2 = new Audio("sound/dragon.wav")
myAudio2.addEventListener('canplaythrough', function(){log('canplaythrough2')}, false)
function playSound(sound)
{
log("playSound() readyState="+sound.readyState)
try{
sound.play()
context.resume().then(() => {
log("Context resumed")
sound.play()
})
}
catch(e)
{
log(e)
}
}
function log(m)
{
console.log(m)
debug.innerHTML += m+"<br />"
}
window.ontouchstart = function()
{
log("ontouchstart()")
playSound(myAudio)
setTimeout(function() {
playSound(myAudio2, 1111)
})
}
</script>
</body>
</html>
和ViewController.swift
import UIKit
import WebKit
class ViewController: UIViewController {
private var myWebView3 : WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
self.myWebView3 = WKWebView(frame: .zero)
self.myWebView3.configuration.preferences.setValue(true, forKey: "allowFileAccessFromFileURLs")
self.myWebView3.configuration.mediaTypesRequiringUserActionForPlayback = []
self.myWebView3.configuration.allowsInlineMediaPlayback = true
let url = Bundle.main.url(forResource: "index6", withExtension: "html", subdirectory: "/")!
self.myWebView3.loadFileURL(url, allowingReadAccessTo: url)
let request = URLRequest(url: url)
self.myWebView3.load(request)
self.view.addSubview(self.myWebView3)
}
override func viewDidLayoutSubviews() {
self.myWebView3.frame = self.view.bounds
}
}
我正在使用Xcode 10.1 iPhone SE 12.1.1 我也在带iOS 10的iPad上尝试过并得到相同的错误。 我也尝试了context.decodeAudioData()/ context.createBufferSource()并得到相同的错误。
答案 0 :(得分:1)
关于:问题1,您需要先创建配置对象 ,然后再设置Web视图的配置。线
self.myWebView3.configuration.mediaTypesRequiringUserActionForPlayback = []
将静默失败。只需创建一个新的WKWebViewConfiguration
对象,设置其属性,然后创建Web视图:
webView = WKWebView(frame: .zero, configuration: webConfiguration)
答案 1 :(得分:0)
在WKWebView的委托中,完成webview的加载后,执行将 addEventListener 添加到具有事件播放,暂停,< strong> end 。然后检查播放状态是否有目的。