Swift:尝试播放音频文件时出现未知错误

时间:2018-09-27 14:44:32

标签: ios swift audio

我目前正在使用Xcode(版本9.4)中的SpriteKit开发应用程序。几个小时前,我在代码中没有任何问题或错误,并且一切正常。我做了一些小的代码更改,但没有什么太过激烈。在过去一个小时的某个时候,我的大多数音频文件停止播放。

在其中创建bulletSound和backingAudioMain的某些代码以及使用它们的函数。

let bulletSound = SKAction.playSoundFileNamed("bulletSound.wav", waitForCompletion: false)

var backingAudioMain = AVAudioPlayer()

//A function that uses bulletSound
func fireBullet() {
let bullet = SKSpriteNode(imageNamed: "bullet")
bullet.name = "Bullet" //Bullet is a reference name for the object bullet
bullet.setScale(1.1)
bullet.position = player.position
bullet.zPosition = 1
bullet.physicsBody = SKPhysicsBody(rectangleOf: bullet.size)
bullet.physicsBody!.affectedByGravity = false
bullet.physicsBody!.categoryBitMask = physicsCategories.Bullet
bullet.physicsBody!.collisionBitMask = physicsCategories.None
bullet.physicsBody!.contactTestBitMask = physicsCategories.EnemyFighter //we will be told when a bullet hits the enemy
self.addChild(bullet)

let moveBullet = SKAction.moveTo(y: self.size.height + bullet.size.height, duration: 0.8) //Bullet moves to the top of the scene height plus the height of the bullet so it goes just off screen
let deleteBullet = SKAction.removeFromParent() //once the bullet is just off screen, the bullet is deleted to preserve memory
let bulletSequence = SKAction.sequence([bulletSound, moveBullet, deleteBullet]) //Sequences off the two actions above
bullet.run(bulletSequence)}

//A function that uses backingAudioMain
func playMusic(){
let filePath = Bundle.main.path(forResource:"backingAudioMain", ofType: "wav")
let audioURL = URL(fileURLWithPath: filePath!)

do{ backingAudioMain = try AVAudioPlayer(contentsOf: audioURL)}
catch { return print ("Cannot find the audio.")}

backingAudioMain.numberOfLoops = -1
backingAudioMain.play()}

尝试使用fireBullet函数时,调试器会出现一些错误。

2018-09-27 10:34:16.414492-0400 Solo Mission[8410:191101] [aqme] 202: err 1718449215
2018-09-27 10:34:16.426109-0400 Solo Mission[8410:191101] [aqme] 202: err 1718449215
2018-09-27 10:34:19.127161-0400 Solo Mission[8410:190608] AUBase.cpp:832:DispatchSetProperty:  ca_require: ValidFormat(inScope, inElement, newDesc) InvalidFormat
2018-09-27 10:34:19.129167-0400 Solo Mission[8410:190608] SKAction: Error loading sound resource: "bulletSound.wav"
2018-09-27 10:34:19.151046-0400 Solo Mission[8410:191101] [aqme] 202: err 1718449215
2018-09-27 10:34:21.597514-0400 Solo Mission[8410:190608] SKAction: Error loading sound resource: "bulletSound.wav"

我注意到只有我的backingAudioMain文件(即AVAudioPlayer)正在播放。经过数小时的尝试来解决和研究问题之后,我什么都没想到。我认为这是Xcode的问题,但我不确定。

1 个答案:

答案 0 :(得分:0)

let deleteBullet = SKAction.removeFromParent() //once the bullet is just off screen, the bullet is deleted to preserve memory
let bulletSequence = SKAction.sequence([bulletSound, moveBullet, deleteBullet]) //Sequences off the two actions above
bullet.run(bulletSequence)

因此声音操作永远不会播放,因为删除操作会中断它。要查看此内容,请将第二行更改为

let bulletSequence = SKAction.sequence([bulletSound, moveBullet])

如果您要从父级中删除,请在序列中插入一个wait操作,例如SKAction.wait(forDuration: 5.0)

相关问题