fixed #721: cocosdenshion can play local file on android

This commit is contained in:
minggo 2012-03-06 16:56:30 +08:00
parent 74b4b14ee3
commit 7890333505
6 changed files with 51 additions and 21 deletions

View File

@ -203,14 +203,18 @@ public class Cocos2dxMusic {
* @return * @return
*/ */
private MediaPlayer createMediaplayerFromAssets(String path){ private MediaPlayer createMediaplayerFromAssets(String path){
MediaPlayer mediaPlayer = null; MediaPlayer mediaPlayer = new MediaPlayer();
try{ try{
AssetFileDescriptor assetFileDescritor = mContext.getAssets().openFd(path); 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.prepare();
mediaPlayer.setVolume(mLeftVolume, mRightVolume); mediaPlayer.setVolume(mLeftVolume, mRightVolume);

View File

@ -219,8 +219,14 @@ public class Cocos2dxSound {
int soundId = INVALID_SOUND_ID; int soundId = INVALID_SOUND_ID;
try { 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){ } catch(Exception e){
soundId = INVALID_SOUND_ID;
Log.e(TAG, "error: " + e.getMessage(), e); Log.e(TAG, "error: " + e.getMessage(), e);
} }

View File

@ -203,14 +203,18 @@ public class Cocos2dxMusic {
* @return * @return
*/ */
private MediaPlayer createMediaplayerFromAssets(String path){ private MediaPlayer createMediaplayerFromAssets(String path){
MediaPlayer mediaPlayer = null; MediaPlayer mediaPlayer = new MediaPlayer();
try{ try{
AssetFileDescriptor assetFileDescritor = mContext.getAssets().openFd(path); 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.prepare();
mediaPlayer.setVolume(mLeftVolume, mRightVolume); mediaPlayer.setVolume(mLeftVolume, mRightVolume);

View File

@ -219,8 +219,14 @@ public class Cocos2dxSound {
int soundId = INVALID_SOUND_ID; int soundId = INVALID_SOUND_ID;
try { 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){ } catch(Exception e){
soundId = INVALID_SOUND_ID;
Log.e(TAG, "error: " + e.getMessage(), e); Log.e(TAG, "error: " + e.getMessage(), e);
} }

View File

@ -203,14 +203,18 @@ public class Cocos2dxMusic {
* @return * @return
*/ */
private MediaPlayer createMediaplayerFromAssets(String path){ private MediaPlayer createMediaplayerFromAssets(String path){
MediaPlayer mediaPlayer = null; MediaPlayer mediaPlayer = new MediaPlayer();
try{ try{
AssetFileDescriptor assetFileDescritor = mContext.getAssets().openFd(path); 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.prepare();
mediaPlayer.setVolume(mLeftVolume, mRightVolume); mediaPlayer.setVolume(mLeftVolume, mRightVolume);

View File

@ -219,8 +219,14 @@ public class Cocos2dxSound {
int soundId = INVALID_SOUND_ID; int soundId = INVALID_SOUND_ID;
try { 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){ } catch(Exception e){
soundId = INVALID_SOUND_ID;
Log.e(TAG, "error: " + e.getMessage(), e); Log.e(TAG, "error: " + e.getMessage(), e);
} }