Android MediaPlayer: Play Audio Resource in Raw Based on URI

随声附和 提交于 2021-02-18 12:01:22

问题


The problem I am trying to solve is in an activity that needs to play back audio files. Most of the files will be user created (and saved into external storage), and therefore played with the following code (based on Google's example code):

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

Some of the audio files, though, are going to be included with the app, are usually played with the following code:

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

The issue is that I would like to be able to play the audio files in the raw folder in the same way that I am playing the user created files since I don't necessarily know which will be needed. I tried tried to get the URI of the audio file in the raw 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 playing audio.

I outlined what I thought was the simplest solution, but I am willing to go another route if it accomplishes the task. Any assistance is greatly appreciated.


回答1:


I've managed to get it working, here is the code I used.

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();
}

I see two problems I see with your code. The first is that "android/resource" needs to become "android.resource"

The second is that setDataSource(String) won't work in this case due to the fact that you need context to use raw files as otherwise it tries to open the file incorrectly. (See the top answer in MediaPlayer.setDataSource(String) not working with local files)




回答2:


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();
        }
    }
});



回答3:


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

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


来源:https://stackoverflow.com/questions/24276451/android-mediaplayer-play-audio-resource-in-raw-based-on-uri

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!