-1

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: Code

Debugger Output: Debugging Output

6
  • Can you show the structure of your project? Commented Feb 19, 2018 at 9:48
  • Try using setDataSource(filePath); method of MediaPlayer
    – Abu Yousuf
    Commented Feb 19, 2018 at 9:53
  • a sample file path would be /storage/sdcard0/Recordings/RECORDING_20180207_124312.m4a
    – Abu Yousuf
    Commented Feb 19, 2018 at 10:03
  • @AbuYousuf I have already tried this. Is there any issue with my URI? Commented Feb 19, 2018 at 10:14
  • i think the problem is the file path . if filePath is correct then it will work. You can just use your download file path.
    – Abu Yousuf
    Commented Feb 19, 2018 at 10:15

3 Answers 3

0

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")); 
3
  • Giving me this exception-> java.io.IOException: Prepare failed.: status=0x1 Commented Feb 19, 2018 at 10:29
  • Do you have this <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> in manifest.xml?, when is giving you the error? Is playing music at least? Commented Feb 19, 2018 at 10:31
  • Yes.. Its added. Commented Feb 19, 2018 at 11:37
0

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)

6
  • Thanks Abu Yousuf. Do you know how to download sound files from api to android app? Because i think there is issue in downloading files that is why it is returning null media player object. Commented Mar 12, 2018 at 8:02
  • Use Android System DownloadManager developer.android.com/reference/android/app/… . Check this androidtutorialpoint.com/networking/… tutorial
    – Abu Yousuf
    Commented Mar 12, 2018 at 8:21
  • Thanks. I am on it. Commented Mar 12, 2018 at 13:11
  • I downloaded(using some website URL) the sound file in android and successfully played it. Now I am looking for the Web Api method which gives me the URL(in Asp .Net Core). Can you help? Commented Mar 24, 2018 at 11:02
  • happy to hear that you successfully played files. "Web Api method which gives me the URL" would you explain it ?
    – Abu Yousuf
    Commented Mar 24, 2018 at 11:11
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.