cocos支持的音效格式:
支持的音乐格式 | ||
---|---|---|
Android | mp3, mid, ogg, wav | 可以播放android.media.MediaPlayer所支持的所有格式 |
iOS | aac, caf, mp3, m4a, wav | 可以播放AVAudioPlayer所支持的所有格式 |
Windows | mid, mp3, wav | 无 |
支持的音效格式 | ||
Android | ogg, wav | 对wav的支持不完美 |
iOS | caf, m4a | 可以播放Cocos2d-iPhone CocosDesion所支持的所有格式 |
Windows | mid, wav | 无 |
一、预加载
加载音乐和音效通常是个耗时间的过程,为了防止由加载产生的延时导致实际播放与游戏播放不协调的现象,在播放音乐和音效前,可以预加载音乐文件。
class EXPORT_DLL SimpleAudioEngine
{
virtual void preloadBackgroundMusic(const char* filePath);
// Preload a compressed audio file.
// The compressed audio will be decoded to wave, then written into
// an internal buffer in SimpleAudioEngine.
virtual void preloadEffect(const char* filePath);
// Unload the preloaded effect from internal buffer.
virtual void unloadEffect(const char* filePath);
}
使用示例:
#include "SimpleAudioEngine.h"
using namespace CocosDenshion;
auto audio = SimpleAudioEngine::getInstance();
// pre-loading background music and effects. You could pre-load
// effects, perhaps on app startup so they are already loaded
// when you want to use them.
audio->preloadBackgroundMusic("myMusic1.mp3");
audio->preloadBackgroundMusic("myMusic2.mp3");
audio->preloadEffect("myEffect1.mp3");
audio->preloadEffect("myEffect2.mp3");
// unload a sound from cache. If you are finished with a sound and
// you wont use it anymore in your game. unload it to free up resources.
audio->unloadEffect("myEffect1.mp3");
二、播放控制
class EXPORT_DLL SimpleAudioEngine
{
// 播放音乐
// filePath: path of the background music file,or the FileName of T_SoundResInfo.
virtual void playBackgroundMusic(const char* filePath, bool loop = false);
// 重头放
virtual void rewindBackgroundMusic();
// 播放音效,返回 sound id。有以下支持限制:
// - no pitch effect on Samsung Galaxy S2 with OpenSL backend enabled;
// - no pitch/pan/gain on win32.
virtual unsigned int playEffect(const char* filePath,
bool loop = false,
float pitch = 1.0f, // 频率
float pan = 0.0f, // 立体音效,-1.0 ~ 1.0,-1.0左声道
float gain = 1.0f); // 音量,0.0 ~ 1.0
// 停止播放,soundId失效,cache还在,需要unload
virtual void stopBackgroundMusic(bool releaseData = false);
virtual void stopEffect(unsigned int soundId);
virtual void stopAllEffects();
// 暂停播放
virtual void pauseEffect(unsigned int soundId);
virtual void pauseAllEffects();
virtual void pauseBackgroundMusic();
// 恢复播放
virtual void resumeEffect(unsigned int soundId);
virtual void resumeAllEffects();
virtual void resumeBackgroundMusic();
// 音量控制,0.0 <= volume <= 1.0
virtual void setBackgroundMusicVolume(float volume);
virtual void setEffectsVolume(float volume);
}