如何在swift中将我的音频会话默认行为设置为播放?

时间:2022-07-05 00:20:37

My app has a "Click" sound functionality. I used the

我的应用程序具有“点击”声音功能。我用了

import AVFoundation

then the following function to run the "Click" sound:

然后以下函数运行“Click”声音:

var audioPlayer = AVAudioPlayer()

func playSound() {
    var soundPath = NSBundle.mainBundle().pathForResource("tick", ofType: "wav")
    var soundURL = NSURL.fileURLWithPath(soundPath!)
    self.audioPlayer = AVAudioPlayer(contentsOfURL: soundURL, error: nil)
    self.audioPlayer.play()
}

Now if the user is running a music player, my app causes the music player to stop. I read about the Audio Session Default Behavior in the documentation, but I don't know how to apply it.

现在,如果用户正在运行音乐播放器,我的应用程序会导致音乐播放器停止。我在文档中阅读了有关音频会话默认行为的信息,但我不知道如何应用它。

Can you please help?

你能帮忙吗?

Thank you!

2 个解决方案

#1


4  

If you are wondering the syntax for swift 2, here it is:

如果你想知道swift 2的语法,这里是:

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

#2


3  

Depending on what you want the app to behave, i.e, how your app's sound effect or music should interact with other app's background audio session, you might need to tweak both the audio session category and categoryOption.

根据您希望应用程序的行为,即应用程序的音效或音乐应如何与其他应用程序的后台音频会话交互,您可能需要调整音频会话类别和categoryOption。

If you just want to play the sound effect, like "tick" sound, then, AVAudioSessionCategoryAmbient and DuckOthers should be used respectively, for example:

如果您只想播放声音效果,如“滴答”声音,则应分别使用AVAudioSessionCategoryAmbient和DuckOthers,例如:

let audioSession = AVAudioSession.sharedInstance()
var error: NSErrorPointer = nil
audioSession.setCategory(AVAudioSessionCategoryAmbient, withOptions: .DuckOthers, error: error)

However, I suppose you are actually trying to play a sound effect, in this case, the AudioServices API is a more suitable choice. You can check func AudioServicesPlaySystemSound(inSystemSoundID: SystemSoundID) in AudioToolbox framework for more details.

但是,我想你实际上是在尝试播放音效,在这种情况下,AudioServices API是一个更合适的选择。您可以在AudioToolbox框架中检查func AudioServicesPlaySystemSound(inSystemSoundID:SystemSoundID)以获取更多详细信息。

Another common scenario. If you want to have your app to play audio exclusively, even if there're other app's playing the music in the background, you need to set the category to AVAudioSessionCategorySoloAmbient, for example:

另一种常见情况。如果您想让您的应用专门播放音频,即使其他应用在后台播放音乐,您也需要将类别设置为AVAudioSessionCategorySoloAmbient,例如:

let audioSession = AVAudioSession.sharedInstance()
var error: NSErrorPointer = nil
audioSession.setCategory(AVAudioSessionCategorySoloAmbient, error: error)

I hope you've got what you're looking for.

我希望你有你想要的东西。

#1


4  

If you are wondering the syntax for swift 2, here it is:

如果你想知道swift 2的语法,这里是:

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

#2


3  

Depending on what you want the app to behave, i.e, how your app's sound effect or music should interact with other app's background audio session, you might need to tweak both the audio session category and categoryOption.

根据您希望应用程序的行为,即应用程序的音效或音乐应如何与其他应用程序的后台音频会话交互,您可能需要调整音频会话类别和categoryOption。

If you just want to play the sound effect, like "tick" sound, then, AVAudioSessionCategoryAmbient and DuckOthers should be used respectively, for example:

如果您只想播放声音效果,如“滴答”声音,则应分别使用AVAudioSessionCategoryAmbient和DuckOthers,例如:

let audioSession = AVAudioSession.sharedInstance()
var error: NSErrorPointer = nil
audioSession.setCategory(AVAudioSessionCategoryAmbient, withOptions: .DuckOthers, error: error)

However, I suppose you are actually trying to play a sound effect, in this case, the AudioServices API is a more suitable choice. You can check func AudioServicesPlaySystemSound(inSystemSoundID: SystemSoundID) in AudioToolbox framework for more details.

但是,我想你实际上是在尝试播放音效,在这种情况下,AudioServices API是一个更合适的选择。您可以在AudioToolbox框架中检查func AudioServicesPlaySystemSound(inSystemSoundID:SystemSoundID)以获取更多详细信息。

Another common scenario. If you want to have your app to play audio exclusively, even if there're other app's playing the music in the background, you need to set the category to AVAudioSessionCategorySoloAmbient, for example:

另一种常见情况。如果您想让您的应用专门播放音频,即使其他应用在后台播放音乐,您也需要将类别设置为AVAudioSessionCategorySoloAmbient,例如:

let audioSession = AVAudioSession.sharedInstance()
var error: NSErrorPointer = nil
audioSession.setCategory(AVAudioSessionCategorySoloAmbient, error: error)

I hope you've got what you're looking for.

我希望你有你想要的东西。