在Swift中,当我的应用程序打开时,如何让其他应用程序继续播放音频?

时间:2021-05-31 07:26:57

I looked around and found vaguely similar questions but nothing quite the same...I do apologize if I missed the answer somewhere.

我环顾四周,发现了类似的问题,但没有什么相同......如果我在某个地方错过了答案,我会道歉。

I am finishing up a game I wrote in Swift using SpriteKit.

我正在完成我使用SpriteKit在Swift中编写的游戏。

Most other games that I've played, I can have itunes or something playing music in the background, and still hear it while I am playing a game.

我玩过的大多数其他游戏,我都可以在背景中播放音乐或播放音乐,并且在玩游戏时仍能听到它。

As I am playing my game, I'm noticing that it automatically shuts of the audio from other apps.

在我玩游戏时,我注意到它会自动关闭来自其他应用程序的音频。

I am not using AVAudioPlayer for the sound, as I currently only have a small amount of audio effects so I was just using an SKAction.playsoundfilenamed action instead.

我没有使用AVAudioPlayer作为声音,因为我目前只有少量的音频效果,所以我只是使用SKAction.playsoundfilenamed动作。

I do have logic in there to turn my sounds on and off, but that is simply using some internal if/else logic.

我确实有逻辑可以打开和关闭我的声音,但这只是使用一些内部的if / else逻辑。

I'm wondering if perhaps there is some AVAudio property I can set that will allow other apps audio to continue playing when mine is open? I can't find this in the documentation.

我想知道是否有一些我可以设置的AVAudio属性,这将允许其他应用音频在我的开放时继续播放?我在文档中找不到这个。

Thanks!

谢谢!

5 个解决方案

#1


3  

Set your AVAudioSession category to Ambient.

将AVAudioSession类别设置为Ambient。

import AVFoundation.AVAudioSession

AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryAmbient, error: nil)

This category is also appropriate for “play along” style apps, such as a virtual piano that a user plays while the Music app is playing. When you use this category, audio from other apps mixes with your audio. Your audio is silenced by screen locking and by the Silent switch (called the Ring/Silent switch on iPhone).

此类别也适用于“播放”风格的应用程序,例如用户在播放音乐应用程序时播放的虚拟钢琴。使用此类别时,来自其他应用程序的音频会与您的音频混合。屏幕锁定和静音开关(在iPhone上称为Ring / Silent开关)可以使您的音频静音。

#2


5  

In Xcode 8.2.1, Swift 3 (latest syntax), this worked for me:

在Xcode 8.2.1,Swift 3(最新语法)中,这对我有用:

import AVFoundation

override func viewDidLoad() {
    super.viewDidLoad()

    let audioSession = AVAudioSession.sharedInstance()
    do {
        try audioSession.setCategory(AVAudioSessionCategoryPlayback, with: [.mixWithOthers])
        try audioSession.setActive(true)
    }catch{
        // handle error
    }

    // the rest of your code
}

#3


3  

As of xCode 7 October 14, 2015, this works for me.

截至2015年10月14日的xCode 7,这对我有用。

Put this on the GameViewController.swift depending which Swift version you're using.

把它放在GameViewController.swift上,取决于你使用的Swift版本。

//Swift 2.2
import AVFoundation.AVAudioSession
//Swift 3.0
import AVFoundation

And put this code in the ViewDidLoad:

并将此代码放在ViewDidLoad中:

try! AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryAmbient)

The use of "try" is when you want the program to do something and then catch an error. It's an error handler. By adding the exclamation point, you're pretty much saying "I know this won't fail."

使用“try”是指您希望程序执行某些操作然后捕获错误。这是一个错误处理程序。通过添加感叹号,你几乎都在说“我知道这不会失败”。

This works for me. I'm playing background music as my app is too.

这对我有用。我正在播放背景音乐,因为我的应用也是如此。

#4


2  

Call this code on app launch so that your AVAudioSession lets other apps chime in:

在应用程序启动时调用此代码,以便您的AVAudioSession允许其他应用程序响铃:

let audioSession = AVAudioSession.sharedInstance()    
audioSession.setCategory(AVAudioSessionCategoryPlayback, withOptions:AVAudioSessionCategoryOptions.MixWithOthers, error: nil);
audioSession.setActive(true, error: nil)

#5


0  

I'm not sure if the architecture of your viewController, but you could try running this in viewDidLoad or create a standalone method that is called within viewDidLoad.

我不确定你的viewController的体系结构,但是你可以尝试在viewDidLoad中运行它,或者创建一个在viewDidLoad中调用的独立方法。

There might be a better spot for this code, but without knowing what your app's code looks like, you could stick it there to see if you can get it working.

这段代码可能有一个更好的位置,但不知道你的应用程序的代码是什么样的,你可以坚持下去,看看你是否能让它运行起来。

// this goes at the top w/ your import statements
import AVFoundation



// you could put this in your viewDidLoad or wherever it's appropriate for your code

    let session = AVAudioSession.sharedInstance()
    session.setCategory(AVAudioSessionCategoryAmbient, withOptions: nil, error: nil)
    session.setActive(true, withOptions: nil, error: nil)

Example code from GitHub

来自GitHub的示例代码

AVAudioSession class reference

AVAudioSession类引用

#1


3  

Set your AVAudioSession category to Ambient.

将AVAudioSession类别设置为Ambient。

import AVFoundation.AVAudioSession

AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryAmbient, error: nil)

This category is also appropriate for “play along” style apps, such as a virtual piano that a user plays while the Music app is playing. When you use this category, audio from other apps mixes with your audio. Your audio is silenced by screen locking and by the Silent switch (called the Ring/Silent switch on iPhone).

此类别也适用于“播放”风格的应用程序,例如用户在播放音乐应用程序时播放的虚拟钢琴。使用此类别时,来自其他应用程序的音频会与您的音频混合。屏幕锁定和静音开关(在iPhone上称为Ring / Silent开关)可以使您的音频静音。

#2


5  

In Xcode 8.2.1, Swift 3 (latest syntax), this worked for me:

在Xcode 8.2.1,Swift 3(最新语法)中,这对我有用:

import AVFoundation

override func viewDidLoad() {
    super.viewDidLoad()

    let audioSession = AVAudioSession.sharedInstance()
    do {
        try audioSession.setCategory(AVAudioSessionCategoryPlayback, with: [.mixWithOthers])
        try audioSession.setActive(true)
    }catch{
        // handle error
    }

    // the rest of your code
}

#3


3  

As of xCode 7 October 14, 2015, this works for me.

截至2015年10月14日的xCode 7,这对我有用。

Put this on the GameViewController.swift depending which Swift version you're using.

把它放在GameViewController.swift上,取决于你使用的Swift版本。

//Swift 2.2
import AVFoundation.AVAudioSession
//Swift 3.0
import AVFoundation

And put this code in the ViewDidLoad:

并将此代码放在ViewDidLoad中:

try! AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryAmbient)

The use of "try" is when you want the program to do something and then catch an error. It's an error handler. By adding the exclamation point, you're pretty much saying "I know this won't fail."

使用“try”是指您希望程序执行某些操作然后捕获错误。这是一个错误处理程序。通过添加感叹号,你几乎都在说“我知道这不会失败”。

This works for me. I'm playing background music as my app is too.

这对我有用。我正在播放背景音乐,因为我的应用也是如此。

#4


2  

Call this code on app launch so that your AVAudioSession lets other apps chime in:

在应用程序启动时调用此代码,以便您的AVAudioSession允许其他应用程序响铃:

let audioSession = AVAudioSession.sharedInstance()    
audioSession.setCategory(AVAudioSessionCategoryPlayback, withOptions:AVAudioSessionCategoryOptions.MixWithOthers, error: nil);
audioSession.setActive(true, error: nil)

#5


0  

I'm not sure if the architecture of your viewController, but you could try running this in viewDidLoad or create a standalone method that is called within viewDidLoad.

我不确定你的viewController的体系结构,但是你可以尝试在viewDidLoad中运行它,或者创建一个在viewDidLoad中调用的独立方法。

There might be a better spot for this code, but without knowing what your app's code looks like, you could stick it there to see if you can get it working.

这段代码可能有一个更好的位置,但不知道你的应用程序的代码是什么样的,你可以坚持下去,看看你是否能让它运行起来。

// this goes at the top w/ your import statements
import AVFoundation



// you could put this in your viewDidLoad or wherever it's appropriate for your code

    let session = AVAudioSession.sharedInstance()
    session.setCategory(AVAudioSessionCategoryAmbient, withOptions: nil, error: nil)
    session.setActive(true, withOptions: nil, error: nil)

Example code from GitHub

来自GitHub的示例代码

AVAudioSession class reference

AVAudioSession类引用