我尝试使用 MediaRecorder 来捕获音频。我看到 MediaRecorder 仅输出文件。我们输出了音频流还是可以使用任何库来访问麦克风?
P.S。抱歉我的英文。
答案 0 :(得分:0)
看看这里: record audio in android
public class SoundRecordingActivity extends Activity {
MediaRecorder recorder;
File audiofile = null;
private static final String TAG = "SoundRecordingActivity";
private View startButton;
private View stopButton;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
startButton = findViewById(R.id.start);
stopButton = findViewById(R.id.stop);
}
public void startRecording(View view) throws IOException {
startButton.setEnabled(false);
stopButton.setEnabled(true);
File sampleDir = Environment.getExternalStorageDirectory();
try {
audiofile = File.createTempFile("sound", ".3gp", sampleDir);
} catch (IOException e) {
Log.e(TAG, "sdcard access error");
return;
}
recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(audiofile.getAbsolutePath());
recorder.prepare();
recorder.start();
}
public void stopRecording(View view) {
startButton.setEnabled(true);
stopButton.setEnabled(false);
recorder.stop();
recorder.release();
addRecordingToMediaLibrary();
}
protected void addRecordingToMediaLibrary() {
ContentValues values = new ContentValues(4);
long current = System.currentTimeMillis();
values.put(MediaStore.Audio.Media.TITLE, "audio" + audiofile.getName());
values.put(MediaStore.Audio.Media.DATE_ADDED, (int) (current / 1000));
values.put(MediaStore.Audio.Media.MIME_TYPE, "audio/3gpp");
values.put(MediaStore.Audio.Media.DATA, audiofile.getAbsolutePath());
ContentResolver contentResolver = getContentResolver();
Uri base = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
Uri newUri = contentResolver.insert(base, values);
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, newUri));
Toast.makeText(this, "Added File " + newUri, Toast.LENGTH_LONG).show();
}
}