JM:I have a dream,期待和你一起去实现!
原文及译文
JM
网友:Daniel Fernandez

        我正在开发一个使用WKWebView的两个webViews在Swift中构建的iOS应用程序。其中一个网页有一个默认的声音,每当webView加载声音(网络不是我的)。所以我需要停止或不允许webView重现任何声音。有...

推荐:想去Google查资料?(你懂的)想建站?想测试?想挂机?想折腾?想要高性价比的服务器?点我!!


不要让webView在Swift上播放声音(Don't allow webView to play sound on Swift)

我正在开发一个使用WKWebView的两个webViews在Swift中构建的iOS应用程序。其中一个网页有一个默认的声音,每当webView加载声音(网络不是我的)。所以我需要停止或不允许webView重现任何声音。有什么方法可以得到这个?我尝试了一些代码,但它不起作用。我正在使用xCode 9和Swift 4。

这是我试过的代码(但不起作用):

do {
    try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
}
catch let error as NSError {
    print(error)  
}

do {
    try AVAudioSession.sharedInstance().setActive(false)
}
catch let error as NSError {
    print(error)  
}

这是我的代码:

import UIKit
import WebKit

class ViewController: UIViewController, WKNavigationDelegate {

@IBOutlet weak var webView: WKWebView!

override func viewDidLoad() {
    super.viewDidLoad()

        let url = URL(string: "theWeb")
        let request = URLRequest(url: url!)
        webView.navigationDelegate = self
        webView.load(request)
    }
}

感谢您的回应,并为我的问题感到抱歉,我是编程新手。


I am developing an iOS app built in Swift with two webViews using WKWebView. One of the web has a default sound that sounds every time the webView loads (the web is not mine). So I need to stop or don't allow the webView to reproduce any sound. There is any method to get this? I tried some code but it doesn't work. I'm using xCode 9 and Swift 4.

This is the code I've tried (but it doesn't work):

do {
    try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
}
catch let error as NSError {
    print(error)  
}

do {
    try AVAudioSession.sharedInstance().setActive(false)
}
catch let error as NSError {
    print(error)  
}

This is my code:

import UIKit
import WebKit

class ViewController: UIViewController, WKNavigationDelegate {

@IBOutlet weak var webView: WKWebView!

override func viewDidLoad() {
    super.viewDidLoad()

        let url = URL(string: "theWeb")
        let request = URLRequest(url: url!)
        webView.navigationDelegate = self
        webView.load(request)
    }
}

Thank you for your responses and sorry for my question, I'm new to programming.


解决方案(由网友Rob Napier提供)

有问题的元素是这些:

<audio src="audio/alarm.mp3" preload="auto" loop="true" class="alarm mp3" type="audio/mp3"></audio>
<audio src="audio/alarm2.mp3" preload="auto" loop="true" class="urgent alarm2 mp3" type="audio/mp3"></audio>

我会尝试的第一件事是改变WKWebViewConfiguration。为了做到这一点,你必须以编程方式创建WKWebView并将其插入到视图中。不幸的是你不能用Interface Builder来做到这一点。这是一些未经测试的代码,可以工作。 (如果没有,我们可以进一步深入,但是这是我开始的地方,下一步如果这不起作用,就是实现一个WKNaviagtionDelegate来拒绝音频加载请求。)

let config = WKWebViewConfiguration()
config.mediaTypesRequiringActionForPlayback = WKAudiovisualMediaTypeAll

webView = WKWebView(frame: view.bounds, configuration: config)
view.addSubview(webView)

如果你的web视图没有填充整个视图,那么我发现一个方便的方法是在Interface Builder中添加一个空白的UIView,并将其放在你想要的地方。然后只需在运行时将webView放置在空白视图中(并使其大小相同)。这很好,因为它可以让你在IB中完成所有的布局,但是仍然可以在需要的地方插入Web视图。


The elements in question are these:

<audio src="audio/alarm.mp3" preload="auto" loop="true" class="alarm mp3" type="audio/mp3"></audio>
<audio src="audio/alarm2.mp3" preload="auto" loop="true" class="urgent alarm2 mp3" type="audio/mp3"></audio>

The first thing I would try is changing the WKWebViewConfiguration. In order to do that, you have to create the WKWebView programmatically and insert it into your view. You can't do this with Interface Builder unfortunately. Here's some untested code that may work. (If it doesn't we can dig into this further, but this is where I'd start. The next step if this doesn't work is to implement a WKNaviagtionDelegate to deny the audio load requests.)

let config = WKWebViewConfiguration()
config.mediaTypesRequiringActionForPlayback = WKAudiovisualMediaTypeAll

webView = WKWebView(frame: view.bounds, configuration: config)
view.addSubview(webView)

If your web view doesn't fill the entire view, then a convenient way I've found to make this work is to add a blank UIView in Interface Builder and lay it out where you want it. Then just put the webView in that blank view at runtime (and make it the same size). This is nice because it lets you do all the layout in IB, but still insert the web view where needed.


推荐:想去Google查资料?(你懂的)想建站?想测试?想挂机?想折腾?想要高性价比的服务器?点我!!


关于站长

JMJavaMethod的缩写,苦逼码农一个,一直想有番作为,奈何人老力衰,只能四处膜拜大佬以获得动力。已经从单机、局域网、互联网、混到了移动互联网,未来希望能在AI世界里继续混下去。这辈子有个终极目标:财务自由,心灵自由。

近期公告

一个人苦逼开发多日,网站终于正式上线,求关注!!! 一个人苦逼多日,终于正式上线,求关注!!!

找他  
猜你喜欢
想建站?想测试?
想挂机?想折腾?
想去Google查资料?(你懂的)
想要高性价比的服务器?
搬瓦工VPS - 性价最高的美国便宜VPS主机
友情链接

冀ICP备17016304号 版权所有 © JavaMethod.com All Rights Reserved,Theme by 拼图