这个Android AudioRecord录音功能是小编自己项目中使用的,因为以后要经常用到,因此想把AudioRecord的使用作为笔记记录下来,小编将Android录音功能写在了Service类里面会更加科学实用,因为可以后台录音,下面是小编的代码,代码可以直接拿来使用!
AudioRecord录制的为“.pcm”格式的文件,需要将它转码成mp3或其它格式的语音文件才能在网页或播放器上播放,后续本站会发布pcm格式转mp3格式语音文件的文章供大家学习!
一:在Service实现AndroidRecord录音功能
package com.xuanyin.homektv.service; import android.app.Service; import android.content.Intent; import android.media.AudioFormat; import android.media.AudioRecord; import android.media.MediaRecorder; import android.os.Binder; import android.os.IBinder; import android.support.annotation.Nullable; import android.util.Log; import com.tpyyes.MainActivity; import java.io.FileNotFoundException; import java.io.FileOutputStream; public class RecorderService extends Service { private AudioRecord audioRecord; //缓冲区大小 private int bufferSize; //录音线程 private Thread recordThread = null; //外部传进来的mp3文件名称 private String songName = ""; //录音标记 private boolean isRunning = true; //存储语音数据 private byte[] voiceData; @Nullable @Override public IBinder onBind(Intent intent) { return new Binder(); } //启动Service,会启动录音功能 @Override public int onStartCommand(Intent intent, int flags, int startId) { if(intent.hasExtra("SONG_NAME")){ songName = intent.getStringExtra("SONG_NAME"); } startRecorder(); return START_STICKY; } //开始录音 public void startRecorder(){ //获取最适合的缓冲大小(小编在原有基础上放大了2倍) bufferSize = AudioRecord.getMinBufferSize(16000,AudioFormat.CHANNEL_IN_MONO,AudioFormat.ENCODING_PCM_16BIT); //存储语音数据的数组 voiceData = new byte[bufferSize]; //创建录音对象(参数为:输入源为麦克风,赫兹16000,音道,比特数) audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC,16000, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT,bufferSize); //录音 audioRecord.startRecording(); //录音线程 recordThread = new Thread(new Runnable() { @Override public void run() { writeAudioToDirectory(); } },"recorder Thread"); //开启线程 recordThread.start(); } //将录音文件写入文件夹 private void writeAudioToDirectory() { //文件存储位置 String filePath = MainActivity.MY_SONGS_VOICE + songName +"_"+(System.currentTimeMillis() / 1000) + ".pcm"; FileOutputStream os = null; try { os = new FileOutputStream(filePath); } catch (FileNotFoundException e) { e.printStackTrace(); } int read = 0; //循环写入文件 while(isRunning){ Log.e("writeAudioToDirectory2","在写入语音文件:"+isRunning); read = audioRecord.read(voiceData,0,bufferSize); if(AudioRecord.ERROR_INVALID_OPERATION != read){ try{ os.write(voiceData); Log.e("writeAudioToDirectory","在写入语言文件"); }catch (Exception ex){ ex.printStackTrace(); } } } } //停止录音 public void stopRecorder(){ isRunning = false; if(recordThread != null){ recordThread.interrupt(); recordThread = null; } if(audioRecord != null){ audioRecord.stop(); audioRecord.release(); audioRecord = null; } } //销毁录音对象和线程 @Override public void onDestroy() { super.onDestroy(); stopRecorder(); Log.e("writeAudioToDirectory33","在写入文件:"+isRunning+"==="+songName); } }
二:注册Service
如果在Service中想要想要开启AudioRecord录音功能,必须要在AndroidManifest.xml中注册Service,代码如下:
<service android:enabled="true" android:name=".service.RecorderService"/>
三:在AndroidManifest.xml中给与录音权限
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
四:在Fragment中启动或关闭AudioRecord录音功能(当然也可以在Activity中使用)
//启动或关闭录音,可用在Click点击事件中 private void recordUserVoice() { String luyinStr = weixin_luyin.getText().toString(); Intent intent = new Intent(getActivity(), RecorderService.class); //开启录音 if(luyinStr.equals("开始录音")){ //可以将文件名传入到Service中,例如 intent.putExtra("SONG_NAME","测试歌曲名称"); getActivity().startService(intent); }else{ //关闭录音 weixin_luyin.setText("开始录音"); getActivity().stopService(intent); } }