Skip to content

Instantly share code, notes, and snippets.

@engr-erum
Created November 15, 2017 11:35
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/7697d0084a3706d5c75a56a7f9608294 to your computer and use it in GitHub Desktop.
Save engr-erum/7697d0084a3706d5c75a56a7f9608294 to your computer and use it in GitHub Desktop.
playAudio();
ivActionPlayVideo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (mediaType == 1) {
if (!mediaPlayer.isPlaying()) {
videoView.setVisibility(View.INVISIBLE);
ivViewTracker.setVisibility(View.VISIBLE);
ivActionPlayVideo.setVisibility(View.VISIBLE);
ivActionPlayVideo.setImageDrawable(ContextCompat.getDrawable(DetailActivityAudio.this, android.R.drawable.ic_media_pause));
startAudio();
} else {
videoView.setVisibility(View.GONE);
ivViewTracker.setVisibility(View.VISIBLE);
ivActionPlayVideo.setVisibility(View.VISIBLE);
ivViewTracker.setBackgroundResource(R.drawable.audio_detail_place_holder);
ivActionPlayVideo.setImageDrawable(ContextCompat.getDrawable(DetailActivityAudio.this, android.R.drawable.ic_media_play));
if (mediaPlayer != null) {
mediaPlayer.pause();
audioLength = mediaPlayer.getCurrentPosition();
}
}
}
}
});
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();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} else {
Log.e(DetailActivityAudio.class.getName(), "mediaFileUrl:" + mediaFileUrl);
}
}
private void startAudio() {
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
//mp.seekTo(audioLength);
mp.start();
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment