The Mute Button Won't Mute AVAudioPlayer

前端 未结 3 1683
再見小時候
再見小時候 2021-02-05 19:32

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 o

相关标签:
3条回答
  • 2021-02-05 20:13

    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)
    
    0 讨论(0)
  • 2021-02-05 20:21

    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...

    0 讨论(0)
  • 2021-02-05 20:25

    From docs:

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

    0 讨论(0)
提交回复
热议问题