Cocos2d-x   发布时间:2022-05-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Cocos2d-x 音频功能学习笔记大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

http://cn.cocos2d-x.org/tutorial/show?id=2448


游戏离不开声音!Cocos2d-x中提供了一个叫做SimpleAudioENGIne的音频引擎。SimpleAudioENGIne能够在游戏中播放背景音效以及游戏音效。SimpleAudioENGIne是一个共享的单例对象,因此你可以在程序的任意地方调用它。就算是一个 HelloWorld 工程也可以很方便地使用这个引擎。SimpleAudioEgnine 支持多种格式的音频,比如MP3和CAF(Core Audio Forma)

新手入门

SimpleAudioENGIne API的使用非常简单。

播放背景音乐

选择一个音频文件作为背景音乐,这个文件会被单曲循环一直播放。

1
2
3
4
5
auto audio = SimpleAudioENGIne::geTinstance();
// set the BACkground music and conTinuously play it.
audio->playBACkgroundMusic( "mymusic.mp3" , true );
// set the BACkground music and play it just once.
audio->playBACkgroundMusic( "mymusic.mp3" ,153)!important; BACkground:none!important">false );

播放音效

播放音效的方法如下:

4
auto audio = SimpleAudioENGIne::geTinstance();
// play a sound effect,just once.
audio->playEffect( "myEffect.mp3" ,153)!important; BACkground:none!important">false ,1.0f,1.0f);

暂停、停止、恢复音乐和音效的播放

当播放音乐和音效时,我们常常需要暂停、停止或者恢复它们。这些实现起来是比较简单的!

暂停

5
6
7
8
9
10
auto audio = SimpleAudioENGIne::geTinstance();
// pause BACkground music.
audio->pauseBACkgroundMusic();
// pause a sound effect.
audio->pauseEffect();
// pause all sound effects.
audio->pauseAllEffects();

停止

// stop BACkground music.
audio->stopBACkgroundMusic();
// stop a sound effect.
audio->stopEffect();
// stops all running sound effects.
audio->stopAllEffects();

Resume 恢复

// resume BACkground music.
audio->resumeBACkgroundMusic();
// resume a sound effect.
audio->resumeEffect();
// resume all sound effects.
audio->resumeAllEffects();

音频高级功能

Setup 设置

SimpleAudioENGIne的API非常简单,但是在游戏中使用还是有一些注意事项,尤其是在手机和平板的等移动设备中使用时。比如在多个APP中切换时应如何处理,在或者当你玩着游戏时有电话打进来又该怎么办?这些异常在制作游戏时都必须提前想好处理方法,当然幸运的是,你能想到的异常引擎都帮我们做好了,你只需使用就好。

在AppDelegate.cpp中,注意以下几个方法:

@H_268_262@ 10
11
12
13
14
15
// This function will be called when the app is inactive. When comes a phone call,
// it's be invoked too
void AppDelegate::applicationDidEnterBACkground() {
Director::geTinstance()->stopAnimation();
// if you use SimpleAudioENGIne,it must be pause
// SimpleAudioENGIne::geTinstance()->pauseBACkgroundMusic();
}
// this function will be called when the app is active again
void AppDelegate::applicationWillEnterForeground() {
Director::geTinstance()->startAnimation();
:1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-size:1em!important; min-height:auto!important; color:rgb(0,it must resume here
// SimpleAudioENGIne::geTinstance()->resumeBACkgroundMusic();
}

如果你要用SimpleAudioENGIne实现背景音乐和音效,那么就需要注意别忘了去掉代码中有用代码的注释。

预加载音效

当游戏开始时,你需要预加载一些音效到内存中,以便当你想使用它们时能随时播放出来。

15
// 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" );

音量

你可以通过程序的控制来增大减小音量。

// setTing the volume specifying value as a float
audio->setEffectsVolume(5.0f);

Ps.以上全文为Cocos引擎中文官网翻译校对,敬请勘误。转载请注明出自"Cocos引擎中文官网"。关于中间章节编写尚未全面完成,敬请期待。

大佬总结

以上是大佬教程为你收集整理的Cocos2d-x 音频功能学习笔记全部内容,希望文章能够帮你解决Cocos2d-x 音频功能学习笔记所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。