简体   繁体   中英

Unable to find sound files URI for Media Player

I want to play sound files when user click on certain text. I have sound files list in my database and the sound files are downloading successfully in my app. I am trying to play these files using MediaPlayer . The issue is I cannot find the URI required to play MediaPlayer . Thus resulting in null media-player object. I have tried nearly all the solutions listed on internet but non solved my issue. The path of sound file while downloading from database is

"/data/user/0/mypackage/files/Sound/diabetes_ur_sound_1_1_1_1.0.0.0.mp3".

Screenshot of one of the method that I used is attached with the debugger output.

Code:代码

Debugger Output:调试输出

You can use setDataSource(Context context, Uri uri) method. If your path is correct you can pass it as a parameter.

Example:

try {
        mp.setDataSource(getApplicationContext(), myUri1);
} catch (IllegalArgumentException e) {
        Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
} catch (SecurityException e) {
        Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
} catch (IllegalStateException e) {
        Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
} catch (IOException e) {
        e.printStackTrace();
}

Take a look on this tutorial

Edit

First of all try if it's working hardcoded and then try to get the path dynamically.

Try this with your path

mp.setDataSrouce(MainActivity.this, Uri.parse("/data/user/0/mypackage/files/Sound/diabetes_ur_sound_1_1_1_1.0.0.0.mp3")); 

Make sure your file is exist in

getFilesDir().getPath()+"/Sound/"

directory. I think your file is missing in that directory. If your file is exist in that directory then it should work. MediaPlayer null pointer exception may occur if file is not found. Try checking your file exist before creating MediaPlayer . If that doesn't work create file in other directory like

getExternalFilesDir(Environment.DIRECTORY_MUSIC)

The issue was in downloading sound files. Therefore the MediaPlayer object results in null.

Now I am downloading the sound with help of static files and using the file path in MediaPlayer. Problem Solved

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM