I am following below post for creating simple music player, but selecting any song from list or pressing prev/next from MediaController is giving below error. I am using Oreo (Andoid 8.1.0 API 27).
public void playSong()
{
Song playSong = songs.get(songPosn);
songTitle=playSong.getTitle();
long currSong = playSong.getID();
Uri trackUri = ContentUris.withAppendedId(android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,currSong);
try
{
player.setDataSource(getApplicationContext(), trackUri);
player.prepareAsync();
}
catch(Exception e){
Log.e("MUSIC SERVICE", "Error setting data source", e);
}
}
Error MediaPlayer error: attachnewplayer called in state 64
Reference: Create Simple Music Player
You have to reset player, also have to set setAudioStreamType, so your code will look like below
try
{
player.reset();
player.setDataSource(getApplicationContext(), trackUri);
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
player.prepareAsync();
}
I think the key to an issue like this is to let people know the list of state:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With