我正在开发一个带有钢琴键盘的应用程序。我希望按键后的声音只播放,当“环/静音”开关是在铃声。然后,它应该停止其他应用程序的背景音乐。如果“铃声/静音”开关处于静音状态,它就不应该做任何事情,让其他应用程序的背景音乐播放(比如Spotify)。
我尝试过的(以Spotify为背景音乐):
选项1 AVAudioSessionCategoryAmbient
AVAudioSessionCategorySoloAmbient 选项2
我认为选项2是我能得到的最接近的选项。iPhone不应该停止背景音乐时,“铃声/静音”开关是安静的,我会很高兴。
AVAudioSessionCategoryPlayback选项3
我的代码:
let NoteSound = NSURL(fileURLWithPath: Bundle.main.path(forResource: currentNote.sound, ofType: "m4a")!)
do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryAmbient)
try AVAudioSession.sharedInstance().setActive(true)
audioPlayer = try AVAudioPlayer(contentsOf: NoteSound as URL)
audioPlayer.prepareToPlay()
audioPlayer.play()
} catch {
print("Problem in getting File")
}在代码示例中,我只更改了.setCategory()。
发布于 2018-09-08 18:18:27
没有本地API来检查手机是否处于静音状态,也不鼓励您将应用程序的功能建立在这样的用户选择上(在低电池模式下,充电与否,.)。
有一个检查设备是否是静音的黑客:播放0.5s的短声音,并检测播放所需的时间。有关此解决方案的更多细节,请查看这里。
https://stackoverflow.com/questions/52237567
复制相似问题