0
votes

I'm creating application which will allow to play audio files to user, create multiple playlists and cross fade each song/playlist etc.

So far everything was working , I'm able to pick song files, and play them properly by loading them into MediaPlayer

Issue however is if I resetart application. Magically all Uri previously properly read are no longer accessible ?

This is how I load songs (some parts removed - views etc. )

val intent = new Intent()
        .setData(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI)
        .putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true)
        .setAction(Intent.ACTION_OPEN_DOCUMENT )
        .addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
        .addFlags(Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION)
        .addFlags(Intent.FLAG_GRANT_PREFIX_URI_PERMISSION);
startActivityForResult.launch(intent);
    private final ActivityResultLauncher<Intent> startActivityForResult = registerForActivityResult(
            new ActivityResultContracts.StartActivityForResult(),
            result -> {
                if (result.getResultCode() == RESULT_OK) {
                    val data = result.getData();
                    if (data.getData() != null) {
                        val uri = data.getData();
                        songOutOfUri(uri);
                    } else if (data.getClipData() != null && data.getClipData().getItemCount() > 0) {
                        val clipData = data.getClipData();
                        for (int i = 0; i < clipData.getItemCount(); i++) {
                            val uri = clipData.getItemAt(i).getUri();
                            songOutOfUri(uri);
                        }
                    }
                    ///...view update
                }
            }
    );

    private void songOutOfUri(Uri uri) {
        val file = new File(uri.getPath());
        val song = Song.builder()
                .filename(file.getName())
                .fileUri(uri.toString())
                .filePath(file.getPath())
                .build();
        song.save();
        playlistService.addSongToPlaylist(playlist, song);
    }

basically result of that intent are some songs added into my playlists , they containt filename ( for better display ) filepath and fileUri . All is persisted into DB using https://github.com/ImangazalievM/ReActiveAndroid ( hence uri to string )

I then try to play file :

///...
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(getApplicationContext(), Uri.parse(currentSong.getFileUri()));
mediaPlayer.prepare();
mediaPlayer.seekTo(position);
mediaPlayer.start();

It will work for multiple songs , but only until I kill /reset application If I reopen application once more , I receive media player error :

W/System.err: java.io.IOException: setDataSource failed.: status=0x80000000
W/System.err:     at android.media.MediaPlayer.nativeSetDataSource(Native Method)
        at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1243)
W/System.err:     at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1230)
        at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1147)
        at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1072)

Sample persisted song:

{
"id":20,
"filename":"05 Mindi Abair - Lucy's.mp3", 
"fileUri":"content://com.android.externalstorage.documents/document/primary%3AMusic%2Fsongs%2F05%20Mindi%20Abair%20-%20Lucy's.mp3", 
"filePath":"/document/primary:Music/songs/05 Mindi Abair - Lucy's.mp3", 
"currentPosition":0
}

Tried to also to to read is as file and play with fileDescriptor, but then got FileNotFound even at first try ( without reset )

Trying to play it before resetting with uri works, with file descriptor, doesn't

File file = new File(currentSong.getFilePath()); //also tried with Uri.parse(song.getFileUri)
FileInputStream is = new FileInputStream(file);
mediaPlayer.setDataSource(is.getFD());
//throws java.io.FileNotFoundException: /document/primary:Music/songs/05 Mindi Abair - Lucy's.mp3: open failed: ENOENT (No such file or directory)

Manifest of course has permission for read storage