声音仅在设备上起作用,而在模拟器中不起作用 [英] Sound does only work on Device but not in Simulator

查看:73
本文介绍了声音仅在设备上起作用,而在模拟器中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在iPad上播放一些短声音,如下所示:播放iOS中的一小段声音

I am playing some short sounds on my iPad like so: Play a short sound in iOS

我正在使用一个caf文件,该文件可以从Finder中成功播放.现在我经历了很多麻烦,试图实现声音的播放,我很好奇可能似乎是我似乎不了解的问题:

I am using a caf file which I can successfully play from the Finder. Now I went through quite a bit of a hassle trying to achieve the playback of the sound and I am curious what might be the problems which I don't seem to understand:

  • 选项1:创建SystemSoundID并立即播放时,我在设备和模拟器上听不到任何声音.

  • Option 1: When I create the SystemSoundID and then play it right away I don't hear anything on the device and the simulator.

选项2:当我为SystemSoundID创建实例变量并在viewDidLoad中对其进行初始化时,我设法只在iPad而不是模拟器上播放声音.

Option 2: When I create an instance variable for the SystemSoundID and initialize it in viewDidLoad I manage to play sound but only on the iPad, not the Simulator.

选项3:除了使用SystemSoundID之外,我还可以使用AVAudioPlayer播放.wav文件,该文件随后可在iPad和Simulator上使用,但是在这里,我需要在viewDidLoad中创建AVAudioPlayer如果一次完成所有操作,我将听不到任何声音.

Option 3: Instead of using SystemSoundID I can also use AVAudioPlayer to playback a .wav file which then works on both the iPad and the Simulator but here I need to create the AVAudioPlayer in viewDidLoad otherwise I won't get any sound if I do everything in one go.

当前最好的选择似乎是Option 3,因为它同时在Simulator和iPad上都可以使用,但是由于我需要预初始化播放器,因此我需要一个AVAudioPlayer实例来处理我想要的每种不同的声音.播放,这似乎不是很明智的...

The best option currently seems to be Option 3 because it works on both the Simulator and the iPad, but because I need to pre-initialize the Player I would need an AVAudioPlayer instance for every different sound that I want to play, which does not seem to be very memory-wise...

我是否缺少某些东西,并且可以使用AudioToolbox框架(选项1和选项2)在两个平台上播放声音

Is there something that I am missing and is it possible to play sounds on both platforms using the AudioToolbox framework (Option 1 & Option 2)

推荐答案

我编写了一个库来简化所有这些操作.它包装了AVAudioPlayer,并且在设备和模拟器上都能正常工作.

I wrote a library to simplify all this. It wraps AVAudioPlayer, and works fine on both the device and simulator.

https://github.com/nicklockwood/SoundManager

尽管我做了一些半聪明的事情来初始化音频播放器,但是代码相当简单.如果您不想使用该库,则只需复制代码即可.

The code is fairly straightforward, although I do some semi-clever stuff to initialise the audio player. If you don't want to use the library you can just copy the code.

不过要提一个警告-每当您使用AVAudioPlayer时,模拟器都会在内部引发一些奇怪的异常.它们根本不会影响应用程序,但是如果您在Xcode中启用了中断例外,则该应用程序将在启动期间掉入调试器几次,并且您将不得不手动恢复,这可能会吓到您.你没想到.

A word of warning though - the simulator throws some odd exceptions internally whenever you use AVAudioPlayer. They don't affect the app at all, but if you have enabled break-on-exceptions in Xcode then the app will drop into the debugger a few times during startup and you'll have to manually resume, which may freak you out if you're not expecting it.

这篇关于声音仅在设备上起作用,而在模拟器中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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