Android MediaPlayer: playing an audio resource in Raw based on URI

The problem I'm trying to solve is the activity that should play audio files. Most files will be created by the user (and saved in external storage) and therefore will be played with the following code (based on the Google example code):

MediaPlayer mPlayer = new MediaPlayer();
mPlayer.setDataSource(filename);
mPlayer.prepare();
mPlayer.start();

Some audio files, however, will be included in the application, usually played with the following code:

MediaPlayer mPlayer = MediaPlayer.create(getBaseContext(), R.raw.filename);
mPlayer.prepare();
mPlayer.start();

The problem is that I would like to be able to play audio files in the source folder in the same way as I play user-created files, since I do not necessarily know what will be needed. I tried to get the URI of the audio file in the source folder and play it with the following code:

Uri uri = Uri.parse("android/resource://com.my.package/" + R.raw.filename);
MediaPlayer mPlayer = new MediaPlayer();
mPlayer.setDataSource(uri.toString());
mPlayer.prepare();
mPlayer.start();

But nothing happens. No error messages, no sound.

, , , . .

+4
3

, , .

mMediaPlayer = new MediaPlayer();
Uri mediaPath = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.filename);
try {
    mMediaPlayer.setDataSource(getApplicationContext(), mediaPath);
    mMediaPlayer.prepare();
    mMediaPlayer.start();
} catch (Exception e) {
    e.printStackTrace();
}

, . -, "/" "android.resource"

-, setDataSource (String) - , , . (. MediaPlayer.setDataSource(String), )

+3
final int[] song = {R.raw.letmeloveyou,R.raw.aarti,R.raw.answer};
final MediaPlayer mp = new MediaPlayer();

lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        try {
            if (true == mp.isPlaying()) {
                mp.stop();
                mp.reset();
                Toast.makeText(getBaseContext(), "Again Playing", Toast.LENGTH_LONG).show();
                mp.setDataSource(Result.this, Uri.parse("android.resource://" + getPackageName() + "/" + song[position]));
                mp.prepare();
                mp.start();
            } else {
                Toast.makeText(getBaseContext(), "Playing", Toast.LENGTH_LONG).show();
                mp.setDataSource(Result.this, Uri.parse("android.resource://" + getPackageName() + "/" + song[position]));
                mp.prepare();
                mp.start();
            }
        } catch (Exception e) {        
            Toast.makeText(getBaseContext(),e.toString(),Toast.LENGTH_LONG).show();
        }
    }
});
0

When I used toString () in Uriit failed, when I used direct Uriit played well.

Uri musicUri = Uri.parse("android.resource://" + context.getPackageName() + "/" + resourceID);
Log.d(TAG, "musicUri: " + musicUri);
mediaPlayer.setDataSource(context, musicUri);
0
source

Source: https://habr.com/ru/post/1545014/


All Articles