Search code examples
androidnullpointerexceptionmedia-player

I want to play wav file stored in external directory but I am not able to create MediaPlayer instance


I am writing a small app that creates wav files using a microphone. I am using AudioRecord class do get raw data and then I am saving it in wav format in external storage. The problem is I can not create MediaPlayer instance to saved wav files. I am able to open the selected file and I have got the permissions to read it. Wav files are saved correctly (at least I guess so because I can play then on computer). I would be grateful for any help :)

What I have got so far:

@Override
public void onRecordingClick(int position) {
    if (player == null) {
        String path = wavFilepathList.get(position);
        File audioFile = new File(path);

        Log.d("audioFileLog", "File exists: " + audioFile.exists() + ", can read: " + audioFile.canRead());

        player = new MediaPlayer();
        try {
            player.setDataSource(path);
            Log.d("audioFileLog", "Data Source Changed");
            player.setOnPreparedListener(this);
            Log.d("audioFileLog", "Listener Set, before prepare method");
            player.prepareAsync();
            Log.d("audioFileLog", "after prepare method");
        } catch (IOException e) {
            e.printStackTrace();
            Log.d("audioFileLog", "IOException when setting data source");
        }
    }
}

@Override
public void onPrepared(MediaPlayer mp) {
    mp.start();
    Log.d("audioFileLog", "after played method");
}

Logcat:

D/audioFileLog: File exists: true, can read: true
E/ExtMediaPlayer-JNI: env->IsInstanceOf fails
E/MediaPlayer-JNI: JNIMediaPlayerFactory: bIsQCMediaPlayerPresent 0
E/ExtMediaPlayer-JNI: env->IsInstanceOf fails
E/MediaPlayer-JNI: JNIMediaPlayerFactory: bIsQCMediaPlayerPresent 0
D/audioFileLog: Data Source Changed
D/audioFileLog: Listener Set, before prepare method
D/audioFileLog: after prepare method
E/MediaPlayer: error (1, -2147483648)
E/MediaPlayer: Error (1,-2147483648)

Older experiments:
1)

public void onRecordingClick(int position) {
    if (player == null) {

        File selectedFile = new File(wavFilepathList.get(position));

        Log.d("Main", "voice exists : " + selectedFile.exists() +
                ", can read : " + selectedFile.canRead());
        player = MediaPlayer.create(this, Uri.fromFile(selectedFile));
    }
    player.start();
} // on recording clicked

Logcat:

D/Main: voice exists : true, can read : true  
E/ExtMediaPlayer-JNI: env->IsInstanceOf fails  
E/MediaPlayer-JNI: JNIMediaPlayerFactory: bIsQCMediaPlayerPresent 0  
E/ExtMediaPlayer-JNI: env->IsInstanceOf fails  
E/MediaPlayer-JNI: JNIMediaPlayerFactory: bIsQCMediaPlayerPresent 0  
E/MediaPlayer: error (1, -2147483648)  
D/MediaPlayer: create failed:  

2)

@Override
public void onRecordingClick(int position) {
    if (player == null) {

        player= new MediaPlayer();
        try {
            player.setDataSource(wavFilepathList.get(position));

            player.setOnPreparedListener(this);
            player.prepareAsync();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
} // on recording clicked

@Override
public void onPrepared(MediaPlayer mp) {
    player.start();
}

Logcat:

E/ExtMediaPlayer-JNI: env->IsInstanceOf fails
E/MediaPlayer-JNI: JNIMediaPlayerFactory: bIsQCMediaPlayerPresent 0
E/ExtMediaPlayer-JNI: env->IsInstanceOf fails
E/MediaPlayer-JNI: JNIMediaPlayerFactory: bIsQCMediaPlayerPresent 0
E/MediaPlayer: error (1, -2147483648)
E/MediaPlayer: Error (1,-2147483648)  

Solution

  • file patch was incorrect hence the problem...