在Swift中,当我的应用程序打开时,如何让其他应用程序继续播放音频? [英] In Swift, how do I let other apps continue to play audio when my app is open?

查看:247
本文介绍了在Swift中,当我的应用程序打开时,如何让其他应用程序继续播放音频?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

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

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.

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

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.

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

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

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

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.

谢谢!

推荐答案

AVAudioSession 类别设置为 Ambient

import AVFoundation.AVAudioSession

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




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

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

这篇关于在Swift中,当我的应用程序打开时,如何让其他应用程序继续播放音频?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆