Swift - AVAudioPlayer在设备静音时仍然播放声音?

时间:2023-01-24 08:30:55

I am making a fairly basic app and have been exploring the use of AVAudioPlayer to play sound in my app, but I am having an issue where it plays sound even when the device is on silent. This is the code I am using:

我正在制作一个相当基本的应用程序,并一直在探索使用AVAudioPlayer在我的应用程序中播放声音,但我遇到的问题是,即使设备处于静音状态,它也能播放声音。这是我正在使用的代码:

var audioPlayer = AVAudioPlayer()

Then in View did load:

然后在View中加载:

AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, error: nil)
AVAudioSession.sharedInstance().setActive(true, error: nil)
var error:NSError?
var beepOne = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("menu", ofType: "wav")!)
audioPlayer = AVAudioPlayer(contentsOfURL: beepOne, error: &error)
audioPlayer.prepareToPlay()

And when the sound is played:

播放声音时:

audioPlayer.play()

What do I need to do to stop the sound from playing when the device is on Silent or switched to silent? Perhaps before audioPlayer.play() an if statement that detects whether the device is in silent or not?

当设备处于静音状态或切换到静音状态时,我需要做什么才能停止播放声音?也许在audioPlayer.play()之前,if语句检测设备是否处于静默状态?

2 个解决方案

#1


17  

Found what was wrong, in the second and third lines of code:

在第二行和第三行代码中找到了错误:

AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, error: nil)
AVAudioSession.sharedInstance().setActive(true, error: nil)

I had to change AVAudioSessionCategoryPlayback to AVAudioSessionCategoryAmbient

我不得不将AVAudioSessionCategoryPlayback更改为AVAudioSessionCategoryAmbient

Hope this helps others who might be having the same problem :)

希望这有助于其他可能遇到同样问题的人:)

#2


0  

I'd like to add a solution using swift 2.0 which requires try/catch error handling. There's a great article on this here.

我想使用swift 2.0添加一个解决方案,需要try / catch错误处理。这里有一篇很棒的文章。

    let audioSession = AVAudioSession.sharedInstance()
    do {
        try audioSession.setCategory(AVAudioSessionCategoryAmbient, withOptions: .DuckOthers)
    } catch {
        print("AVAudioSession cannot be set")
    }       

This post was to silence audio playback if the phone is muted. I wanted to do the opposite and indeed if you replace AVAudioSessionCategoryAmbient above with AVAudioSessionCategoryPlayback, your audio will play even if the phone is muted.

这篇文章是为了在手机静音时使音频播放静音。我想做相反的事情,如果用AVAudioSessionCategoryPlayback替换上面的AVAudioSessionCategoryAmbient,即使手机静音,你的音频也会播放。

#1


17  

Found what was wrong, in the second and third lines of code:

在第二行和第三行代码中找到了错误:

AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, error: nil)
AVAudioSession.sharedInstance().setActive(true, error: nil)

I had to change AVAudioSessionCategoryPlayback to AVAudioSessionCategoryAmbient

我不得不将AVAudioSessionCategoryPlayback更改为AVAudioSessionCategoryAmbient

Hope this helps others who might be having the same problem :)

希望这有助于其他可能遇到同样问题的人:)

#2


0  

I'd like to add a solution using swift 2.0 which requires try/catch error handling. There's a great article on this here.

我想使用swift 2.0添加一个解决方案,需要try / catch错误处理。这里有一篇很棒的文章。

    let audioSession = AVAudioSession.sharedInstance()
    do {
        try audioSession.setCategory(AVAudioSessionCategoryAmbient, withOptions: .DuckOthers)
    } catch {
        print("AVAudioSession cannot be set")
    }       

This post was to silence audio playback if the phone is muted. I wanted to do the opposite and indeed if you replace AVAudioSessionCategoryAmbient above with AVAudioSessionCategoryPlayback, your audio will play even if the phone is muted.

这篇文章是为了在手机静音时使音频播放静音。我想做相反的事情,如果用AVAudioSessionCategoryPlayback替换上面的AVAudioSessionCategoryAmbient,即使手机静音,你的音频也会播放。