Created
August 21, 2017 21:57
-
-
Save nazmulidris/e48b67f137f81e7c7a2cdd2576849407 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void initializeMediaPlayer() { | |
if (mMediaPlayer == null) { | |
mMediaPlayer = new MediaPlayer(); | |
mMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { | |
@Override | |
public void onCompletion(MediaPlayer mediaPlayer) { | |
stopUpdatingCallbackWithPosition(true); | |
logToUI("MediaPlayer playback completed"); | |
if (mPlaybackInfoListener != null) { | |
mPlaybackInfoListener.onStateChanged(PlaybackInfoListener.State.COMPLETED); | |
mPlaybackInfoListener.onPlaybackCompleted(); | |
} | |
} | |
}); | |
} | |
} | |
@Override | |
public void play() { | |
if (mMediaPlayer != null && !mMediaPlayer.isPlaying()) { | |
logToUI(String.format("playbackStart() %s", | |
mContext.getResources().getResourceEntryName(mResourceId))); | |
mMediaPlayer.start(); | |
if (mPlaybackInfoListener != null) { | |
mPlaybackInfoListener.onStateChanged(PlaybackInfoListener.State.PLAYING); | |
} | |
startUpdatingCallbackWithPosition(); | |
} | |
} | |
@Override | |
public void pause() { | |
if (mMediaPlayer != null && mMediaPlayer.isPlaying()) { | |
mMediaPlayer.pause(); | |
if (mPlaybackInfoListener != null) { | |
mPlaybackInfoListener.onStateChanged(PlaybackInfoListener.State.PAUSED); | |
} | |
} | |
} | |
@Override | |
public void reset() { | |
if (mMediaPlayer != null) { | |
logToUI("playbackReset()"); | |
mMediaPlayer.reset(); | |
loadMedia(mResourceId); | |
if (mPlaybackInfoListener != null) { | |
mPlaybackInfoListener.onStateChanged(PlaybackInfoListener.State.RESET); | |
} | |
stopUpdatingCallbackWithPosition(true); | |
} | |
} | |
@Override | |
public void loadMedia(int resourceId) { | |
mResourceId = resourceId; | |
initializeMediaPlayer(); | |
AssetFileDescriptor assetFileDescriptor = | |
mContext.getResources().openRawResourceFd(mResourceId); | |
try { | |
mMediaPlayer.setDataSource(assetFileDescriptor); | |
} catch (Exception e) { | |
logToUI(e.toString()); | |
} | |
try { | |
mMediaPlayer.prepare(); | |
} catch (Exception e) { | |
logToUI(e.toString()); | |
} | |
initializeProgressCallback(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment