From ae403153c70d2534ba8c002866be47af644dbcd2 Mon Sep 17 00:00:00 2001 From: minggo Date: Thu, 15 Sep 2011 16:18:50 +0800 Subject: [PATCH] fixed #719: update java code to HelloLua & tests --- .../src/org/cocos2dx/lib/Cocos2dxActivity.java | 7 +++++-- .../src/org/cocos2dx/lib/Cocos2dxMusic.java | 18 +++++++++++++++++- .../src/org/cocos2dx/lib/Cocos2dxActivity.java | 4 ++++ .../src/org/cocos2dx/lib/Cocos2dxMusic.java | 16 ++++++++++++++++ 4 files changed, 42 insertions(+), 3 deletions(-) diff --git a/HelloLua/android/src/org/cocos2dx/lib/Cocos2dxActivity.java b/HelloLua/android/src/org/cocos2dx/lib/Cocos2dxActivity.java index a6a28ec64a..03f7b66f38 100644 --- a/HelloLua/android/src/org/cocos2dx/lib/Cocos2dxActivity.java +++ b/HelloLua/android/src/org/cocos2dx/lib/Cocos2dxActivity.java @@ -24,8 +24,7 @@ THE SOFTWARE. package org.cocos2dx.lib; - -import org.cocos2dx.hellolua.R; +import org.cocos2dx.application.R; import android.app.Activity; import android.app.AlertDialog; @@ -105,6 +104,10 @@ public class Cocos2dxActivity extends Activity{ accelerometer.disable(); } + public static void preloadBackgroundMusic(String path){ + backgroundMusicPlayer.preloadBackgroundMusic(path); + } + public static void playBackgroundMusic(String path, boolean isLoop){ backgroundMusicPlayer.playBackgroundMusic(path, isLoop); } diff --git a/HelloLua/android/src/org/cocos2dx/lib/Cocos2dxMusic.java b/HelloLua/android/src/org/cocos2dx/lib/Cocos2dxMusic.java index 797bbba73b..65ba979676 100644 --- a/HelloLua/android/src/org/cocos2dx/lib/Cocos2dxMusic.java +++ b/HelloLua/android/src/org/cocos2dx/lib/Cocos2dxMusic.java @@ -48,6 +48,22 @@ public class Cocos2dxMusic { initData(); } + public void preloadBackgroundMusic(String path){ + if ((mCurrentPath == null) || (! mCurrentPath.equals(path))){ + // preload new background music + + // release old resource and create a new one + if (mBackgroundMediaPlayer != null){ + mBackgroundMediaPlayer.release(); + } + + mBackgroundMediaPlayer = createMediaplayerFromAssets(path); + + // record the path + mCurrentPath = path; + } + } + public void playBackgroundMusic(String path, boolean isLoop){ if (mCurrentPath == null){ // it is the first time to play background music @@ -159,8 +175,8 @@ public class Cocos2dxMusic { } public void setBackgroundVolume(float volume){ + this.mLeftVolume = this.mRightVolume = volume; if (this.mBackgroundMediaPlayer != null){ - this.mLeftVolume = this.mRightVolume = volume; this.mBackgroundMediaPlayer.setVolume(this.mLeftVolume, this.mRightVolume); } } diff --git a/tests/test.android/src/org/cocos2dx/lib/Cocos2dxActivity.java b/tests/test.android/src/org/cocos2dx/lib/Cocos2dxActivity.java index ba574c560f..5315d0a197 100644 --- a/tests/test.android/src/org/cocos2dx/lib/Cocos2dxActivity.java +++ b/tests/test.android/src/org/cocos2dx/lib/Cocos2dxActivity.java @@ -106,6 +106,10 @@ public class Cocos2dxActivity extends Activity{ accelerometer.disable(); } + public static void preloadBackgroundMusic(String path){ + backgroundMusicPlayer.preloadBackgroundMusic(path); + } + public static void playBackgroundMusic(String path, boolean isLoop){ backgroundMusicPlayer.playBackgroundMusic(path, isLoop); } diff --git a/tests/test.android/src/org/cocos2dx/lib/Cocos2dxMusic.java b/tests/test.android/src/org/cocos2dx/lib/Cocos2dxMusic.java index 5f48bba8a5..65ba979676 100644 --- a/tests/test.android/src/org/cocos2dx/lib/Cocos2dxMusic.java +++ b/tests/test.android/src/org/cocos2dx/lib/Cocos2dxMusic.java @@ -48,6 +48,22 @@ public class Cocos2dxMusic { initData(); } + public void preloadBackgroundMusic(String path){ + if ((mCurrentPath == null) || (! mCurrentPath.equals(path))){ + // preload new background music + + // release old resource and create a new one + if (mBackgroundMediaPlayer != null){ + mBackgroundMediaPlayer.release(); + } + + mBackgroundMediaPlayer = createMediaplayerFromAssets(path); + + // record the path + mCurrentPath = path; + } + } + public void playBackgroundMusic(String path, boolean isLoop){ if (mCurrentPath == null){ // it is the first time to play background music