寻找使用AudioRecord

时间:2016-05-11 03:47:10

标签: android audio-recording

我正在尝试编写一个从麦克风中读取的应用程序以及我在同一应用程序中将结果/缓冲区自己写入我自己的文件(在单独的线程中)。相信我,我用谷歌搜索,直到我的手指麻木。但我对整个"回调"有问题。部分。

无论是onPeriodicNotification还是onMarkerReached部分,我似乎无法做到正确。不是在寻找任何人为我写的,但如果我能在网上找到合适的外壳,我可以自己做。

所以,如果有人知道一个好的例子,请。

1 个答案:

答案 0 :(得分:-1)

制作一个简单的声音管理器,您可以录制声音:

public class SoundManager {

private static final String TAG = SoundManager.class.getSimpleName();
public boolean isRecording = false;
private MediaRecorder recorder;
private String audioFileName = "sound";

private Context mContext;
private String storePath;

public SoundManager(Context context) {
    this.mContext = context;
}

public void onRecord(boolean toStart) {
    if (toStart) {
        try {
            startRecording();
            isRecording = true;
        } catch (IOException e) {
            Log.d("Tag", e.toString());
        }
    } else {
        stopRecording();
    }
}

private void startRecording() throws IOException {
    stopRecording();
    audioFileName = UUID.randomUUID().toString();
    storePath = new File(mContext.getExternalFilesDir(null), "/images/"+ audioFileName+".3gp").getAbsolutePath();
    recorder = new MediaRecorder();
    recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
    recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
    recorder.setOutputFile(storePath);

    recorder.prepare();
    recorder.start();
}

private void stopRecording() {
    if (isRecording && recorder != null) {
        try {
            recorder.stop();
            recorder.release();
        } catch (RuntimeException e) {
            recorder.release();
            Log.d(TAG, e.toString());
        }

        recorder = null;
        isRecording = false;
    }
}

public File getAudioOutputPath() {
    return new File(storePath);
}

}

<强>用途:

  1. 开始记录:soundManager.onRecord(true);
  2. 停止记录:soundManager.onRecord(false);
  3. 获取声音文件路径:soundManager.getAudioOutputPath();
  4. 在Manifest中添加音频权限:

    <uses-permission android:name="android.permission.RECORD_AUDIO" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    

    `