Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - MediaPlayer error: attachnewplayer called in state 64

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

like image 626
Arpit Sharma Avatar asked Apr 18 '18 17:04

Arpit Sharma


2 Answers

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();
}
like image 144
vijay22uk Avatar answered Sep 21 '22 14:09

vijay22uk


I think the key to an issue like this is to let people know the list of state:

  • 1: ?
  • 64: ?
  • 128: PlaybackCompleted. I encounter 128, after play was done, then stop and reset are not called before setDataSource is called. I know the sequence of stop, reset, setDataSource, prepareAsync, start was working.
  • ?: ?
like image 45
user1462586 Avatar answered Sep 21 '22 14:09

user1462586