Cocos2d 播放音效

Cocos2D对音乐/音效播放的封装已经很好了,下边是例子:

定义2个文件:

[cpp] view
plain
copy

  1. #define MUSIC_FILE        "LOLBgMusic.mp3"
  2. #define EFFECT_FILE      "Attack.wav"

在初始化的时候预加载音乐/音效文件:

[cpp] view
plain
copy

  1. //预加载
  2. CocosDenshion::SimpleAudioEngine::sharedEngine()->preloadBackgroundMusic(MUSIC_FILE);
  3. CocosDenshion::SimpleAudioEngine::sharedEngine()->preloadEffect( EFFECT_FILE );
  4. //设置音量
  5. CocosDenshion::SimpleAudioEngine::sharedEngine()->setEffectsVolume(0.5);
  6. CocosDenshion::SimpleAudioEngine::sharedEngine()->setBackgroundMusicVolume(0.5);

播放:

[cpp] view
plain
copy

  1. //循环播放背景音乐,
  2. CocosDenshion::SimpleAudioEngine::sharedEngine()->playBackgroundMusic(MUSIC_FILE,true);

[cpp] view
plain
copy

  1. //播放音效
  2. CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect(EFFECT_FILE);

其他相关函数:

[cpp] view
plain
copy

  1. // stop background music
  2. SimpleAudioEngine::sharedEngine()->stopBackgroundMusic();
  3. // pause background music
  4. SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();
  5. // resume background music
  6. SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();
  7. // rewind background music
  8. SimpleAudioEngine::sharedEngine()->rewindBackgroundMusic();
  9. // is background music playing
  10. SimpleAudioEngine::sharedEngine()->isBackgroundMusicPlaying()
  11. // add bakcground music volume
  12. SimpleAudioEngine::sharedEngine()->setBackgroundMusicVolume(SimpleAudioEngine::sharedEngine()->getBackgroundMusicVolume() + 0.1f);
  13. SimpleAudioEngine::sharedEngine()->pauseEffect(m_nSoundId);
  14. SimpleAudioEngine::sharedEngine()->resumeEffect(m_nSoundId);
  15. SimpleAudioEngine::sharedEngine()->pauseAllEffects();
  16. SimpleAudioEngine::sharedEngine()->resumeAllEffects();
  17. SimpleAudioEngine::sharedEngine()->stopAllEffects();
时间: 2024-08-10 15:04:58

Cocos2d 播放音效的相关文章

android SoundPool播放音效

MediaPlayer的缺点: 资源占用量高,延时时间较长 不支持多个音效同时播放 SoundPool主要用于播放一些较短的声音片段,CPU资源占用率低和反应延时小,还支持自行色设置声音的品质,音量,播放比率等参数,避免使用SoundPool来播放歌曲或者做游戏背景音乐,只有那些短促的密集的声音才考虑使用SoundPool播放 构造器: public SoundPool (int maxStreams, int streamType, int srcQuality) Parameters max

cocos2d 播放GIF动画类

cocos2d 播放GIF动画类 以前项目中曾经用到过,后来因为GIF图像的质量较差,被弃用了,把公司名字去掉分享下,根据网上资料改编的cocos2d-iphone版的. // // CCSpriteGif.h // // Created by Yuming on 13-1-23. // Copyright 2013年 __MyCompanyName__. All rights reserved. // // 本类需要导入ImageIO.framework #import <Foundation

Android设备 cocos2dx 骨骼动画注册事件播放音效,退到后台再返回黑屏问题

最近遇到一个cocos2dx 骨骼动画注册事件播放音效,在骨骼动画播放的时候,按HOME键退到桌面,再次打开游戏的时候,会黑屏. 解决办法如下,可能不是太完美,至少解决了大部分问题. 1.在org.cocos2dx.lib下的 Cocos2dxRenderer.java 中添加native方法 public static native void setIsPause(boolean isPause); 2.在Cocos2dxRenderer.java对应的Java_org_cocos2dx_li

安卓播放音效

public class MyActivity extends Activity {       /** Called when the activity is first created. */       SoundPool sp;                           //得到一个声音池引用       HashMap<Integer,Integer> spMap;           //得到一个map的引用       Button b1;               

iOS 播放音效

音效:通俗的说是短音频,音频文件必须满足下面的条件 1.播放的时间不能超过30秒 2.数据必须是 PCM或者IMA4流格式 3.必须被打包成下面三个格式之一:Core Audio Format (.caf), Waveform audio (.wav), 或者 Audio Interchange File (.aiff) 1.引入框架 1 #import <AudioToolbox/AudioToolbox.h> 2.播放音效 1 - (void)touchesEnded:(NSSet *)t

C#播放音效(web/winform)

[System.Runtime.InteropServices.DllImport("winmm.DLL", EntryPoint = "PlaySound", SetLastError = true)]        private static extern bool PlaySound(string szSound, System.IntPtr hMod, PlaySoundFlags flags); [System.Flags]        public

cocos2dx AudioEngine在Android7上播放音效卡顿问题处理

1.此问题在cocos2dx 3.13/3.14版本(其它版本没有测试过)在Android7中使用AudioEngine的play2d函数播放音效时出现. 调试时出现如下提示: 2.论坛中相关讨论帖地址:http://forum.cocos.com/t/android7/44119 3.解决办法: 将cocos2d-x\cocos\audio\android\AudioPlayerProvider.cpp中的getSystemAPILevel() < 17都改为getSystemAPILevel

按钮点击播放音效

1.引入音频管理文件 #import <AVFoundation/AVFoundation.h> 2.方法抽取 /************ 播放音效 *****************************/ - (void)playSoundEffect:(NSString *)name withType:(VoiceType)type { //播放扫描二维码的声音 SystemSoundID soundID; NSString *strSoundFile = [[NSBundle mai

用AudioSource.PlayClipAtPoint播放音效

游戏中的声音通常可以分为音乐和音效,音乐通常时间较长,且需要循环播放,音效则时间较短,不需要循环播放. 播放声音的方式常见有两种: 1.建立一个空物体,为每一个音乐或音效添加AudioSource,给每个AudioSource添加相应的声音剪辑,播放时,获取各个AudioSource组件来播放声音. 2.建立一个空物体,添加一个AudioSource用来播放背景音乐:再添加一个Audiosource,用来播放音效,该组件的Audio Clip属性,在运行时根据需要播放的声音,赋予不同的声音剪辑,