Android Mediarecorder录制的时候录制声音
时间: 2023-11-25 09:07:53 浏览: 117
要在Android Mediarecorder中录制声音,需要设置音频源,并且确保录制权限已被授予。可以使用以下代码来设置音频源:
```
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
```
在此例中,我们将音频源设置为麦克风。您可以将其替换为其他音频源,如MediaRecorder.AudioSource.CAMCORDER或MediaRecorder.AudioSource.VOICE_RECOGNITION,具体取决于您的需求。
请注意,录制音频还需要在AndroidManifest.xml文件中添加录制权限:
```
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
```
通过设置音频源和授予录制权限,您应该能够成功录制声音。
相关问题
Android Mediarecorder录制的时候屏蔽掉声音
要在Android的MediaRecorder录制过程中屏蔽掉声音,你可以通过设置MediaRecorder的AudioSource为MIC,然后将其Mute掉。具体的代码如下所示:
```
MediaRecorder recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
recorder.setOutputFile(filePath);
recorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setAudioChannels(1);
recorder.setAudioSamplingRate(44100);
recorder.setAudioEncodingBitRate(192000);
recorder.setVideoEncodingBitRate(512 * 1000);
recorder.setVideoFrameRate(30);
recorder.prepare();
recorder.start();
recorder.setVolume(0f);
```
在这段代码中,我们将MediaRecorder的AudioSource设置为MIC,然后使用setVolume函数将其静音。这样录制过程中就不会有声音了。
mediaRecorder录制系统声音
在 Android 平台上,要录制系统声音需要使用 MediaProjection API 和 AudioRecord API。
首先,你需要请求用户授权使用 MediaProjection API。可以通过以下代码启动授权对话框:
```java
private static final int REQUEST_MEDIA_PROJECTION = 1;
private MediaProjectionManager mMediaProjectionManager;
// 在 onCreate() 方法中初始化 mMediaProjectionManager
mMediaProjectionManager = (MediaProjectionManager) getSystemService(Context.MEDIA_PROJECTION_SERVICE);
// 启动授权对话框
startActivityForResult(mMediaProjectionManager.createScreenCaptureIntent(), REQUEST_MEDIA_PROJECTION);
```
当用户授权后,可以在 onActivityResult() 方法中获取到 MediaProjection 对象:
```java
private MediaProjection mMediaProjection;
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_MEDIA_PROJECTION && resultCode == RESULT_OK) {
mMediaProjection = mMediaProjectionManager.getMediaProjection(resultCode, data);
// 开始录制
startRecord();
}
}
```
接下来,可以使用 AudioRecord API 来录制系统声音。需要注意的是,录制系统声音需要 root 权限,或者使用系统签名的应用程序。
以下是录制系统声音的示例代码:
```java
private static final int SAMPLE_RATE = 44100;
private static final int CHANNEL_CONFIG = AudioFormat.CHANNEL_IN_MONO;
private static final int AUDIO_FORMAT = AudioFormat.ENCODING_PCM_16BIT;
private AudioRecord mAudioRecord;
private byte[] mBuffer;
private boolean mIsRecording;
private void startRecord() {
int minBufferSize = AudioRecord.getMinBufferSize(SAMPLE_RATE, CHANNEL_CONFIG, AUDIO_FORMAT);
mBuffer = new byte[minBufferSize];
mAudioRecord = new AudioRecord(MediaRecorder.AudioSource.REMOTE_SUBMIX, SAMPLE_RATE, CHANNEL_CONFIG, AUDIO_FORMAT, minBufferSize);
mAudioRecord.startRecording();
mIsRecording = true;
new Thread(new Runnable() {
@Override
public void run() {
FileOutputStream fos = null;
try {
fos = new FileOutputStream(new File(getExternalFilesDir(null), "record.pcm"));
while (mIsRecording) {
int readSize = mAudioRecord.read(mBuffer, 0, mBuffer.length);
if (readSize > 0) {
fos.write(mBuffer, 0, readSize);
fos.flush();
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}).start();
}
private void stopRecord() {
mIsRecording = false;
mAudioRecord.stop();
mAudioRecord.release();
mAudioRecord = null;
}
```
上述代码中使用了 AudioRecord.getMinBufferSize() 方法获取最小缓冲区大小,然后创建了一个 AudioRecord 对象,并使用 startRecording() 方法开始录制。
录制过程中,使用 read() 方法从 AudioRecord 中读取数据,然后将数据写入到本地文件中。
最后,使用 stop() 方法停止录制,并释放资源。
需要注意的是,上述代码中使用了 REMOTE_SUBMIX 作为录音源,这是 Android 4.4 及以上版本中新增的一种录音源,可以录制系统声音。
如果你的设备不支持 REMOTE_SUBMIX 录音源,可以尝试使用其他录音源,比如 MIC、VOICE_COMMUNICATION 等。不过,这些录音源可能无法录制系统声音,需要根据具体情况进行选择。
阅读全文
相关推荐
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)