使用媒体记录器在android中录制音频

时间:2015-12-20 07:15:15

标签: java android android-mediarecorder voice-recording

我使用录音机课程录制语音并将其上传到网络服务器,但我不知道录制语音文件何时完全写在SD卡上。

你知道一个界面可以帮助我找出文件是否在SD卡上完整保存。

2 个答案:

答案 0 :(得分:0)

你不是自己停止录音吗?好吧,如果我遇到像你那样的情况,我会怎么做。

  1. 创建MediaRecorder对象并设置以下属性:

    private MediaRecorder myAudioRecorder; 
    String outputFile = Environment.getExternalStorageDirectory().getAbsolutePath() + "/recording.3gp";
    myAudioRecorder=new MediaRecorder();
    myAudioRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    myAudioRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
    myAudioRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
    myAudioRecorder.setOutputFile(outputFile);
    
  2. 点击记录按钮调用这些方法开始录制:

    record.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            try {
                myAudioRecorder.prepare();
                myAudioRecorder.start();
            } catch (IllegalStateException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            Toast.makeText(getApplicationContext(), "Recording started", Toast.LENGTH_LONG).show();
        }
    });
    
  3. 单击停止按钮停止录制,然后上传文件:

    stop.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            myAudioRecorder.stop();
            myAudioRecorder.release();
            myAudioRecorder  = null;
    
            Toast.makeText(getApplicationContext(), "Audio recorded successfully",Toast.LENGTH_LONG).show();
    
            doUpload(outputFile); //call the method to upload your file and perform upload.
        }
    });
    
  4. 只要停止mediaRecorder并将其释放,录制的文件就会保存在存储卡中,因此您无需担心。只需在此之后立即上传您的文件。

答案 1 :(得分:0)

请看下面的语音记录代码

public class AudioRecorder {

    private boolean singleFile = true;

    public MediaRecorder recorder;

    private ArrayList<String> files = new ArrayList<String>();

    private String fileDirectory;

    private String finalAudioPath;

    private boolean isRecording;

    public boolean isStarted;

    public boolean isRecording() {
        return isRecording;
    }

    public String getAudioFilePath() {
        return finalAudioPath;
    }

    public AudioRecorder(String audioFileDirectory) {
        this.fileDirectory = audioFileDirectory;

        if (!this.fileDirectory.endsWith("/")) {
            this.fileDirectory += "/";
        }

        newRecorder();
    }

    public boolean start() {
        prepareRecorder();

        try {
            recorder.prepare();
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }

        if (recorder != null) {
            recorder.start();
            isRecording = true;
            isStarted = true;
            return true;
        }

        return false;
    }

    public boolean pause() {
        if (recorder == null || !isRecording) {
            throw new IllegalStateException("[AudioRecorder] recorder is not recording!");
        }

        recorder.stop();
        recorder.release();
        recorder = null;

        isRecording = false;

        return merge();
    }

    public boolean resume() {
        if (isRecording) {
            throw new IllegalStateException("[AudioRecorder] recorder is recording!");
        }

        singleFile = false;
        newRecorder();
        return start();
    }

    public boolean stop() {
        if (!isRecording) {
            return merge();
        }

        if (recorder == null) {
            return false;
        }

        recorder.stop();
        recorder.release();
        recorder = null;
        isRecording = false;
        isStarted = false;
        return merge();
    }

    public void clear() {
        if (recorder != null || isRecording) {
            recorder.stop();
            recorder.release();
            recorder = null;
            isRecording = false;
        }
        for (int i = 0, len = files.size(); i < len; i++) {
            File file = new File(this.files.get(i));
            file.delete();
        }
        finalAudioPath = "";
    }

    private boolean merge() {

        // If never paused, just return the file
        if (singleFile) {
            this.finalAudioPath = this.files.get(0);
            return true;
        }

        // Merge files
        String mergedFilePath = this.fileDirectory + System.currentTimeMillis() + ".amr";
        try {
            FileOutputStream fos = new FileOutputStream(mergedFilePath);

            for (int i = 0, len = files.size(); i < len; i++) {

                try {
                    File file = new File(this.files.get(i));
                    FileInputStream fis = new FileInputStream(file);

                    // Skip file header bytes,
                    // amr file header's length is 6 bytes
                    if (i > 0) {
                        for (int j = 0; j < 6; j++) {
                            fis.read();
                        }
                    }

                    byte[] buffer = new byte[512];
                    int count = 0;
                    while ((count = fis.read(buffer)) != -1) {
                        fos.write(buffer, 0, count);
                    }

                    fis.close();
//                    fos.flush();
//                    file.delete();

                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            fos.flush();
            fos.close();
//            files.clear();
            this.finalAudioPath = mergedFilePath;
//            files.add(this.finalAudioPath)
            return true;

        } catch (Exception e) {
            e.printStackTrace();
        }

        return false;
    }

    private void newRecorder() {
        recorder = new MediaRecorder();
    }

    private void prepareRecorder() {
        File directory = new File(this.fileDirectory);
        if (!directory.exists() || !directory.isDirectory()) {
            throw new IllegalArgumentException("[AudioRecorder] audioFileDirectory is a not valid directory!");
        }

        String filePath = directory.getAbsolutePath() + "/" + System.currentTimeMillis() + ".amr";
        this.files.add(filePath);

        recorder.setOutputFile(filePath);
        recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        recorder.setOutputFormat(MediaRecorder.OutputFormat.AMR_NB);
        recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
    }
}