如何在 android 设备中录制内部音频 [root/no root]
Posted
技术标签:
【中文标题】如何在 android 设备中录制内部音频 [root/no root]【英文标题】:How to record internal audio [root/no root] in android devices 【发布时间】:2020-01-27 07:44:43 【问题描述】:我在录制屏幕表面时使用并尝试过 MediaRecorder.Audiosource.REMOTE_SUBMIX,由于权限问题,应用程序崩溃,在 HTC 10 中测试[根植于 superSu 中]。
虽然它适用于以下输入:
mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
我想在不使用麦克风的情况下录制系统的内部/输出音频。 要求: 我需要记录屏幕的活动以及音频(不使用麦克风), 当屏幕录像机打开时,如果播放 youtube,我需要记录它的音频以及屏幕活动(不使用麦克风)。我只面临音频部分的挑战,请建议
private void initRecorder()
try
mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.REMOTE_SUBMIX);
mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);
mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mMediaRecorder.setOutputFile(Environment
.getExternalStoragePublicDirectory(Environment
.DIRECTORY_DOWNLOADS) + "/video.mp4");
mMediaRecorder.setVideoSize(DISPLAY_WIDTH, DISPLAY_HEIGHT);
mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mMediaRecorder.setVideoEncodingBitRate(512 * 1000);
mMediaRecorder.setVideoFrameRate(30);
int rotation = getWindowManager().getDefaultDisplay().getRotation();
int orientation = ORIENTATIONS.get(rotation + 90);
mMediaRecorder.setOrientationHint(orientation);
mMediaRecorder.prepare();
catch (IOException e)
e.printStackTrace();
开始录制会弹出一个权限对话框,同时创建以下Logcat条目
2020-01-27 12:38:19.463 3958-3958/com.truiton.screencapture E/MediaRecorder: prepare failed: -2147483648
单击立即开始(用户权限)会使应用程序崩溃并显示以下 logcat 输出
2020-01-27 12:55:09.174 12787-12787/com.truiton.screencapture E/MediaRecorder: start called in an invalid state: 0
2020-01-27 12:55:09.178 12787-12787/com.truiton.screencapture E/MediaRecorder: Failed to start recording
java.lang.IllegalStateException
at android.media.MediaRecorder._start(Native Method)
at android.media.MediaRecorder.start(MediaRecorder.java:1063)
at com.truiton.screencapture.MainActivity.onActivityResult(MainActivity.java:122)
at android.app.Activity.dispatchActivityResult(Activity.java:7246)
at android.app.ActivityThread.deliverResults(ActivityThread.java:4577)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:4631)
at android.app.ActivityThread.-wrap19(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1699)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:180)
at android.app.ActivityThread.main(ActivityThread.java:6944)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:835)
2020-01-27 12:55:09.183 12787-12787/com.truiton.screencapture E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.truiton.screencapture, PID: 12787
java.lang.RuntimeException: Failure delivering result ResultInfowho=null, request=1000, result=-1, data=Intent (has extras) to activity com.truiton.screencapture/com.truiton.screencapture.MainActivity: java.lang.IllegalStateException
at android.app.ActivityThread.deliverResults(ActivityThread.java:4581)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:4631)
at android.app.ActivityThread.-wrap19(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1699)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:180)
at android.app.ActivityThread.main(ActivityThread.java:6944)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:835)
Caused by: java.lang.IllegalStateException
at android.media.MediaRecorder._start(Native Method)
at android.media.MediaRecorder.start(MediaRecorder.java:1063)
at com.truiton.screencapture.MainActivity.onActivityResult(MainActivity.java:122)
at android.app.Activity.dispatchActivityResult(Activity.java:7246)
at android.app.ActivityThread.deliverResults(ActivityThread.java:4577)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:4631)
at android.app.ActivityThread.-wrap19(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1699)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:180)
at android.app.ActivityThread.main(ActivityThread.java:6944)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:835)
【问题讨论】:
【参考方案1】:是否添加权限: android.Manifest.permission.CAPTURE_AUDIO_
【讨论】:
以上是关于如何在 android 设备中录制内部音频 [root/no root]的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 Auriotouch 将麦克风的音频输入静音并仅识别设备的内部音频
如何使用robolectric对Android音频录制应用进行单元测试
音频处理使用 Adobe Audition 录制电脑内部声音 ( 启用电脑立体声混音 | Adobe Audition 中设置音频设备 | Adobe Audition 内录 )
使用 MediaRecorder.AudioSource.VOICE_COMMUNICATION 录制的音频在某些搭载 Android 10 的设备上为空