From 789033350519fb5dbb9d77bca2239f3f7d91144a Mon Sep 17 00:00:00 2001 From: minggo Date: Tue, 6 Mar 2012 16:56:30 +0800 Subject: [PATCH] fixed #721: cocosdenshion can play local file on android --- .../src/org/cocos2dx/lib/Cocos2dxMusic.java | 16 ++++++++++------ .../src/org/cocos2dx/lib/Cocos2dxSound.java | 8 +++++++- .../src/org/cocos2dx/lib/Cocos2dxMusic.java | 16 ++++++++++------ .../src/org/cocos2dx/lib/Cocos2dxSound.java | 8 +++++++- .../src/org/cocos2dx/lib/Cocos2dxMusic.java | 16 ++++++++++------ .../src/org/cocos2dx/lib/Cocos2dxSound.java | 8 +++++++- 6 files changed, 51 insertions(+), 21 deletions(-) diff --git a/HelloLua/android/src/org/cocos2dx/lib/Cocos2dxMusic.java b/HelloLua/android/src/org/cocos2dx/lib/Cocos2dxMusic.java index ef7686eb48..10e3e5f3c6 100644 --- a/HelloLua/android/src/org/cocos2dx/lib/Cocos2dxMusic.java +++ b/HelloLua/android/src/org/cocos2dx/lib/Cocos2dxMusic.java @@ -203,14 +203,18 @@ public class Cocos2dxMusic { * @return */ private MediaPlayer createMediaplayerFromAssets(String path){ - MediaPlayer mediaPlayer = null; + MediaPlayer mediaPlayer = new MediaPlayer(); - try{ - AssetFileDescriptor assetFileDescritor = mContext.getAssets().openFd(path); + try{ + if (path.startsWith("/")) { + mediaPlayer.setDataSource(path); + } + else { + AssetFileDescriptor assetFileDescritor = mContext.getAssets().openFd(path); + mediaPlayer.setDataSource(assetFileDescritor.getFileDescriptor(), + assetFileDescritor.getStartOffset(), assetFileDescritor.getLength()); + } - mediaPlayer = new MediaPlayer(); - mediaPlayer.setDataSource(assetFileDescritor.getFileDescriptor(), - assetFileDescritor.getStartOffset(), assetFileDescritor.getLength()); mediaPlayer.prepare(); mediaPlayer.setVolume(mLeftVolume, mRightVolume); diff --git a/HelloLua/android/src/org/cocos2dx/lib/Cocos2dxSound.java b/HelloLua/android/src/org/cocos2dx/lib/Cocos2dxSound.java index fef1c1fc1e..ca646cb545 100644 --- a/HelloLua/android/src/org/cocos2dx/lib/Cocos2dxSound.java +++ b/HelloLua/android/src/org/cocos2dx/lib/Cocos2dxSound.java @@ -219,8 +219,14 @@ public class Cocos2dxSound { int soundId = INVALID_SOUND_ID; try { - soundId = mSoundPool.load(mContext.getAssets().openFd(path), 0); + if (path.startsWith("/")){ + soundId = mSoundPool.load(path, 0); + } + else { + soundId = mSoundPool.load(mContext.getAssets().openFd(path), 0); + } } catch(Exception e){ + soundId = INVALID_SOUND_ID; Log.e(TAG, "error: " + e.getMessage(), e); } diff --git a/HelloWorld/android/src/org/cocos2dx/lib/Cocos2dxMusic.java b/HelloWorld/android/src/org/cocos2dx/lib/Cocos2dxMusic.java index ef7686eb48..10e3e5f3c6 100644 --- a/HelloWorld/android/src/org/cocos2dx/lib/Cocos2dxMusic.java +++ b/HelloWorld/android/src/org/cocos2dx/lib/Cocos2dxMusic.java @@ -203,14 +203,18 @@ public class Cocos2dxMusic { * @return */ private MediaPlayer createMediaplayerFromAssets(String path){ - MediaPlayer mediaPlayer = null; + MediaPlayer mediaPlayer = new MediaPlayer(); - try{ - AssetFileDescriptor assetFileDescritor = mContext.getAssets().openFd(path); + try{ + if (path.startsWith("/")) { + mediaPlayer.setDataSource(path); + } + else { + AssetFileDescriptor assetFileDescritor = mContext.getAssets().openFd(path); + mediaPlayer.setDataSource(assetFileDescritor.getFileDescriptor(), + assetFileDescritor.getStartOffset(), assetFileDescritor.getLength()); + } - mediaPlayer = new MediaPlayer(); - mediaPlayer.setDataSource(assetFileDescritor.getFileDescriptor(), - assetFileDescritor.getStartOffset(), assetFileDescritor.getLength()); mediaPlayer.prepare(); mediaPlayer.setVolume(mLeftVolume, mRightVolume); diff --git a/HelloWorld/android/src/org/cocos2dx/lib/Cocos2dxSound.java b/HelloWorld/android/src/org/cocos2dx/lib/Cocos2dxSound.java index fef1c1fc1e..ca646cb545 100644 --- a/HelloWorld/android/src/org/cocos2dx/lib/Cocos2dxSound.java +++ b/HelloWorld/android/src/org/cocos2dx/lib/Cocos2dxSound.java @@ -219,8 +219,14 @@ public class Cocos2dxSound { int soundId = INVALID_SOUND_ID; try { - soundId = mSoundPool.load(mContext.getAssets().openFd(path), 0); + if (path.startsWith("/")){ + soundId = mSoundPool.load(path, 0); + } + else { + soundId = mSoundPool.load(mContext.getAssets().openFd(path), 0); + } } catch(Exception e){ + soundId = INVALID_SOUND_ID; Log.e(TAG, "error: " + e.getMessage(), e); } diff --git a/tests/test.android/src/org/cocos2dx/lib/Cocos2dxMusic.java b/tests/test.android/src/org/cocos2dx/lib/Cocos2dxMusic.java index ef7686eb48..10e3e5f3c6 100644 --- a/tests/test.android/src/org/cocos2dx/lib/Cocos2dxMusic.java +++ b/tests/test.android/src/org/cocos2dx/lib/Cocos2dxMusic.java @@ -203,14 +203,18 @@ public class Cocos2dxMusic { * @return */ private MediaPlayer createMediaplayerFromAssets(String path){ - MediaPlayer mediaPlayer = null; + MediaPlayer mediaPlayer = new MediaPlayer(); - try{ - AssetFileDescriptor assetFileDescritor = mContext.getAssets().openFd(path); + try{ + if (path.startsWith("/")) { + mediaPlayer.setDataSource(path); + } + else { + AssetFileDescriptor assetFileDescritor = mContext.getAssets().openFd(path); + mediaPlayer.setDataSource(assetFileDescritor.getFileDescriptor(), + assetFileDescritor.getStartOffset(), assetFileDescritor.getLength()); + } - mediaPlayer = new MediaPlayer(); - mediaPlayer.setDataSource(assetFileDescritor.getFileDescriptor(), - assetFileDescritor.getStartOffset(), assetFileDescritor.getLength()); mediaPlayer.prepare(); mediaPlayer.setVolume(mLeftVolume, mRightVolume); diff --git a/tests/test.android/src/org/cocos2dx/lib/Cocos2dxSound.java b/tests/test.android/src/org/cocos2dx/lib/Cocos2dxSound.java index fef1c1fc1e..ca646cb545 100644 --- a/tests/test.android/src/org/cocos2dx/lib/Cocos2dxSound.java +++ b/tests/test.android/src/org/cocos2dx/lib/Cocos2dxSound.java @@ -219,8 +219,14 @@ public class Cocos2dxSound { int soundId = INVALID_SOUND_ID; try { - soundId = mSoundPool.load(mContext.getAssets().openFd(path), 0); + if (path.startsWith("/")){ + soundId = mSoundPool.load(path, 0); + } + else { + soundId = mSoundPool.load(mContext.getAssets().openFd(path), 0); + } } catch(Exception e){ + soundId = INVALID_SOUND_ID; Log.e(TAG, "error: " + e.getMessage(), e); }