关于javascript:如何在WKWebView中播放音频?

How to play audio in WKWebView?

我正在制作一个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

有什么好的解决方案或破解方法吗?现在,我在第一次轻按时播放每个声音,并且所有声音都已满。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<html>
    <body style='bakcground:#DDD;height:333px'>
       
            Debug<br />
            Tap here to play the sound.<br />
       
       
            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)
                           })
            }
       
    </body>
</html>

还有ViewController.swift

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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()并得到相同的错误。


回复:问题1,您需要先创建配置对象,然后再设置Web视图的配置。 线

1
self.myWebView3.configuration.mediaTypesRequiringUserActionForPlayback = []

将无声地失败。 只需创建一个新的WKWebViewConfiguration对象,设置其属性,然后创建Web视图:

1
webView = WKWebView(frame: .zero, configuration: webConfiguration)

在WKWebView的委托中,在完成webview的加载后,执行将addEventListener添加到音频和视频标签的JavaScript,并进行事件播放,暂停和结束。 然后检查播放状态是否有目的。