Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MusicFactory: AssetFileDescriptor closed and method to load music async #205

Open
wants to merge 1 commit into
base: GLES2
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 46 additions & 3 deletions src/org/andengine/audio/music/MusicFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnPreparedListener;

/**
* (c) 2010 Nicolas Gramlich
* (c) 2010 Nicolas Gramlich
* (c) 2011 Zynga Inc.
*
* @author Nicolas Gramlich
Expand Down Expand Up @@ -64,7 +65,9 @@ public static void onCreate() {
public static Music createMusicFromFile(final MusicManager pMusicManager, final File pFile) throws IOException {
final MediaPlayer mediaPlayer = new MediaPlayer();

mediaPlayer.setDataSource(new FileInputStream(pFile).getFD());
final FileInputStream fileInputStream = new FileInputStream(pFile);
mediaPlayer.setDataSource(fileInputStream.getFD());
fileInputStream.close();
mediaPlayer.prepare();

final Music music = new Music(pMusicManager, mediaPlayer);
Expand All @@ -78,13 +81,46 @@ public static Music createMusicFromAsset(final MusicManager pMusicManager, final

final AssetFileDescriptor assetFileDescritor = pContext.getAssets().openFd(MusicFactory.sAssetBasePath + pAssetPath);
mediaPlayer.setDataSource(assetFileDescritor.getFileDescriptor(), assetFileDescritor.getStartOffset(), assetFileDescritor.getLength());
assetFileDescritor.close();
mediaPlayer.prepare();

final Music music = new Music(pMusicManager, mediaPlayer);
pMusicManager.add(music);

return music;
}

public static void createMusicFromAssetAsync(final MusicManager pMusicManager, final Context pContext, final String pAssetPath, final IMusicLoadedListener pListener) throws IOException {
final MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {
@Override
public boolean onError(final MediaPlayer player, final int what, final int extra) {
boolean managed = false;
if (pListener != null) {
managed = pListener.onError(player, what, extra);
}
return managed;
}
});

mediaPlayer.setOnPreparedListener(new OnPreparedListener() {
@Override
public void onPrepared(final MediaPlayer player) {
final Music music = new Music(pMusicManager, mediaPlayer);
pMusicManager.add(music);
if (pListener != null) {
pListener.onMusicLoaded(music);
}
}
});

final AssetFileDescriptor assetFileDescritor = pContext.getAssets().openFd(MusicFactory.sAssetBasePath + pAssetPath);
if (assetFileDescritor != null) {
mediaPlayer.setDataSource(assetFileDescritor.getFileDescriptor(), assetFileDescritor.getStartOffset(), assetFileDescritor.getLength());
assetFileDescritor.close();
mediaPlayer.prepareAsync();
}
}

public static Music createMusicFromResource(final MusicManager pMusicManager, final Context pContext, final int pMusicResID) throws IOException {
final MediaPlayer mediaPlayer = MediaPlayer.create(pContext, pMusicResID);
Expand All @@ -98,7 +134,7 @@ public static Music createMusicFromResource(final MusicManager pMusicManager, fi

public static Music createMusicFromAssetFileDescriptor(final MusicManager pMusicManager, final AssetFileDescriptor pAssetFileDescriptor) throws IOException {
final MediaPlayer mediaPlayer = new MediaPlayer();

mediaPlayer.setDataSource(pAssetFileDescriptor.getFileDescriptor(), pAssetFileDescriptor.getStartOffset(), pAssetFileDescriptor.getLength());
mediaPlayer.prepare();

Expand All @@ -111,4 +147,11 @@ public static Music createMusicFromAssetFileDescriptor(final MusicManager pMusic
// ===========================================================
// Inner and Anonymous Classes
// ===========================================================
public interface IMusicLoadedListener {
/**
* @see {@link android.media.MediaPlayer.OnErrorListener#onError(MediaPlayer, int, int)}
*/
boolean onError(final MediaPlayer player, final int what, final int extra);
void onMusicLoaded(final Music pMusic);
}
}