如何在android中录制语音?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (15)

我试图在android中录制声音但它会在路径(sdcard /文件名)上创建.mp3文件但是当我运行此文件时,它不会播放,因为它不录制声音。

这是我的代码:

public void onClick(View v) {
        // TODO Auto-generated method stub
        switch(v.getId()){
        case(R.id.Button01):
            try {
                //audio.start();
                startRecord();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        case(R.id.Button02):
            //audio.stop();
            stopRecord();
        }

    }
     private void startRecord() throws IllegalStateException, IOException{
           // recorder = new MediaRecorder(); 
            recorder.setAudioSource(MediaRecorder.AudioSource.MIC);  //ok so I say audio source is the microphone, is it windows/linux microphone on the emulator? 
            recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); 
            recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); 
            recorder.setOutputFile("/sdcard/Music/"+System.currentTimeMillis()+".amr"); 
            recorder.prepare(); 
            recorder.start();      
        }

        private void stopRecord(){
            recorder.stop();
          //recorder.release();
        }



}

清单文件:

<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
提问于
用户回答回答于

请参阅Android Audio Capture文档以记录音频并播放录制的音频。

用户回答回答于
你可以使用这样的代码:
import java.io.File;
import java.io.IOException;

import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.os.Environment;

public class AudioRecorder {

    final MediaRecorder recorder = new MediaRecorder();
    public final String path;

    public AudioRecorder(String path) {
        this.path = sanitizePath(path);
    }

    private String sanitizePath(String path) {
        if (!path.startsWith("/")) {
            path = "/" + path;
        }
        if (!path.contains(".")) {
            path += ".3gp";
        }
        return Environment.getExternalStorageDirectory().getAbsolutePath()
                + path;
    }

    public void start() throws IOException {
        String state = android.os.Environment.getExternalStorageState();
        if (!state.equals(android.os.Environment.MEDIA_MOUNTED)) {
            throw new IOException("SD Card is not mounted.  It is " + state
                    + ".");
        }

        // make sure the directory we plan to store the recording in exists
        File directory = new File(path).getParentFile();
        if (!directory.exists() && !directory.mkdirs()) {
            throw new IOException("Path to file could not be created.");
        }

        recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
        recorder.setOutputFile(path);
        recorder.prepare();
        recorder.start();
    }

    public void stop() throws IOException {
        recorder.stop();
        recorder.release();
    }

    public void playarcoding(String path) throws IOException {
        MediaPlayer mp = new MediaPlayer();
        mp.setDataSource(path);
        mp.prepare();
        mp.start();
        mp.setVolume(10, 10);
    }
}

扫码关注云+社区