我一直在尝试使用MediaRecorder录制音频剪辑。我已经阅读了许多教程和文章,但是我的代码在我的Samsung S7手机上不起作用。该应用具有清单中的所有权限,可以写入外部存储并访问麦克风。该文件已创建,但长度为零,无论代码运行多长时间,大小通常仅为32个字节。
有人可以帮助我吗?这是我的代码:
var query = (from...
where...
select q).TakeRandom(1);
这是没有错误消息的日志:
private void createRecorder() {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO) == PackageManager.PERMISSION_GRANTED) {
recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
}
}
public void onRecorderClicked(View v) {
if (((ToggleButton) v).isChecked()) {
createRecorder();
if (recorder != null) {
final String dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getPath() + File.separator + getString(R.string.app_name);
final File folder = new File(dir);
if (!folder.exists()) {
folder.mkdirs();
}
final String fileName = dateFormat.format(new Date()) + ".3gp";
final String path = dir + File.separator + fileName;
recorder.setOutputFile(path);
try {
recorder.prepare();
} catch (IOException e) {
e.printStackTrace();
}
recorder.start();
MediaScannerConnection.scanFile(this,
new String[]{path}, new String[]{"audio/3gp"},
new MediaScannerConnection.OnScanCompletedListener() {
@Override
public void onScanCompleted(String path, Uri uri) {
Log.i("ExternalStorage", "Scanned " + path + ":");
Log.i("ExternalStorage", "-> uri=" + uri);
}
});
Toast.makeText(getApplicationContext(), "Recorder started: " + fileName, Toast.LENGTH_SHORT).show();
}
} else {
if (recorder != null) {
recorder.stop();
recorder.reset();
recorder.release();
recorder = null;
Toast.makeText(getApplicationContext(), "Recorder stopped", Toast.LENGTH_SHORT).show();
}
}
}