Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 使用MediaPlayer播放MediaRecorder创建的AMR文件失败,muri为空_Android_Media Player_Mediarecorder_Amr - Fatal编程技术网

Android 使用MediaPlayer播放MediaRecorder创建的AMR文件失败,muri为空

Android 使用MediaPlayer播放MediaRecorder创建的AMR文件失败,muri为空,android,media-player,mediarecorder,amr,Android,Media Player,Mediarecorder,Amr,我正在编写一个应用程序,使用MediaRecorder以AMR格式录制麦克风中的声音,然后使用MediaPlayer播放数据 无论如何,这就是目标 我很有信心我的MediaRecorder能够正常工作,我正在以正确的数据速率在正确的位置生成数据文件。下面是我如何启动和停止我的MediaRecorder public void OnStartRecord(View v ) { System.out.println( "StartRecord"); try { aud

我正在编写一个应用程序,使用MediaRecorder以AMR格式录制麦克风中的声音,然后使用MediaPlayer播放数据

无论如何,这就是目标

我很有信心我的MediaRecorder能够正常工作,我正在以正确的数据速率在正确的位置生成数据文件。下面是我如何启动和停止我的MediaRecorder

public void OnStartRecord(View v )
{
    System.out.println( "StartRecord");

    try {
       audioFile = File.createTempFile("amrtmp", ".amr", getApplicationContext().getFilesDir());
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    System.out.println( "Recording to " + audioFile.getAbsolutePath());

    mRecorder = new MediaRecorder();
    mRecorder.setAudioSource(MediaRecorder.AudioSource.VOICE_COMMUNICATION);
    mRecorder.setOutputFormat(MediaRecorder.OutputFormat.AMR_NB);
    mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
    mRecorder.setAudioEncodingBitRate(4750);
    mRecorder.setAudioSamplingRate(8000);      
    mRecorder.setOutputFile(audioFile.getAbsolutePath());

    try {
        mRecorder.prepare();
    } catch (IllegalStateException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    mRecorder.start();
}

public void OnStopRecord(View v )
{
    System.out.println( "StopRecord");
    mRecorder.stop();
    mRecorder.release();
}
这很有魅力。典型的输出类似于

StartRecord
Recording to /data/data/com.test.playback/files/amrtmp-235967797.amr
当我开始录制,然后停止录制时,我可以看到文件已经创建,其中有一定数量的数据,与设置正确对应

旁注:运行时,我检测到扬声器发出奇怪的嗡嗡声。知道那是什么吗

然而,当我试图播放该文件时,我遇到了无尽的麻烦。我尝试了以下方法:

public void OnPlay(View v )
{
    MediaPlayer mPlayer = new MediaPlayer();
    mPlayer.setAudioStreamType(AudioManager.STREAM_VOICE_CALL);

    FileInputStream FIS = null;
    try {
        FIS = new FileInputStream(audioFile.getAbsolutePath());
        mPlayer.setDataSource(FIS.getFD());
        mPlayer.prepare();
    }
    catch( Exception e )        
    {
        e.printStackTrace();
    }
    mPlayer.start();
}
这导致MediaPlayer的以下输出完全不播放任何内容:

start()mURI为空

我也尝试了相同的代码,但设置mPlayer的数据源不同:

mPlayer.setDataSource(audioFile.getAbsolutePath());
当使用java.io.IOException状态0x1调用prepare时,此操作失败


我不得不想象我还需要用MediaPlayer做些别的事情来正确设置它。有什么建议吗?

请提供日志,以便我们能够帮助您-谢谢!