Chapter 11
Audio and Labels
你会学到如何播放audio,包括音效和音乐(sound effects and music)。sound effects可以通过Timeline播放,但是大部分都需要使用ObjectAL编程,ObjectAL是Cocos2D和SpriteBuilder使用的audio engine。
第二部分是关于Labels的------特别是,TrueType font效果。
Introducing ObjectAL
Cocos2D包括ObjectAL audio框架,用于播放sound effects和music。ObjectAL是一个建立在OpenAL API上的框架,用于播放short sound effects很好用(.wav, .caf, .aiff),和Apple的AVAudioPlayer,它在decode和stream long-running audio时很好用(mp3,m4a,mp4,ac3)。
OALSimpleAudio singleton为所有必要的audio needs提供了简单易用的编程接口。可以概括为两种类型的audio:effects for short,memory-buffered sound effects, and bg(background)for long-running audio like music and speech.
effects commands通过OpenAL播放,background commands通过AVAudioPlayer播放。
了解这两种类型的不同很重要:
Caution:不是所有的audio格式在移动设备上都是支持的。比如OGG和FLAC文件都不可以在IOS上播放,最好使用最通用的格式:对于sound effects使用.caf, .wav格式,对于long-running audio使用mp3或者m4a格式。
“You’ll learn more about OALSimpleAudio’s programming interface shortly in this chapter. Suffice it to say that ObjectAL covers all of your audio needs. If you need more functionality than offered by OALSimpleAudio but don’t know where to start (besides the documentation and ObjectAL demos, obviously), I recommend you take a look at how the OALSimpleAudio class uses the ObjectAL, OALAudioTrack, and OALAudioSession classes.
You can dig deeper into ObjectAL on its homepage at http://kstenerud.github.io/ObjectAL-for-iPhone, where you’ll find the documentation and class reference as well as the download archive, which contains additional demo projects.”
摘录来自: Steffen Itterheim. “Learn SpriteBuilder for iOS Game Development”。 iBooks.
Using Audio Files in SpriteBuilder
尽管你可以直接添加audio文件到Xcode项目中,但是建议你让SpriteBuilder来管理所有的audio文件,不论它们是否在Timeline中用到。
Importing Audio into SpriteBuilder
如果你使用SpriteBuilder来管理你的audio文件,你不需要担心audio格式细节。如果你导入未压缩的audio文件,比如WAV文件是最好的。Underneath,you see the duration expressed in seconds and an indication of whether the audio file is mono or stereo.在底层,有一个publishing settings,你可以选择CAF格式(uncompressed)针对short sound effects,和MP4格式(compressed)针对long-running audio.MP4格式同时允许你改变quality setting,quality越低,最后的文件越小。默认的audio quality settings在File-》Project Settings对话框中打开。
SpriteBuilder会默认输入的audio文件短于15s的为sound effect,并且选择对应的CAF格式。否则,就是MP4格式。同时注意,目前,只有.caf和.wav的audio files可以被导入。
Playing Sound Effects via the Timeline
在SpriteBuilder中,你可以添加sound-effects 关键帧在任何Timeline中。如果你需要和其他Timeline animations加锁的audio playback,那么这一点就很有用了。Timeline sound effects会通过OALSimpleAudio的effects channel播放,thus using OpenAL for playback regardless of the file format.
Caution:如果有一个Timeline的CCBAnimationManager的node 释放了,那么通过Timeline播放的sound effect会被stopped或者根本不会开始播放。这很典型的会影响audio文件的playback(那些持续时间比Timeline长的,而且node从scene中移除了)----比如,通过一个Callbacks 关键帧。(This typically affects the playback of audio files with a duration longer than the Timeline whose end has the node removed from the scene----for instance, via a Callbacks keyframe).
在SpriteBuilder中,打开MainMenuButtons.ccb。目标是在logo和buttons出现的时候,synchronize sound-effects playback。
你可以添加Sound effects 关键帧,就像添加Callbacks关键帧一样,按住Option键,左键点击添加一个关键帧。