ios - 无法播放声音 : application crashes

标签 ios swift sprite-kit avfoundation

我尝试以各种方式播放声音:首先我尝试使用 SpriteKit 的 Action :

sprite.runAction(SKAction.playSoundFileNamed("explosion.mp3", waitForCompletion: false))

我还尝试将此代码分成两部分:在 init 方法中我初始化操作,稍后执行。但即使我只是初始化 SKAction,应用程序也会崩溃:

self.soundAction = SKAction.playSoundFileNamed("explosion.mp3", waitForCompletion: true)

我也尝试过使用 AVFoundation。这段代码在 init 方法中:

let explosionURL = NSBundle.mainBundle().URLForResource("explosion", withExtension: "mp3")
var error:NSError?
let data = NSData(contentsOfURL: explosionURL!)
explosionSoundPlayer = AVAudioPlayer(data: data, fileTypeHint: AVFileTypeMPEGLayer3, error: &error)
if explosionSoundPlayer != nil {
    explosionSoundPlayer?.prepareToPlay()
} else {
    println(error?.localizedDescription)
}

我必须使用 NSData 对象,因为它无法使用 URL。这样它也失败了,但至少它给了我一个更有意义的错误描述:

Optional("The operation couldn’t be completed. (OSStatus error -39.)")

最佳答案

我刚刚测试了 AVAudioPlayer,它在两种情况下都对我有用(使用 NSData 或 NSUrl)。检查下面的代码:

class GameScene: SKScene {

    var audioPlayer : AVAudioPlayer?

    var soundAction = SKAction.playSoundFileNamed("sound.wav", waitForCompletion: true)

    override func didMoveToView(view:SKView) {


        let explosionURL = NSBundle.mainBundle().URLForResource("music", withExtension: "mp3")
        var error:NSError?
        let data = NSData(contentsOfURL: explosionURL!)
        audioPlayer = AVAudioPlayer(data: data, fileTypeHint: AVFileTypeMPEGLayer3, error: &error)
        if audioPlayer != nil {
            audioPlayer?.prepareToPlay()
        } else {
            println(error?.localizedDescription)
        }

        /*
        var soundUrl = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("music", ofType: "mp3")!)
        println(soundUrl)

        var error:NSError?
        audioPlayer = AVAudioPlayer(contentsOfURL: soundUrl, error: &error)
        audioPlayer?.prepareToPlay()
       */
    }
}

你可以像这样播放声音(例如在 touchesBegan 方法中):

//audioPlayer?.play()

 self.runAction(self.soundAction)

尝试复制并粘贴此代码(只需更改声音名称和扩展名),看看您是否仍然遇到错误。 NSData 部分与您的示例相同。

我猜你已经知道了,但提一下你应该如何将声音文件添加到你的项目中也无妨:

  • 将声音拖到您的项目中
  • 在目标部分,选择“需要时复制”
  • 在添加的文件夹部分,选择“创建组”
  • 在添加到目标中,选择您的目标。

如果还是不行,我会在“Build Phases”->“Copy Bundle Resources”下查看“explosion.mp3”是否存在。如果不是,请使用 + 按钮(悬停时会显示添加项目)并添加它。

提示:

请注意,不建议使用 mp3 文件播放短音,因为 mp3 是压缩文件,需要硬件解码。解码需要 cpu 时间。此外,使用硬件解码器只能播放一个 mp3 文件。如果您一次播放多个 mp3,这些将被软件解码,速度很慢。来自文档:

When using hardware-assisted decoding, the device can play only a single instance of one of the supported formats at a time. For example, if you are playing a stereo MP3 sound using the hardware codec, a second simultaneous MP3 sound will use software decoding. Similarly, you cannot simultaneously play an AAC and an ALAC sound using hardware. If the iPod application is playing an AAC or MP3 sound in the background, it has claimed the hardware codec; your application then plays AAC, ALAC, and MP3 audio using software decoding.

播放短声音的更好选择是 .wav 和 .caf 格式。

关于ios - 无法播放声音 : application crashes,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32696429/

相关文章:

swift - 如何检测我触摸了哪些 Sprite

javascript - jQuery - $(this) 在 iPad 上

iphone - 如何确定当前位置是否在 KML 定义的区域内?

ios - 通过点击外部关闭 UIAlertViewController

swift - 当 workItem 在后台线程上完成时,如何通知主线程?

ios - 将 UIAlertAction 的功能传递给 UIAlertController 扩展

swift - UIScrollView 放大时用 1 根手指平移

ios - Swift:将 SKshapeNode 定位到最左/最右但不在屏幕外

swift - Sprite-kit:在圆形路径中移动元素

ios - 当参数不是预期的参数时,OCMock 抛出 NSInternalInconsistencyException