【问题标题】:Record only Audio with MediaRecorder使用 MediaRecorder 仅录制音频
【发布时间】:2020-03-13 01:59:01
【问题描述】:

我想用麦克风录制 .mp4 格式的音频文件。 这是我的代码:

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
    }
}

这是可行的,但生成的文件被视为视频文件。

我不明白为什么,我的后端需要音频文件而不是视频文件。

我可以做些什么来录制音频文件?谢谢!

【问题讨论】:

    标签: android mediarecorder


    【解决方案1】:

    您应该更改输出格式。它在文档中写道:

     /** 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;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-08
      • 1970-01-01
      • 1970-01-01
      • 2011-08-18
      相关资源
      最近更新 更多