0
MediaPlayer mPlayer;

String path = /storage/emulated/0/Audio/1429167704744.wav;
mPlayer = new MediaPlayer();
mPlayer = MediaPlayer.create(getActivity(), Uri.parse(path));
mPlayer.start();

Logs

04-20 13:04:09.667: E/AndroidRuntime(28202): Process: com.abs_ind.audio, PID: 28202 04-20 13:04:09.667: E/AndroidRuntime(28202): java.lang.NullPointerException: Attempt to invoke virtual method 'void android.media.MediaPlayer.start()' on a null object reference 
04-20 13:04:09.667: E/AndroidRuntime(28202):at com.bb_ind.test.Browse_Fragment.show_dialog(Browse_Fragment.java:71)

I'm getting NullPointerException when i start the Mediaplayer. I'm sure the file path is correct and the file is not corrupted. Thanks in advance .

Mattia Maestrini
  • 32,270
  • 15
  • 87
  • 94
Sai
  • 15,188
  • 20
  • 81
  • 121

2 Answers2

1

Try not to specify the path directly, use the helper of Environment to get your file path. Declare the permissions in your manifest file

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

Then try the following code to play your audio file.

    final String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC)
            + File.separator + "1429167704744.wav";

    MediaPlayer mediaPlayer = MediaPlayer.create(this, Uri.parse(path));
    try {
        mediaPlayer.prepare();
    } catch (IOException e) {
        e.printStackTrace();
    }
    mediaPlayer.start();
alijandro
  • 11,627
  • 2
  • 58
  • 74
0

You could try with this code:

MediaPlayer mp = new MediaPlayer(); mp.setDataSource("/mnt/sdcard/yourdirectory/youraudiofile.wav"); mp.prepare(); mp.start();

And did you added the following permission line in your manifest?

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

Gabriella Angelova
  • 2,985
  • 1
  • 17
  • 30