gpt4 book ai didi

iOS:在 WebView 中播放嵌入式剪辑时静音

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:13:10 25 4
gpt4 key购买 nike

在我的应用程序中,我使用 AVAudioSession 播放音频流,它会在应用程序启动时自动开始播放。

在应用程序的另一部分,如果用户导航,则有一个 webview,其中可能包含来自 youtube、soundcloud 的嵌入剪辑,无论什么都可能包含声音。我希望主音频流暂停,以防用户选择播放 web View 中的声音。现在,如果用户选择在 webview 中播放剪辑,声音会混合在一起,这很烦人,用户必须手动暂停主流。有没有办法防止这种情况?我需要将主流静音或暂停,以防用户选择收听嵌入的剪辑。

我在 AppDelegate 中使用的是以下内容:

AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
AVAudioSession.sharedInstance().setActive(true)

最佳答案

1.您需要订阅AVAudioSession.interruptionNotification您可以在哪里停止播放音频

swift 4.2

订阅通知

NotificationCenter.default.addObserver(self, selector: #selector(handleAudioSessionInterruption), name: AVAudioSession.interruptionNotification, object: AVAudioSession.sharedInstance())

处理通知

@objc func handleAudioSessionInterruption(_ notification : Notification) {
guard let userInfo = notification.userInfo as? [String: AnyObject] else { return }
guard let rawInterruptionType = userInfo[AVAudioSessionInterruptionTypeKey] as? NSNumber else { return }
guard let interruptionType = AVAudioSession.InterruptionType(rawValue: rawInterruptionType.uintValue) else { return }

switch interruptionType {
case .began:
//stop play your audio
case .ended: //interruption ended
//do something if needed
}
}

2. 这仅适用于设备。不要在模拟器上测试

关于iOS:在 WebView 中播放嵌入式剪辑时静音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42878677/

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