Android 开发 MediaRecorder音频录制
<h1><span style="color: rgba(0, 128, 128, 1)">前言</span></h1><p> MediaRecorder类是Android sdk提供的一个专门用于音视频录制,一般利用手机麦克风采集音频和摄像头采集图像.这个类是属于简单的音频录制类,录制音频简单容易但是对音频流的控制也比较弱,这篇博客将只介绍音频的录制</p>
<h1><span style="color: rgba(0, 128, 128, 1)">实现流程</span></h1>
<ol>
<li>获取权限</li>
<li>实例化MediaRecorder</li>
<li>配置MediaRecorder</li>
<li>开启录音</li>
<li>停止录音</li>
<li>暂停录音与恢复录音</li>
<li>销毁释放</li>
</ol>
<h2><span style="color: rgba(0, 128, 128, 1)">获取权限</span></h2>
<div class="cnblogs_code">
<pre> <span style="color: rgba(0, 128, 0, 1)"><!--</span><span style="color: rgba(0, 128, 0, 1)">音频录制权限 </span><span style="color: rgba(0, 128, 0, 1)">--></span>
<span style="color: rgba(0, 0, 255, 1)"><</span><span style="color: rgba(128, 0, 0, 1)">uses-permission </span><span style="color: rgba(255, 0, 0, 1)">android:name</span><span style="color: rgba(0, 0, 255, 1)">="android.permission.RECORD_AUDIO"</span> <span style="color: rgba(0, 0, 255, 1)">/></span>
<span style="color: rgba(0, 128, 0, 1)"><!--</span><span style="color: rgba(0, 128, 0, 1)">读取和写入存储权限</span><span style="color: rgba(0, 128, 0, 1)">--></span>
<span style="color: rgba(0, 0, 255, 1)"><</span><span style="color: rgba(128, 0, 0, 1)">uses-permission </span><span style="color: rgba(255, 0, 0, 1)">android:name</span><span style="color: rgba(0, 0, 255, 1)">="android.permission.WRITE_EXTERNAL_STORAGE"</span> <span style="color: rgba(0, 0, 255, 1)">/></span>
<span style="color: rgba(0, 0, 255, 1)"><</span><span style="color: rgba(128, 0, 0, 1)">uses-permission </span><span style="color: rgba(255, 0, 0, 1)">android:name</span><span style="color: rgba(0, 0, 255, 1)">="android.permission.READ_EXTERNAL_STORAGE"</span> <span style="color: rgba(0, 0, 255, 1)">/></span></pre>
</div>
<p><strong>注意!在Android 5.0后需要动态授权</strong></p>
<h2><span style="color: rgba(0, 128, 128, 1)">实例化MediaRecorder</span></h2>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)"> private</span><span style="color: rgba(0, 0, 0, 1)"> MediaRecorder mMediaRecorder;
</span><span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> initMediaRecorer(){
mMediaRecorder </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> MediaRecorder();
}</span></pre>
</div>
<p>没啥好说的,就是new一个<span style="color: rgba(0, 0, 0, 1)">MediaRecorder</span></p>
<h2><span style="color: rgba(0, 128, 128, 1)">配置MediaRecorder</span></h2>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> configMediaRecorer(){
File demoAmrFile </span>= <span style="color: rgba(0, 0, 255, 1)">new</span> File(getExternalCacheDir(),"demo.amr"<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)"> (demoAmrFile.exists()){
demoAmrFile.delete();
}
mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">音频录入源</span>
mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.AMR_NB);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">录制音频的输出格式</span>
mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">录制音频的编码格式目前手机设备上可能就AMR_NB有用</span>
mMediaRecorder.setOutputFile(demoAmrFile.getAbsolutePath());<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">音频输出路径和文件名称 注意!这个设置是有顺序要求的,必需是上面编码格式设置完成后才能,写到前面会报错</span>
mMediaRecorder.setOnErrorListener(<span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> MediaRecorder.OnErrorListener() {
@Override
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span> onError(MediaRecorder mr, <span style="color: rgba(0, 0, 255, 1)">int</span> what, <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> extra) {
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">异常监听</span>
mMediaRecorder.stop();<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">暂停</span>
mMediaRecorder.release();<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">释放资源</span>
mMediaRecorder = <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">;
}
});
mMediaRecorder.setOnInfoListener(</span><span style="color: rgba(0, 0, 255, 1)">new</span> MediaRecorder.OnInfoListener() {<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">信息回调</span>
<span style="color: rgba(0, 0, 0, 1)"> @Override
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span> onInfo(MediaRecorder mr, <span style="color: rgba(0, 0, 255, 1)">int</span> what, <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> extra) {
}
});
}</span></pre>
</div>
<p><strong>注意!</strong>每一次录制音频前都需要配置一次.假设你录制暂停后,想重新录制新音频也需要重新执行一次这个方法,重新配置<span style="color: rgba(0, 0, 0, 1)">MediaRecorder</span>!</p>
<p><span style="color: rgba(255, 0, 0, 1)"><strong>再次注意!</strong> </span>配置参数是有顺序要求的的,没有按照顺序配置会报错,一般顺序是 > 配置音频源 > 配置音频输出格式 和 编码格式 > 配置其他参数(例如:文件大小限制/录制时间限制/音频比特率)> 配置文件保存路径 > 配置各种监听.<span style="color: rgba(0, 128, 0, 1)"><br></span></p>
<h2><span style="color: rgba(0, 128, 128, 1)">开启录音</span></h2>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> startRecorer(){
</span><span style="color: rgba(0, 0, 255, 1)">if</span> (mMediaRecorder != <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">){
</span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> {
mMediaRecorder.prepare();</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">准备</span>
mMediaRecorder.start();<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">开启</span>
} <span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (IOException e) {
e.printStackTrace();
}
}
}</span></pre>
</div>
<p><strong>注意!点击后,开始录制之前先配置一次</strong></p>
<div class="cnblogs_code">
<pre> mBtnStatr.setOnClickListener(<span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> View.OnClickListener() {
@Override
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> onClick(View v) {
configMediaRecorer();
startRecorer();
}
});</span></pre>
</div>
<h2><span style="color: rgba(0, 128, 128, 1)">暂停录音</span></h2>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> stopRecorer(){
</span><span style="color: rgba(0, 0, 255, 1)">if</span> (mMediaRecorder != <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">){
mMediaRecorder.stop();</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">暂停</span>
mMediaRecorder.reset();<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">重启到空闲状态 此方法调用后需要重新配置参数</span>
<span style="color: rgba(0, 0, 0, 1)"> }
}</span></pre>
</div>
<h2><span style="color: rgba(0, 128, 128, 1)">暂停录音与恢复录音</span></h2>
<p>暂停录制,注意这里是<span style="color: rgba(0, 0, 0, 1)">pause()方法,不是<span style="color: rgba(0, 0, 0, 1)">stop()</span></span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">private <span style="color: rgba(0, 0, 255, 1)">void<span style="color: rgba(0, 0, 0, 1)"> pauseRecorder(){
<span style="color: rgba(0, 0, 255, 1)">if (Build.VERSION.SDK_INT >=<span style="color: rgba(0, 0, 0, 1)"> Build.VERSION_CODES.N) {
mMediaRecorder.pause();<span style="color: rgba(0, 128, 0, 1)">//<span style="color: rgba(0, 128, 0, 1)">暂停
<span style="color: rgba(0, 0, 0, 1)"> }
}</span></span></span></span></span></span></span></span></pre>
</div>
<p>恢复录制</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">private <span style="color: rgba(0, 0, 255, 1)">void<span style="color: rgba(0, 0, 0, 1)"> resumeRecorder(){
<span style="color: rgba(0, 0, 255, 1)">if (Build.VERSION.SDK_INT >=<span style="color: rgba(0, 0, 0, 1)"> Build.VERSION_CODES.N) {
mMediaRecorder.resume();<span style="color: rgba(0, 128, 0, 1)">//<span style="color: rgba(0, 128, 0, 1)">恢复
<span style="color: rgba(0, 0, 0, 1)"> }
}</span></span></span></span></span></span></span></span></pre>
</div>
<h2><span style="color: rgba(0, 128, 128, 1)">销毁释放</span></h2>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> destroy(){
</span><span style="color: rgba(0, 0, 255, 1)">if</span> (mMediaRecorder != <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">){
mMediaRecorder.stop();
mMediaRecorder.release();</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">调用这个方法前必需先调用stop()方法</span>
mMediaRecorder = <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">;
}
}</span></pre>
</div>
<p><span style="color: rgba(0, 0, 0, 1)"><strong>请注意!</strong> 如果在start后马上执行stop方法可能会出现 RuntimeException的异常抛出。如果你有需求会被用户操作到连续点击录制,解决办法是增加操作延迟(比如点击延迟)与截获这个异常进行处理。</span></p>
<h1><span style="color: rgba(0, 128, 128, 1)">介绍有关音频录制的API</span></h1>
<p><strong><span style="color: rgba(0, 0, 0, 1)">关于开始/停止/暂停/恢复/重置/释放的方法我就不在这里说明了,上面的流程代码里有写注释.也说了它们的关键点.另外录制视频的相关API也不在这里介绍</span></strong></p>
<h3><span style="color: rgba(0, 128, 128, 1)">setAudioSource(<span style="font-weight: bold">int audio_source)</span></span></h3>
<p><span style="color: rgba(0, 0, 0, 1)">配置音频录入源,一般只需要选择MediaRecorder.AudioSource.MIC即可</span></p>
<h3><span style="color: rgba(0, 128, 128, 1)">setOutputFormat(<span style="font-weight: bold">int output_format)</span></span></h3>
<p><span style="color: rgba(0, 0, 0, 1)">设置录制音频的输出格式,在MediaRecorder.OutputFormat里有以下这些格式可以选择,一般常用<span style="color: rgba(0, 0, 0, 1)"><span style="color: rgba(0, 0, 0, 1)"><span style="color: rgba(0, 0, 0, 1)"><span style="color: rgba(0, 0, 0, 1)"><span style="color: rgba(0, 0, 0, 1)"><span style="color: rgba(0, 0, 0, 1)"><strong>AAC_ADTS</strong></span></span></span></span></span></span></span></p>
<ul>
<li style="color: rgba(0, 0, 0, 1)"><span style="color: rgba(0, 0, 0, 1)"><strong>DEFAULT:</strong>默认 在不更新对应值的情况下,不要更改这些值</span></li>
<li style="color: rgba(0, 0, 0, 1)"><span style="color: rgba(0, 0, 0, 1)"><strong>THREE_GPP</strong>:3GP格式</span></li>
<li style="color: rgba(0, 0, 0, 1)"><span style="color: rgba(0, 0, 0, 1)"><strong>MPEG_4</strong>:MP4格式</span></li>
<li style="color: rgba(0, 0, 0, 1)"><span style="color: rgba(0, 0, 0, 1)"><strong>RAW_AMR</strong>:录制原始文件,这只支持音频录制,同时要求音频编码为AMR_NB</span></li>
<li style="color: rgba(0, 0, 0, 1)"><span style="color: rgba(0, 0, 0, 1)"><strong>AMR_NB</strong>: <span style="color: rgba(0, 0, 0, 1)">常用与通话文件语音格式 语音带宽范围:300-3400Hz,8KHz采样 </span></span></li>
<li style="color: rgba(0, 0, 0, 1)"><span style="color: rgba(0, 0, 0, 1)"><span style="color: rgba(0, 0, 0, 1)"><span style="color: rgba(0, 0, 0, 1)"><strong>AMR_WB</strong>: <span style="color: rgba(0, 0, 0, 1)">常用与通话文件语音格式 语音带宽范围: 50-7000Hz,16KHz采样 </span></span></span></span></li>
<li style="color: rgba(0, 0, 0, 1)"><span style="color: rgba(0, 0, 0, 1)"><span style="color: rgba(0, 0, 0, 1)"><span style="color: rgba(0, 0, 0, 1)"><span style="color: rgba(0, 0, 0, 1)"><span style="color: rgba(0, 0, 0, 1)"><strong>AAC_ADIF</strong>: Audio Data Interchange Format音频数据交换格式。这种格式的特征是可以确定的找到这个音频数据的开始,不需进行在音频数据流中间开始的解码,即它的解码必须在明确定义的开始处进行。故这种格式常用在磁盘文件中。</span></span></span></span></span></li>
<li style="color: rgba(0, 0, 0, 1)"><span style="color: rgba(0, 0, 0, 1)"><span style="color: rgba(0, 0, 0, 1)"><span style="color: rgba(0, 0, 0, 1)"><span style="color: rgba(0, 0, 0, 1)"><span style="color: rgba(0, 0, 0, 1)"><span style="color: rgba(0, 0, 0, 1)"><strong>AAC_ADTS</strong>: Audio Data Transport Stream 音频数据传输流。这种格式的特征是它是一个有同步字的比特流,解码可以在这个流中任何位置开始。它的特征类似于mp3数据流格式。这种格式可以用于广播电视。</span></span></span></span></span></span></li>
<li style="color: rgba(0, 0, 0, 1)"><span style="color: rgba(0, 0, 0, 1)"><span style="color: rgba(0, 0, 0, 1)"><span style="color: rgba(0, 0, 0, 1)"><span style="color: rgba(0, 0, 0, 1)"><span style="color: rgba(0, 0, 0, 1)"><span style="color: rgba(0, 0, 0, 1)"><span style="color: rgba(0, 0, 0, 1)"><strong>OUTPUT_FORMAT_RTP_AVP</strong>: 未知</span></span></span></span></span></span></span></li>
<li style="color: rgba(0, 0, 0, 1)"><span style="color: rgba(0, 0, 0, 1)"><span style="color: rgba(0, 0, 0, 1)"><span style="color: rgba(0, 0, 0, 1)"><span style="color: rgba(0, 0, 0, 1)"><span style="color: rgba(0, 0, 0, 1)"><span style="color: rgba(0, 0, 0, 1)"><span style="color: rgba(0, 0, 0, 1)"><span style="color: rgba(0, 0, 0, 1)"><strong>MPEG_2_TS</strong>:MPEG2-TS(Transport Stream“传输流”;又称TS、TP、MPEG-TS 或 M2T)是用于音效、图像与数据的通信协定,最早应用于DVD的实时传送节目。</span></span></span></span></span></span></span></span></li>
<li style="color: rgba(0, 0, 0, 1)"><span style="color: rgba(0, 0, 0, 1)"><span style="color: rgba(0, 0, 0, 1)"><span style="color: rgba(0, 0, 0, 1)"><span style="color: rgba(0, 0, 0, 1)"><span style="color: rgba(0, 0, 0, 1)"><span style="color: rgba(0, 0, 0, 1)"><span style="color: rgba(0, 0, 0, 1)"><span style="color: rgba(0, 0, 0, 1)"><span style="color: rgba(0, 0, 0, 1)"><strong>WEBM</strong>: WebM由Google提出,是一个开放、免费的媒体文件格式。WebM 影片格式其实是以 Matroska(即 MKV)容器格式为基础开发的新容器格式,里面包括了VP8影片轨和 Ogg Vorbis 音轨,其中Google将其拥有的VP8视频编码技术以类似BSD授权开源,Ogg Vorbis 本来就是开放格式。 WebM标准的网络视频更加偏向于开源并且是基于HTML5标准的,WebM 项目旨在为对每个人都开放的网络开发高质量、开放的视频格式,其重点是解决视频服务这一核心的网络用户体验。Google 说 WebM 的格式相当有效率,应该可以在 netbook、tablet、手持式装置等上面顺畅地使用</span></span></span></span></span></span></span></span></span></li>
</ul>
<h3><span style="color: rgba(0, 128, 128, 1)"> setAudioEncoder(<span style="font-weight: bold">int audio_encoder)</span></span></h3>
<p><span style="color: rgba(0, 0, 0, 1)"> 设置录制音频的编码格式 请酌情设置,有些设备并不支持一些格式。一般常用AAC。全部格式请参考如下代码:</span></p>
<p> </p>
<div class="cnblogs_code">
<pre> <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">final</span> <span style="color: rgba(0, 0, 255, 1)">int</span> DEFAULT = 0<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)"> AMR (Narrowband) audio codec </span><span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">final</span> <span style="color: rgba(0, 0, 255, 1)">int</span> AMR_NB = 1<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)"> AMR (Wideband) audio codec </span><span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">final</span> <span style="color: rgba(0, 0, 255, 1)">int</span> AMR_WB = 2<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)"> AAC Low Complexity (AAC-LC) audio codec </span><span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">final</span> <span style="color: rgba(0, 0, 255, 1)">int</span> AAC = 3<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)"> High Efficiency AAC (HE-AAC) audio codec </span><span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">final</span> <span style="color: rgba(0, 0, 255, 1)">int</span> HE_AAC = 4<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)"> Enhanced Low Delay AAC (AAC-ELD) audio codec </span><span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">final</span> <span style="color: rgba(0, 0, 255, 1)">int</span> AAC_ELD = 5<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)"> Ogg Vorbis audio codec </span><span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">final</span> <span style="color: rgba(0, 0, 255, 1)">int</span> VORBIS = 6;</pre>
</div>
<p> </p>
<h3><span style="color: rgba(0, 128, 128, 1)">setOutputFile(String path)</span></h3>
<p><span style="color: rgba(0, 0, 0, 1)">设置输出音频文件的路径加文件名称,另外有一个setOutputFile(File file)方法,这个方法需要26API<br></span></p>
<h3><span style="color: rgba(0, 128, 128, 1)">setAudioEncodingBitRate(<span style="font-weight: bold">int bitRate)</span></span> </h3>
<p><span style="color: rgba(0, 0, 0, 1)">设置音频的比特率, 比特率是指每秒传送的比特(bit)数。<span style="color: rgba(0, 0, 0, 1)">单位为 <span style="color: rgba(0, 0, 0, 1)">bps(Bit Per Second),<span style="color: rgba(0, 0, 0, 1)">比特率越高,每秒传送<span style="color: rgba(0, 0, 0, 1)">数据就越多,音频也就越清晰</span></span></span></span></span></p>
<h3><span style="color: rgba(0, 128, 128, 1)">setMaxFileSize(<span style="font-weight: bold">long max_filesize_bytes)</span></span></h3>
<p>设置录音文件的最大存储大小 ,单位是byte</p>
<h3><span style="color: rgba(0, 128, 128, 1)">setMaxDuration(<span style="font-weight: bold">int max_duration_ms)</span></span></h3>
<p><span style="color: rgba(0, 0, 0, 1)">设置录音文件的最大录制时间,单位是毫秒</span></p>
<h3><span style="color: rgba(0, 128, 128, 1)">setOnErrorListener(OnErrorListener l)</span></h3>
<p><span style="color: rgba(0, 0, 0, 1)">设置监听异常报错</span></p>
<h3><span style="color: rgba(0, 128, 128, 1)">setAudioChannels(<span style="font-weight: bold">int numChannels)</span> </span></h3>
<p><span style="color: rgba(0, 0, 0, 1)">设置音频声道,官方注解里也说了这个方法的值一般是1或者2,分别代表单声道与双声道.</span></p>
<h3><span style="color: rgba(0, 128, 128, 1)">setOnInfoListener(OnInfoListener listener)</span></h3>
<p><span style="color: rgba(0, 0, 0, 1)">设置监听录制信息,对应返回信息,请参考如下代码注释:</span></p>
<p> </p>
<div class="cnblogs_code">
<pre> mMediaRecorder.setOnInfoListener(<span style="color: rgba(0, 0, 255, 1)">new</span> MediaRecorder.OnInfoListener() {<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">信息回调</span>
<span style="color: rgba(0, 0, 0, 1)"> @Override
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span> onInfo(MediaRecorder mr, <span style="color: rgba(0, 0, 255, 1)">int</span> what, <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> extra) {
</span><span style="color: rgba(0, 0, 255, 1)">switch</span><span style="color: rgba(0, 0, 0, 1)"> (what) {
</span><span style="color: rgba(0, 0, 255, 1)">case</span><span style="color: rgba(0, 0, 0, 1)"> MediaRecorder.MEDIA_RECORDER_INFO_MAX_DURATION_REACHED:
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">已设置最大录制持续时间,现在已达到</span>
<span style="color: rgba(0, 0, 255, 1)">break</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 0, 255, 1)">case</span><span style="color: rgba(0, 0, 0, 1)"> MediaRecorder.MEDIA_ERROR_SERVER_DIED:
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">媒体服务器死亡。在这种情况下,应用程序必须释放* MediaRecorder对象并实例化一个新对象</span>
<span style="color: rgba(0, 0, 255, 1)">break</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 0, 255, 1)">case</span><span style="color: rgba(0, 0, 0, 1)"> MediaRecorder.MEDIA_RECORDER_ERROR_UNKNOWN:
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">未指定的媒体记录器错误</span>
<span style="color: rgba(0, 0, 255, 1)">break</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 0, 255, 1)">case</span><span style="color: rgba(0, 0, 0, 1)"> MediaRecorder.MEDIA_RECORDER_INFO_UNKNOWN:
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">未指定的媒体记录器信息</span>
<span style="color: rgba(0, 0, 255, 1)">break</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 0, 255, 1)">case</span><span style="color: rgba(0, 0, 0, 1)"> MediaRecorder.MEDIA_RECORDER_INFO_MAX_FILESIZE_APPROACHING:
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">已设置最大文件大小,并且当前记录的文件大小*已达到限制的90%
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">达到或者超过90%限制时每个文件发送一次,要继续记录,应该使用{@link #setNextOutputFile}设置下一个输出文件。
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">否则,达到最大文件大小时,录制将停止</span>
<span style="color: rgba(0, 0, 255, 1)">break</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 0, 255, 1)">case</span><span style="color: rgba(0, 0, 0, 1)"> MediaRecorder.MEDIA_RECORDER_INFO_MAX_FILESIZE_REACHED:
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">已设置最大文件大小,现在已达到。
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">注意:如果应用程序已经通过{@link #setNextOutputFile}设置了下一个输出文件,则不会发送此事件</span>
<span style="color: rgba(0, 0, 255, 1)">break</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 0, 255, 1)">case</span><span style="color: rgba(0, 0, 0, 1)"> MediaRecorder.MEDIA_RECORDER_INFO_NEXT_OUTPUT_FILE_STARTED:
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">已达到最大文件大小,媒体记录器信息下一个输出文件已开始,
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">并且MediaRecorder已将*输出切换为应用程序{@link #setNextOutputFile}设置的新文件
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">为了获得最佳实践,应用程序应使用此事件来跟踪*是否使用了先前设置的文件</span>
<span style="color: rgba(0, 0, 255, 1)">break</span><span style="color: rgba(0, 0, 0, 1)">;
}
}
});</span></pre>
</div>
<p> </p>
</div>
<div id="MySignature" role="contentinfo">
<div style="text-align: center">
<p style="color:orange;font-size:16px;" >本文来自博客园,作者:观心静 ,转载请注明原文链接:https://www.cnblogs.com/guanxinjing/p/10976026.html </p>
<div style="color:orange;font-size:16px;">本文版权归作者和博客园共有,欢迎转载,但必须给出原文链接,并保留此段声明,否则保留追究法律责任的权利。 </div>
</div><br><br>
来源:https://www.cnblogs.com/guanxinjing/p/10976026.html
頁:
[1]