播放声音和音乐我同时使用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
}
}
}
感谢
答案 0 :(得分:1)
如果我了解情况,你可以在音乐应用程序中听音乐,例如同时玩游戏,对吗?
我对spriteKit不太熟悉,但对于普通应用,您可以使用AVAudioSessionCategoryAmbient,这样您的应用就不会停止其他声音。
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryAmbient)