The Mute Button Won't Mute AVAudioPlayer

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-04 09:21:16

问题


I need to loop play a .caf file in my iPhone app.

The AVAudioPlayer looks promising. But there's one problem. It won't stop or mute even if I pressed the mute button on the iPhone.

And I understand that there would be no way to programmatically find out whether that mute button is pressed or not in iOS 5.

So, what I need to know might be some properties about AVAudioPlayer or AVAudioSession. Or, perhaps, I may need to consider other libraries to loop playing my .caf file.

Any idea?


回答1:


I realized it was because I had this line of code

[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error: nil];

I then changed it into

[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryAmbient error: nil];

Then the mute button would work and stop my AVAudioPlayer...




回答2:


my 2 cents for swift 3.0:

let sharedSession = AVAudioSession.sharedInstance()

do {
    try sharedSession.setCategory(AVAudioSessionCategoryAmbient)
} catch _{
    return false
}

or simpler:

try? sharedSession.setCategory(AVAudioSessionCategoryAmbient)



回答3:


From docs:

So basically you just have to figure out which Audio Session category suits your need.



来源:https://stackoverflow.com/questions/9346438/the-mute-button-wont-mute-avaudioplayer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!