johnsonsu/react-native-sound-player

Accessing android file from external storage based on content:// uri protocol

Malthehave opened this issue · 0 comments

Hi!

I'm using react-native-document-picker version 5.0.4, to get local files on the users device.

On Android, after choosing an audio file, it returns me an object like this:

{
  "fileCopyUri": "content://com.android.providers.media.documents/document/audio%3A31", 
  "name": "file_example_MP3_700KB.mp3", 
  "size": 764176, 
  "type": "audio/mpeg", 
  "uri": "content://com.android.providers.media.documents/document/audio%3A31"
}

Now my question is how I would play this audio file, stored on the users device, via the two paths being given.
I tried calling SoundPlayer.playSoundFile(content://com.android.providers.media.documents/document/audio%3A31, 'mp3'), but it throws this error:

image

It seems that react-native-sound-player can't read the file based on the content:// protocol.
I'm using react-native-sound-player version 0.10.9.

Code to reproduce:

import DocumentPicker from 'react-native-document-picker';
import SoundPlayer from 'react-native-sound-player'

const accessFile = async () => {
    try {
        const audioRes = await DocumentPicker.pick({
            type: [DocumentPicker.types.audio],
        });

        SoundPlayer.playSoundFile(audioRes.uri, 'mp3')        
    } catch (err) {
        if (DocumentPicker.isCancel(err)) {
            // User cancelled the picker, exit any dialogs or menus and move on
        } else {
            throw err;
        }
    }

}

I hope someone can point me in the right direction, as this has been troubling me for some time now :-)