1

My problem is that my code was not working. i have 100+ mp3 store in my assets/anthem folder.

flAnthem = c.getString(c.getColumnIndex(KEY_ANTHEM));
flAnthemSDCard = c.getString(c.getColumnIndex(KEY_ANTHEM_SDCARD));

AssetManager assetManager= getAssets();
InputStream inputStream = null;
try{
    inputStream = assetManager.open("anthem/" + flAnthem);
    mediaPlayer.setDataSource(String.valueOf(inputStream));
    mediaPlayer.prepare();
    mediaPlayer.start();
}catch(IOException e){
    e.printStackTrace();
}
jopo
  • 23
  • 5

1 Answers1

1

Replace this

inputStream = assetManager.open("anthem/" + flAnthem);
mediaPlayer.setDataSource(String.valueOf(inputStream)); //DataSource is not correctly setted. 

By Using AssetFileDescriptor

flAnthem = c.getString(c.getColumnIndex(KEY_ANTHEM)) + ".mp3"; // don't forget extension 

AssetFileDescriptor descriptor = getAssets().openFd("anthem/"+mediaName);
mediaPlayer.setDataSource(descriptor.getFileDescriptor(), descriptor.getStartOffset(), descriptor.getLength());

BTW

Khaled Lela
  • 7,831
  • 6
  • 45
  • 73
  • Could you please explain **how** this will help with OP's issue? Why are you adding an extra layer of complexity by taking the ringer mode into account? And OP is obtaining a resource using an `AssetManager`, not a resource ID. – Michael Dodd Sep 03 '18 at 16:25
  • I am updating my question now to fix this, Thanks for your feedback – Khaled Lela Sep 03 '18 at 16:27
  • @MichaelDodd I fixed my answer using `AssetFileDescriptor`, **BTW** i created a sample app and that worked my me. – Khaled Lela Sep 03 '18 at 17:45
  • what is timber for? – jopo Sep 03 '18 at 18:03
  • @jopo `Timber` is a Logger library by **JakeWharton** https://github.com/JakeWharton/timber – Khaled Lela Sep 03 '18 at 19:10
  • @MichaelDodd Thanks for your kindly feedback about answer shall be describe the OP's problem and step for fixing, Not just some code or another way for fixing the issue *(Like i did)*, Your suggestion will provide more help for OP and Others when search for same issue, But i intend to add more info when have time as update. – Khaled Lela Sep 03 '18 at 19:33
  • i remove timber and change it into e.printStackTrace(); , but it works fine – jopo Sep 03 '18 at 19:44
  • @jopo Updated my answer by replacing `Timber` with `ex.printStackTrace();` Happy that helped you. – Khaled Lela Sep 03 '18 at 19:48
  • @MichaelDodd Simplify my answer and described OP's issue, Waiting for yours Vote up :) – Khaled Lela Sep 03 '18 at 20:16
  • 1
    @KhaledLela Duely noted, thanks for making Stack Overflow a more helpful place for future readers ;) – Michael Dodd Sep 03 '18 at 20:55
  • hello, last night my mp3 is working but now why still playing when i click the next button. – jopo Sep 04 '18 at 05:24
  • i didnt change my code but my music still playing when i pressed next button. last night my mp3 is work when i play next it stop and when i guessed it will play the music – jopo Sep 04 '18 at 05:32
  • @jopo If old media keep running after pressing next button, You just need to stop old `mediaPlayer.stop();` before start playing new media `mediaPlayer.start();` – Khaled Lela Sep 04 '18 at 10:03
  • Is it going to stop because my next button is at my dialog box different activity/java – jopo Sep 04 '18 at 12:55
  • If you don't have globe reference to `MediaPlayer`, Then next button switch activity it will stop, Feel free to ask another question if you still have an issue. – Khaled Lela Sep 04 '18 at 15:52