Skip to content

Instantly share code, notes, and snippets.

@engr-erum
Created January 2, 2018 05:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save engr-erum/190b78af3fefb5d998486f49240143be to your computer and use it in GitHub Desktop.
Save engr-erum/190b78af3fefb5d998486f49240143be to your computer and use it in GitHub Desktop.
public void playAudio() {
if (mediaPlayer == null) {
mediaPlayer = new MediaPlayer();
}
if (!TextUtils.isEmpty(mediaFileUrl)) {
try {
mediaPlayer.reset();
mediaPlayer.setDataSource(mediaFileUrl);
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.prepare();
setButtons();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private void setButtons()
{
if (mediaPlayer != null) {
if (!mediaPlayer.isPlaying()) {
ivActionPlayVideo.setVisibility(View.VISIBLE);
ivActionPlayVideo.setImageDrawable(ContextCompat.getDrawable(AudioDetailActivity.this, android.R.drawable.ic_media_pause));
if (audioLength > 0) {
mediaPlayer.seekTo(audioLength);
}
mediaPlayer.start();
ivViewTracker.setVisibility(View.GONE);
ivRecordingGifMic.setVisibility(View.VISIBLE);
} else {
ivActionPlayVideo.setVisibility(View.VISIBLE);
ivActionPlayVideo.setImageDrawable(ContextCompat.getDrawable(AudioDetailActivity.this, android.R.drawable.ic_media_play));
if (mediaPlayer != null && mediaPlayer.isPlaying()) {
mediaPlayer.pause();
audioLength = mediaPlayer.getCurrentPosition();
}
ivViewTracker.setVisibility(View.VISIBLE);
ivRecordingGifMic.setVisibility(View.GONE);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment