0

我正在尝试使用 AudioRecorde 录制音频。

虽然它创建文件但无法记录它。请提供一些输入。

private void startRecording(){
    recorder = new AudioRecord(MediaRecorder.AudioSource.MIC,44100,AudioFormat.CHANNEL_IN_MONO,AudioFormat.ENCODING_PCM_16BIT,AudioRecord.getMinBufferSize(44100, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT));


    try {
        recorder.startRecording();
    } catch (IllegalStateException e) {
        e.printStackTrace();
    } 
}

停止录制时

private void stopRecording(){

    if(null != recorder){
        recorder.stop();
        recorder.release();

        FileOutputStream fOut = null;
        try{
            int buffSize = AudioRecord.getMinBufferSize(44100, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT);
            //ByteBuffer buffer = ByteBuffer.allocate(AudioRecord.getMinBufferSize(44100, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT));
            byte b[] = new byte[buffSize];
            recorder.read(b,0,buffSize);
            //getFilename() will get the file
            fOut = new FileOutputStream(getFilename());
            fOut.write(b);
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            if(fOut != null){
                try {
                    fOut.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        recorder = null;
    }
}

它会创建一个带有 buffsize 的文件,但无法播放。

这是播放文件的代码

private void startPlaying() {
            mPlayer = new MediaPlayer();
            if(playing){
                playing = false;
            }else{
                try {
                    File myFile = new File(prevFile);
                    txtStatus.setText("Playing...");
                    imgStatus.setVisibility(View.VISIBLE);
                    imgStatus.setBackgroundDrawable(getResources().getDrawable(R.drawable.play_small));
                    if(myFile.exists()){
                        System.out.println("file is exist");
                    }else{
                        System.out.println("file is not exist");
                    }
                    mPlayer.setDataSource(prevFile);
                    System.out.println("file path is is ::> " + prevFile);
                    mPlayer.prepare();
                    mPlayer.start();


                    playing = true;
                } catch (IOException e) {
                    e.printStackTrace();
                    write.saveToFile(getApplicationContext(), "prepare() failed"+"\n");
                } 
            }
        }
4

0 回答 0