Back to project page AudioChoice.
The source code is released under:
Apache License
If you think the Android project AudioChoice listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.hwrdprkns.audiochoice; /* ww w . ja v a 2 s .c om*/ import android.content.Context; import android.content.res.AssetFileDescriptor; import android.media.AudioManager; import android.media.MediaPlayer; import android.util.Log; import java.io.IOException; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; import java.util.concurrent.TimeUnit; public class BasicMediaPlayer implements IAudioPlayer, MediaPlayer.OnBufferingUpdateListener, MediaPlayer.OnCompletionListener, MediaPlayer.OnPreparedListener, MediaPlayer.OnErrorListener, MediaPlayer.OnInfoListener, MediaPlayer.OnSeekCompleteListener { private final static String TAG = BasicMediaPlayer.class.getSimpleName(); protected IPlayerView viewDelegate; protected Context context; protected MediaPlayer mediaPlayer; protected volatile boolean shouldBePlaying; // Probably the worst race condition prone protected volatile boolean isPrepared; public BasicMediaPlayer(Context context, IPlayerView viewDelegate) { this.context = context; this.viewDelegate = viewDelegate; } @Override public void start() { if (getMediaPlayer().isPlaying()) return; try { shouldBePlaying = true; if (!isPrepared) { AssetFileDescriptor afd = context.getResources().openRawResourceFd(R.raw.wildfire); getMediaPlayer().setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength()); getMediaPlayer().prepareAsync(); } else { getMediaPlayer().start(); } } catch (IOException e) { Log.d(TAG, "Exception when starting" + e.getMessage()); } } protected MediaPlayer getMediaPlayer() { if (mediaPlayer == null) { mediaPlayer = new MediaPlayer(); mediaPlayer.setOnPreparedListener(this); mediaPlayer.setOnErrorListener(this); mediaPlayer.setOnBufferingUpdateListener(this); mediaPlayer.setOnInfoListener(this); mediaPlayer.setOnCompletionListener(this); mediaPlayer.setOnSeekCompleteListener(this); mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); } return mediaPlayer; } @Override public void pause() { shouldBePlaying = false; if (getMediaPlayer().isPlaying()) { getMediaPlayer().pause(); } } @Override public int getCurrentPosition() { return isPrepared ? getMediaPlayer().getCurrentPosition() : 0; } @Override public int getDuration() { return isPrepared ? getMediaPlayer().getDuration() : 0; } @Override public void seekTo(int percent) { getMediaPlayer().seekTo(mediaPlayer.getDuration() * percent); } @Override public boolean isPlaying() { return getMediaPlayer().isPlaying(); } @Override public void stop() { if (isPrepared) { getMediaPlayer().stop(); isPrepared = false; } } @Override public void release() { if (isPrepared) { getMediaPlayer().release(); } isPrepared = false; mediaPlayer = null; } @Override public void reset() { getMediaPlayer().reset(); isPrepared = false; } // Media Player Listeners @Override public void onBufferingUpdate(MediaPlayer mp, int percent) { // Nothing to do here since file is all in memory. } @Override public void onCompletion(MediaPlayer mp) { mp.reset(); } @Override public boolean onError(MediaPlayer mp, int what, int extra) { isPrepared = false; return false; } @Override public boolean onInfo(MediaPlayer mp, int what, int extra) { return false; } @Override public void onPrepared(MediaPlayer mp) { isPrepared = true; long hours = TimeUnit.MILLISECONDS.toHours(mp.getDuration()); Date date = new Date(mp.getDuration()); DateFormat formatter = new SimpleDateFormat(hours > 0 ? "HH:mm:ss" : "mm:ss"); viewDelegate.setDuration(formatter.format(date)); if (shouldBePlaying) { mp.start(); } } @Override public void onSeekComplete(MediaPlayer mp) { } }