如何在 Android 上创建麦克风应用程序?

How can I create a microphone app on Android?

我想在 Android 上创建一个麦克风应用程序,它将通过麦克风接收声音并通过扬声器播放,但我不知道我应该使用哪些 类 和服务。

你的答案的核心是:

A) 记录和存储,如 here 所述。

MediaRecorder recorder = new MediaRecorder();    
String status = Environment.getExternalStorageState();
if(status.equals("mounted")){
    String path = Environment.getExternalStorageDirectory().toString()+"/YOURFOLDER"; // your custom path
    recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); // notice that this is the audio format, and you might want to change it to [other available audio formats][2]
    recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
    recorder.setOutputFile(path);
    recorder.prepare();

    // To start recording
    recorder.start();

    // To stop recording
    recorder.stop();
    recorder.release();

} else {
    // Handle the situation
}

[Other available audio formats | 2]

B) 获取录音,如部分所述here。然后你应该给他们看。

try {
    String path = Environment.getExternalStorageDirectory().toString()+"/YOURFOLDER"; // your custom path
    File directory = new File(path);
    File[] files = directory.listFiles();
} catch {
    // Handle errors (or maybe no files in the directory)
}

C) 播放录音,如部分所述here

MediaPlayer mp = new MediaPlayer();
mp.setDataSource(Environment.getExternalStorageDirectory().toString()+"/YOURFOLDER"+"/yourfilename.formatextension"; // your custom pathare to use the file format and directory you used when saving
mp.prepare();
mp.start();

希望这个回答对您有所帮助!