当我的应用程序在使用Swift时仍然播放其声音时,如何允许继续播放背景音乐

Nat*_*per 42 audio mobile xcode ios swift

我创建了一个应用程序,我试图让用户在玩游戏的同时继续听他们的音乐,但每当他们点击"播放"并且发出游戏声音时它就会停止背景音乐.我正在使用Swift在iOS上进行开发.这是启动游戏声音的一段代码.

func playSpawnedDot() {
    var alertSound: NSURL = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("spawnDot", ofType: "mp3")!)!
    var error:NSError?
    audioPlayer = AVAudioPlayer(contentsOfURL: alertSound, error: &error)
    audioPlayer.prepareToPlay()

    if volumeBool {
        audioPlayer.play()
    }
}
Run Code Online (Sandbox Code Playgroud)

lch*_*amp 50

您需要设置的AVAudioSession类别,具有以下值之一:https://developer.apple.com/library/ios/documentation/AVFoundation/Reference/AVAudioSession_ClassReference/index.html(AVAudioSession类参考).

默认值设置为AVAudioSessionCategorySoloAmbient.你可以读到:

[...]使用此类别意味着您的应用程序的音频不可混合 - 激活您的会话将中断任何其他也不可混合的音频会话.要允许混音,请改用AVAudioSessionCategoryAmbient类别.

在播放声音之前,您必须更改类别.为此:

AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryAmbient, error: nil)
AVAudioSession.sharedInstance().setActive(true, error: nil)
Run Code Online (Sandbox Code Playgroud)

每次播放声音时都不需要调用这些行.您可能只想做一次.

  • 如果您在将此代码适应较新版本的 Swift 时遇到困难,请尝试使用如下内容:“尝试?” AVAudioSession.sharedInstance().setCategory(AVAudioSession.Category.ambient)` (2认同)

Mil*_*sáľ 15

Swift 4版本:

try? AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryAmbient)
try? AVAudioSession.sharedInstance().setActive(true)
Run Code Online (Sandbox Code Playgroud)


小智 13

这就是我在Swift 3.0中的表现

var songPlayer : AVAudioPlayer?         

func SetUpSound() {


        if let path = Bundle.main.path(forResource: "TestSound", ofType: "wav") {
            let filePath = NSURL(fileURLWithPath:path)
            songPlayer = try! AVAudioPlayer.init(contentsOf: filePath as URL)
            songPlayer?.numberOfLoops = -1 //logic for infinite loop
            songPlayer?.prepareToPlay()
            songPlayer?.play()
        }

        let audioSession = AVAudioSession.sharedInstance()
        try!audioSession.setCategory(AVAudioSessionCategoryPlayback, with: AVAudioSessionCategoryOptions.duckOthers) //Causes audio from other sessions to be ducked (reduced in volume) while audio from this session plays  
}
Run Code Online (Sandbox Code Playgroud)

您可以在此处查看更多AVAudioSessionCategoryOptions:https://developer.apple.com/reference/avfoundation/avaudiosessioncategoryoptions


小智 10

这是我用于Swift 2.0的内容:

let sess = AVAudioSession.sharedInstance()
if sess.otherAudioPlaying {
    _ = try? sess.setCategory(AVAudioSessionCategoryAmbient, withOptions: .DuckOthers)
    _ = try? sess.setActive(true, withOptions: [])
}
Run Code Online (Sandbox Code Playgroud)

请注意,如果您不想降低背景音乐而不是播放它.DuckOthers,[]则可以替换.


itn*_*nti 5

lchamp的解决方案非常适合我,适用于Objective-C:

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


小智 5

因为他们似乎无法从版本到版本下定决心。这是 Swift 5.0 中的

do{
   try AVAudioSession.sharedInstance().setCategory(.ambient)
   try AVAudioSession.sharedInstance().setActive(true, options: .notifyOthersOnDeactivation)
} catch {
   NSLog(error.localizedDescription)
}
Run Code Online (Sandbox Code Playgroud)