如何在安卓cocos2d中播放背景音乐

3

你好,我是cocos2d android的新手。在android cocos2d中找不到SimpleAudioEngine和CDAudioEngine,但这两个引擎在iphone cocos2d中有。那么在android中如何播放背景音乐呢?

3个回答

3

你好,这是我使用的代码。尝试一下这种方式,你可以在四个重写方法中使用声音引擎。希望对你有所帮助。

 @Override
 protected void onStart() {
// Preload background music
SoundEngine.sharedEngine().preloadSound(context, R.raw.theme_ver1);
SoundEngine.sharedEngine().playSound(context, R.raw.theme_ver1, true);
            SoundEngine.sharedEngine().preloadEffect(CCDirector.sharedDirector().getActivity(), R.raw.object_tap2);
            super.onStart();
        }

@Override
    protected void onPause() {
        SoundEngine.sharedEngine().pauseSound();
        super.onPause();
    }

    @Override
    protected void onResume() {     
        SoundEngine.sharedEngine().resumeSound();
        super.onResume();
    }

    @Override
    protected void onDestroy() {
        SoundEngine.sharedEngine().realesAllEffects();
        SoundEngine.sharedEngine().realesAllSounds();
        SoundEngine.purgeSharedEngine();
        super.onDestroy();
    } 

2
你需要使用SoundEngine (org.cocos2d.sound.SoundEngine)。
将音乐文件放在res/raw文件夹中,并添加以下代码。
SoundEngine.sharedEngine().playSound(context, R.raw.your_music, true);

0

我希望这能帮到您。

Context context = CCDirector.sharedDirector().getActivity();
    SoundEngine.sharedEngine().preloadEffect(context, R.raw.pew_pew_lei);
    SoundEngine.sharedEngine().playSound(context, R.raw.background_music_aac, true);

停止音乐

SoundEngine.sharedEngine().realesAllSounds();

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接