简体   繁体   English

我想播放存储在外部目录中的wav文件,但无法创建MediaPlayer实例

[英]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. 我正在编写一个使用麦克风创建WAV文件的小型应用程序。 I am using AudioRecord class do get raw data and then I am saving it in wav format in external storage. 我正在使用AudioRecord类来获取原始数据,然后以wav格式将其保存在外部存储中。 The problem is I can not create MediaPlayer instance to saved wav files. 问题是我无法创建MediaPlayer实例来保存WAV文件。 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). Wav文件已正确保存(至少我猜是这样,因为我可以在计算机上播放)。 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: 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) 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: 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) 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: 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)  

Try setting the mediaPlayer using the following way: 尝试使用以下方式设置mediaPlayer:

    try{
        File audioFile = new File("The File's Path");     //  audio.wav path
        if(audioFile.exists()){    
            Log.d("audioFileLog", "Found the file");
            MediaPlayer mp = new MediaPlayer();
            mp.setDataSource('audioFile.getPath());
            mp.prepare();
            mp.start();
        }else{
            Log.d("audioFileLog", "Did not find the file");
        }
    }catch(Exception ex){
        Log.d("audioFileLog", ex.toString());
    }

and then use: 然后使用:

mp.stop();
mp.release();

when you have finished. 完成后。

文件补丁不正确,因此出现了问题...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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