android – MediaRecorder – 记录调用应用程序
作者:互联网
我正在尝试开发录制通话的应用程序.
当我录制输出声音听起来非常有线 – 电子声音而不是
别人的声音.
这是我的代码:
public class MainActivity extends Activity implements OnClickListener {
private Boolean Recording;
private Button btn_REC;
private MediaRecorder mrec;
private File audiofile = null;
private static final String TAG = "SoundRecordingDemo";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Recording = false;
mrec = new MediaRecorder();
btn_REC = (Button) findViewById(R.id.btn_record);
btn_REC.setOnClickListener(this);
}
@Override
public void onClick(View v)
{
if (!Recording)
{
try
{
startRecording();
Recording = true;
}
catch (IOException e1)
{
e1.printStackTrace();
}
btn_REC.setText("RECORDING");
}
else
{
stopRecording();
btn_REC.setText("RECORD");
}
}
protected void startRecording() throws IOException {
mrec.setAudioSource(MediaRecorder.AudioSource.VOICE_DOWNLINK);
mrec.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mrec.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
if (audiofile == null) {
File sampleDir = Environment.getExternalStorageDirectory();
try {
audiofile = File.createTempFile("ibm", ".3gp", sampleDir);
} catch (IOException e) {
Log.e(TAG, "sdcard access error");
return;
}
}
mrec.setOutputFile(audiofile.getAbsolutePath());
mrec.prepare();
mrec.start();
}
protected void stopRecording() {
mrec.stop();
mrec.release();
processaudiofile();
}
protected void processaudiofile() {
ContentValues values = new ContentValues(3);
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));
}
}
当我试图将AudioSource更改为上行链路或语音呼叫时,它仍然是相同的.
当我把它定义为MIC时,所有工作都很好,但当我打电话时,这个奇怪的声音开始……
有什么想法吗?
谢谢!
解决方法:
使用mRecorder.setAudioSource(MediaRecorder.AudioSource.VOICE_CALL);
但是,由于Call Recording不合法,所以许多设备不支持(MediaRecorder.AudioSource.VOICE_CALL / MediaRecorder.AudioSource.VOICE_DOWNLINK),可以在某些设备上运行.
我在LG测试过它并且工作正常,但没有使用Nexus设备.
所以相反,它们使用所有设备都允许的MediaRecorder.AudioSource.MIC.
标签:android,audio-recording,mediarecorder 来源: https://codeday.me/bug/20191003/1850149.html