Reputation: 319
In android 6.0 devices (not emulators api 23) I am receiving weird problem. Player starts playing song but after 1 or 2 seconds it again goes to 00:00 during this transition player skips the time required for transition and starts playing from that place eg(00:07).
But after 20+ seconds song starts playing normally for about 5 to 10 seconds and then player stops playing because the song is completed. Total time of this process is half than the duration of song.
What I think is there must be problem in preparing the music, but I am not sure what is the problem.
Here is the code what I have written so far
public static MediaPlayer player = null;
// above line is declared at start of class
// below code is written in a async task doInBackground
if (player != null) {
player.release();
player = null;
}
durationOfSong = 0;
String streamUrl = getSongsList.getStreamUrl(position);
player = MediaPlayer.create(context, Uri.parse(streamUrl));
try {
Log.d(LOG_TAG, "end time: " + player.getDuration());
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
mHandler.postDelayed(UpdateSong, 100);
player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
loadingSongProgress.setMessage("Buffering song...");
}
});
Manipulate manipulate = new Manipulate();
endTimingOfSong = manipulate.MillisecondsToFormat(player.getDuration());
} catch (NullPointerException e) {
Log.e(LOG_TAG, "NP: error: " + e.toString());
DialogWithNoSongError(view);
}
// and in post execute method
loadingSongProgress.dismiss();
player.start();
createMediaDialog(view,position);
// create media dialog is used to show image, song name and running time
Android devices running <6.0 OS and emulators (running 6.0) plays song in normal fashion. If more code is needed I will edit the post, in case needed please suggest. Thanks in advance!!
Upvotes: 0
Views: 1690
Reputation: 798
Remove the code the following code from the bottom
player.start();
and replace the following
player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
loadingSongProgress.setMessage("Buffering song...");
mHandler.postDelayed(UpdateSong, 100)
}
});
player.prepareAsync();
Try to run this it might help you
Upvotes: 1
Reputation: 1472
Try this. It worked for me.
player.setDataSource(getSongsList.getStreamUrl(position));
player.prepareAsync();
Note: There are two methods to prepare the media player: 1. player.prepare(); this one returns after completing the prepare operation hence causes a delay in the UI thread.
Upvotes: 0