SpriteBuilder 学习笔记八

Chapter 11node

Audio and Labelsgit

你会学到如何播放audio,包括音效和音乐(sound effects and music)。sound effects能够经过Timeline播放,可是大部分都须要使用ObjectAL编程,ObjectAL是Cocos2D和SpriteBuilder使用的audio engine。github

第二部分是关于Labels的------特别是,TrueType font效果。express

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播放。less

了解这两种类型的不一样很重要:ide

 

 

Caution:不是全部的audio格式在移动设备上都是支持的。好比OGG和FLAC文件都不能够在IOS上播放,最好使用最通用的格式:对于sound effects使用.caf, .wav格式,对于long-running audio使用mp3或者m4a格式。oop

 

 

“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项目中的一个子文件夹中。

相关文章
相关标签/搜索