MediaRecorder

MediaRecorder

Used to record audio and video.用来音视频的录制与播放,状态图如下:

这里写图片描述

A common case of using MediaRecorder to record audio works as follows,使用流程如下:

 // initial
 MediaRecorder recorder = new MediaRecorder();
 // initialized
 recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    AudioSource
        DEFAULT Default audio source
        MIC Microphone audio source
        REMOTE_SUBMIX   Audio source for a submix of audio streams to be presented remotely.
        VOICE_CALL  Voice call uplink + downlink audio source
        VOICE_COMMUNICATION Microphone audio source tuned for voice communications such as VoIP
        VOICE_RECOGNITION   Microphone audio source tuned for voice recognition if available, behaves like DEFAULT otherwise.


 // config
 recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        // 输出格式
        AAC_ADTS    AAC ADTS file format
        AMR_NB  AMR NB file format
        AMR_WB  AMR WB file format
        DEFAULT 
        MPEG_4  MPEG4 media file format
        RAW_AMR This constant was deprecated in API level 16. Deprecated in favor of MediaRecorder.OutputFormat.AMR_NB
        THREE_GPP   3GPP media file format
        WEBM    VP8/VORBIS data in a WEBM container




 recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
    // 音频编码格式
    AAC AAC Low Complexity (AAC-LC) audio codec
    AAC_ELD Enhanced Low Delay AAC (AAC-ELD) audio codec
    AMR_NB  AMR (Narrowband) audio codec
    AMR_WB  AMR (Wideband) audio codec
    DEFAULT HE_AAC  High Efficiency AAC (HE-AAC) audio codec
    VORBIS  Ogg Vorbis audio codec


 recorder.setOutputFile(PATH_NAME);
 // prepare
 recorder.prepare();
 // recording
 recorder.start();   // Recording is now started
 ...
 recorder.stop();
 recorder.reset();   // You can reuse the object by going back to setAudioSource() step
 recorder.release(); // Now the object cannot be reused

使用的时候最好根据上图的流程,不然会报一些异常:

public void setAudioEncoder (int audio_encoder)

Added in API level 1
Sets the audio encoder to be used for recording. If this method is not called, the output file will not contain an audio track. Call this after setOutputFormat() but before prepare().

Parameters
audio_encoder   the audio encoder to use.
Throws
IllegalStateException   if it is called before setOutputFormat() or after prepare().
要注意调用顺序
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值