AVAudioPlayer声音没有播放 [英] AVAudioPlayer sound not playing

查看:675
本文介绍了AVAudioPlayer声音没有播放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在iOS 8 / Xcode 6中,我有一个包含声音效果的功能。在多次更改代码后,它不再适用于iOS 9。这是我试过的:

In iOS 8/Xcode 6 I had a function that included a sound effect. It no longer works in iOS 9 after changing the code multiple times. This is what I've tried:

原文:

let bangSoundEffect = SKAction.playSoundFileNamed("Bang.mp3", waitForCompletion: false)
        runAction(bangSoundEffect)

其他尝试:

self.runAction(SKAction.playSoundFileNamed("Bang.mp3", waitForCompletion: false))

此外:

func playRocketExplosionSound(filename: String) {
            let url = NSBundle.mainBundle().URLForResource(
                filename, withExtension: nil)
            if (url == nil) {
                print("Could not find file: \(filename)")
                return }
            var error: NSError? = nil
            do {
                backgroundMusicPlayer =
                    try AVAudioPlayer(contentsOfURL: url!)
            } catch let error1 as NSError {
                error = error1
                backgroundMusicPlayer = nil
            }
            if backgroundMusicPlayer == nil {
                print("Could not create audio player: \(error!)")
                return}
            backgroundMusicPlayer.numberOfLoops = 1
            backgroundMusicPlayer.prepareToPlay()
            backgroundMusicPlayer.play() }



playRocketExplosionSound("Bang.mp3")

我正在拔头发。我在不同的场景中使用相同的代码来获得另一种声音效果,它工作得很好!!出了什么问题?
我注意到声音效果有时在模拟器中开始播放,但它没有完成并抛出此错误:

I'm pulling my hair out. I'm using the same code in a different scene for another sound effect and it works fine!! What's going wrong? I've noticed that the sound effect begins to play sometimes in the simulator, however it doesn't complete and throws this error:

2015-09-24 19:12:14.554 APPNAME[4982:270835] 19:12:14.553 ERROR:    177: timed out after 0.012s (735 736); mMajorChangePending=0

它在实际设备上根本不起作用。

It doesn't work at all on actual devices.

有什么问题? :'(

推荐答案

MP3文件可能出现问题



问题很可能与你正在使用的 MP3 文件有关。该代码适用于其他声音,这表明 MP3 文件可能已损坏且 AVAudioPlayer 因解码而失败。您可以尝试重新编码此文件并查看问题是否仍然存在。或者更好的是,将其转换为 WAV

Possible problem with MP3 file

The problem is most likely connected with the MP3 file you're using. The code works for other sounds, this suggests that the MP3 file might be corrupted and AVAudioPlayer fails with decoding it. You can try re-encode this file and see if the problem persists. Or, even better, converting it to WAV.

拇指的一般规则当为游戏创建短音效时,是使用 WAV ,除非你真的觉得你需要修剪它的脂肪。

General rule of the thumb when creating short sound effects for games, is to use WAV unless you really feel you need the trim the fat.

一流的游戏将获得顶级的制作质量,因此他们可以录制和制作未压缩的资产 24bit / 48kHz 。野心稍小的标题可以在 16 / 44.1 中记录和生产,这是官方标准对于 CD 质量音频。

Top-notch games are going for top-of-the-line production quality, so they record and produce assets uncompressed 24bit/48kHz. Titles with slightly lesser ambitions might record and produce in 16/44.1, which is the official standard for CD quality audio.

这至少有两个好处。一个是声音质量更好。第二个, CPU 不必解码文件来播放它。

This has at least two benefits. One is that the sound has a better quality. Second one, the CPU does not have to decode the file to play it.

这篇关于AVAudioPlayer声音没有播放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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