Android 使用MediaRecorder仅录制音频
我想从麦克风录制.mp4格式的音频文件。 这是我的代码:Android 使用MediaRecorder仅录制音频,android,mediarecorder,Android,Mediarecorder,我想从麦克风录制.mp4格式的音频文件。 这是我的代码: fun startRecording() { recorder = MediaRecorder().apply { setAudioSource(MediaRecorder.AudioSource.MIC) setOutputFormat(MediaRecorder.OutputFormat.MPEG_4) setAudioEncoder(MediaRecorder.AudioEnc
fun startRecording() {
recorder = MediaRecorder().apply {
setAudioSource(MediaRecorder.AudioSource.MIC)
setOutputFormat(MediaRecorder.OutputFormat.MPEG_4)
setAudioEncoder(MediaRecorder.AudioEncoder.AAC)
setOutputFile(fileName)
try {
prepare()
start()
recordingListener?.invoke(Step.RECORDING, null)
} catch (e: Exception) {
recordingListener?.invoke(Step.ERROR, null)
stopRecorder()
}
}
}
private fun stopRecorder() {
recorder?.apply {
stop()
release()
isRecording = false
}
}
这是可行的,但生成的文件被视为视频文件
我不明白为什么,我的后端需要音频文件而不是视频文件
我可以做什么来录制为音频文件?谢谢 您应该更改输出格式。它在文档中写道:
/** The following formats are audio only .aac or .amr formats */
/**
* AMR NB file format
* @deprecated Deprecated in favor of MediaRecorder.OutputFormat.AMR_NB
*/
public static final int RAW_AMR = 3;
/** AMR NB file format */
public static final int AMR_NB = 3;
/** AMR WB file format */
public static final int AMR_WB = 4;
/** @hide AAC ADIF file format */
public static final int AAC_ADIF = 5;
/** AAC ADTS file format */
public static final int AAC_ADTS = 6;
/** @hide Stream over a socket, limited to a single stream */
public static final int OUTPUT_FORMAT_RTP_AVP = 7;
/** H.264/AAC data encapsulated in MPEG2/TS */
public static final int MPEG_2_TS = 8;
/** VP8/VORBIS data in a WEBM container */
public static final int WEBM = 9;