0

I create a record file in MP3 format, but this file does not run in Android 9 and above, while it runs in Android 7, another problem is that this file does not open with any other application on the Android phone, but when the file I upload the desired file to the server or run the desired file via USB in Windows. This file will run

MediaPlayer player = new MediaPlayer()
String filePath = Environment.getExternalStorageDirectory() + "/Audio/20210907144451.mp3"
try {
       if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
           player.setAudioAttributes(new AudioAttributes.Builder()
                 .setUsage(AudioAttributes.USAGE_MEDIA)
                 .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
                 .setLegacyStreamType(AudioManager.STREAM_MUSIC)
                 .build());
       } else {
             player.setAudioStreamType(AudioManager.STREAM_MUSIC);
       }

       //filePath: /storage/emulated/0/Audio/20210907144451.mp3

       player.setDataSource(filePath);
       player.prepare();
       player.start();
} catch (Exception ex) {
    LOG(ex.getMessage());
}

Permissions:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

Error text

Prepare failed.: status=0x1

1 Answers1

1

Try this:

MediaPlayer mediaPlayer = new MediaPlayer();
FileInputStream fis = null;
try {
    File directory = new File("/storage/emulated/0/Audio/20210907144451.mp3");
    fis = new FileInputStream(directory);
    mediaPlayer.setDataSource(fis.getFD());
    mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
    mediaPlayer.prepare();
}   finally {
    if (fis != null) {
        try {
            fis.close();
        } catch (IOException ignore) {
        }
    }

}
Khaby Lame
  • 228
  • 2
  • 16