Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.