I am having a shot at setting a mediaplayer as a singleton so that it can be controlled from various activities in my project. Much research has led me to the following code.
if (current_player == null)
{
try
{
current_player = MusicPlayer.getInstance();
AssetFileDescriptor afd = this.getResources().openRawResourceFd(R.raw.mytune);
current_player.setDataSource(afd.getFileDescriptor());
current_player.prepare();
current_player.setLooping(true);
current_player.start();
afd.close();
}
catch (IllegalArgumentException e) { Log.e("IllegalArgumentException", "error: " + e.getMessage(), e); }
catch (IllegalStateException e) {Log.e("IllegalStateException", "error: " + e.getMessage(), e); }
catch (IOException e) {Log.e("IOException", "error: " + e.getMessage(), e); }
catch (Exception e){ Log.e("Main media player", "error: " + e.getMessage(), e);
}
I do know that
1. the MediaPlayer is being created
2. the AssetFileDescriptor is for the correct file
The code fails at the .setDataSource line with an IOException
error: setDataSourceFD failed.: status=0x80000000
The code is deliberately similar to many examples shown on various forums that claim it should work.
To be clear I have proven that
current_player= MediaPlayer.create(this, R.raw.mytune);
works fine.
I would be very grateful for any suggestions.
Put the file in the directory assets. Сorrect line AssetFileDescriptor afd =… on