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();
startAudio(); // when calling startAudio from here its playing audio but when calling startAudio from button press then its not playing
} 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) {
if(!mediaPlayer.isPlaying())
{
mediaPlayer.start();
mediaPlayer.seekTo(audioLength);
}
}
});
}
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.