SpriteBuilder 学习笔记八

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键,左键点击添加一个关键帧。

为了真正的能够播放音乐,你必须双击每个关键帧,编辑属性。

有Pitch,Pan和Gain设置项。

值为1.0的Pitch意思是播放原始版本(frequency)。选择0.01到1.0之间更低的pitch,会增加duration。高于1.0会增加pitch和缩短duration。2.0时,会比原始版本的速度快1倍。

Pan是-1.0到1.0之间的值,-1.0值播放left speaker,1.0播放right speaker。

Panning在stereo文件中无效,只有mono audio 文件可以被spanned。

Gain设置增加了sound的volume。但是这和volume不太一样。如果你增加Gain太多,sound会distortion。但是,总的来说,它可以用于让一些sounds比其他低10到20%。

 

但是sound-effect Timeline不适合播放需要和game evets synchronized的sounds,比如一次碰撞或者加速。

它也不能用于循环播放sounds。

 

Programming Audio Playback

对于game events的Streaming audio和audio需要编程实现。

Playing Long/Streaming Audio

 总的来说,有两种方法播放stream audio,可以把stream audio视为背景音乐,通常文件结尾扩展名为.bg。可以在预先载入一小段stream audio,也可以直接播放。

如果你想要在一个特定的时间点播放背景音乐,就可以通过seekTime参数预先载入背景音乐。或者你需要无延迟的播放---比如一段对应嘴型的演讲内容。代码如下:

OAlSimpleAudio *audio = [OALSimpleAudio sharedInstance];
[audio preload:@"Audio/menu-music.m4a" seekTime:0.0];
// later you can play back the most recently preloaded track:
[audio playBgWithLoop:YES];

OALSimpleAudio接口中,没有协议机制或者告诉我们是否预载入结束的通知。预载入不是多线程的。在上例中,playlaWithLoop只有在预载入成功后才会工作。

Caution:和其他的资源文件一样,audio文件放在SpriteBuilder项目中的一个子文件夹中。

posted on 2015-04-02 16:23  SomeBod_Y  阅读(322)  评论(0编辑  收藏  举报