使用 AVQueuePlayer 播放声音,即使 iPhone 已静音

Play sounds with AVQueuePlayer even if iPhone is muted

我正在使用 AVQueuePlayer 播放声音(一个供孩子们听图画书的小应用程序),但如果 iPhone 静音,则听不到音频。

即使 iPhone 静音,也有强制音频的方法吗?我想去能听到音频的 YouTube 或 Instagram,当你必须调高音量才能听故事时。

谢谢。

默认情况下 AVPlayer 取决于铃声位置,如果铃声处于静音模式则您无法收听音频。如果您想在铃声处于静音状态时播放音频,请在播放视频之前将 setCategory 设置为 AVAudioSessionCategoryPlayback

try? AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, mode: AVAudioSessionModeDefault, options: [])

我认为您不能否决硬件按钮(也不应该),更好的方法是检查设备是否静音并在这种情况下向用户显示警报,但是,下面的库说:

There is no native iOS API to detect if the mute switch is enabled/disabled on a device.

The general principle to check if the device is muted is to play a short sound and detect the length it took to play. From this length we can determine if it was muted or not.

The library 可能对你有帮助。