Spritekit 在游戏时保留 iPhone 音乐
Posted
技术标签:
【中文标题】Spritekit 在游戏时保留 iPhone 音乐【英文标题】:Spritekit keep iPhone music while gaming 【发布时间】:2017-03-04 18:40:12 【问题描述】:播放声音和音乐我都使用SKAction.playSoundFileNamed
和AVAudioPlayer
。
如果我在 iPhone 上播放音乐并打开游戏,我的游戏会停止播放音乐。如果我打开另一个游戏,音乐不会停止。
有人知道如何让音乐播放静止吗?
我的声音管理器类是:
class SoundManager
static let sharedInstance = SoundManager()
/// Short Incidental Sound SKAction
let coin = SKAction.playSoundFileNamed("coin.wav", waitForCompletion: false)
let newLevel = SKAction.playSoundFileNamed("newLevel.wav", waitForCompletion: true)
let blup = SKAction.playSoundFileNamed("singleWater.wav", waitForCompletion: true)
let pop = SKAction.playSoundFileNamed("estrazionePop" + ".wav", waitForCompletion: true)
let pip = SKAction.playSoundFileNamed("estrazionePip" + ".wav", waitForCompletion: true)
let split = SKAction.playSoundFileNamed("split" + ".mp3", waitForCompletion: true)
let fire = SKAction.playSoundFileNamed("fire" + ".wav", waitForCompletion: true)
let stoneStop = SKAction.playSoundFileNamed("stoneStop" + ".wav", waitForCompletion: true)
let small = SKAction.playSoundFileNamed("BvS-Exp-Small" + ".wav", waitForCompletion: true)
let medium = SKAction.playSoundFileNamed("BvS-Exp-Medium" + ".wav", waitForCompletion: true)
let big = SKAction.playSoundFileNamed("BvS-Exp-Big" + ".wav", waitForCompletion: true)
let smallBoss = SKAction.playSoundFileNamed("BvS-Exp-Boss-Small" + ".wav", waitForCompletion: true)
let mediumBoss = SKAction.playSoundFileNamed("BvS-Exp-Boss-Medium" + ".wav", waitForCompletion: true)
let bigBoss = SKAction.playSoundFileNamed("BvS-Exp-Boss-Big" + ".wav", waitForCompletion: true)
let bombInizio = SKAction.playSoundFileNamed("BombInizio" + ".wav", waitForCompletion: true)
let bombScintilla:SKAudio = SKAudio(fileName: "BombScintilla" + ".wav", numberOfLoops: -1)
let bombBoom = SKAction.playSoundFileNamed("BombBoom" + ".wav", waitForCompletion: true)
let spiderSvamp = [
SKAction.playSoundFileNamed("Fhh0" + ".wav", waitForCompletion: true),
//SKAction.playSoundFileNamed("Fhh1" + ".wav", waitForCompletion: true), non mi piacciono
//SKAction.playSoundFileNamed("Fhh2" + ".wav", waitForCompletion: true),
SKAction.playSoundFileNamed("Fhh3" + ".wav", waitForCompletion: true),
SKAction.playSoundFileNamed("Fhh4" + ".wav", waitForCompletion: true),
SKAction.playSoundFileNamed("Fhh5" + ".wav", waitForCompletion: true)
]
///Suoni lunghi
var gameover: SKAudio = SKAudio(fileName: "gameover" + ".mp3", numberOfLoops: 0)
和
class SKAudio
var audio: AVAudioPlayer!
var url: URL?
fileprivate var numberOfLoops:Int!
///-1 infinite, 0 = once
init(fileName: String, numberOfLoops:Int? = -1)
url = Bundle.main.url(forResource: fileName, withExtension: nil)
self.numberOfLoops = numberOfLoops
func playSound()
if (url == nil)
print("Could not find the file \(audio)")
do
audio = try AVAudioPlayer(contentsOf: url!, fileTypeHint: nil)
catch let error as NSError print(error.debugDescription)
if audio == nil
print("Could not create audio player")
else
audio.prepareToPlay()
audio.numberOfLoops = numberOfLoops
audio.play()
func pause()
if let player = audio
if player.isPlaying
player.pause()
func resume()
if let player = audio
if !player.isPlaying
player.play()
func stop()
if let player = audio
if player.isPlaying
player.stop()
func isPlaying() -> Bool
if let player = audio
return player.isPlaying
else
return false
func increaseVolume(value:Float)
if let player = audio
if player.isPlaying
if player.volume != 1.0
let incrrease = min(value, 1.0)
player.volume += incrrease
func decreaseVolume(value:Float)
if let player = audio
if player.isPlaying
if player.volume != 0.0
let decr = max(value, 0.0)
player.volume -= decr
func setVolume(value:Float)
if let player = audio
var val = max(value, 0.0)
val = min(value, 1.0)
player.volume = val
谢谢
【问题讨论】:
【参考方案1】:如果我了解这种情况,您希望能够在音乐应用程序中听音乐并同时玩游戏,对吗?
我对 spriteKit 不是很熟悉,但对于普通应用,您可以使用 AVAudiosessionCategoryAmbient,这样您的应用就不会停止其他声音。
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryAmbient)
【讨论】:
以上是关于Spritekit 在游戏时保留 iPhone 音乐的主要内容,如果未能解决你的问题,请参考以下文章