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項目中的一個子文件夾中。