我正在尝试在服务中使用MediaRecorder来录制声音.但是它在开始时会产生1秒的延迟(静音).我该如何摆脱呢?我尝试使用RehearsalAudioRecorder仍然没有运气.如果有人已解决此问题,请先提出建议.
开始
Intent serviceIntent = new Intent(); serviceIntent.setAction("com.soundrecoder.RecorderService"); serviceIntent.putExtra("audiofile", path); serviceIntent.putExtra("state", true); startService(serviceIntent);
停止
Intent serviceIntent = new Intent(); serviceIntent.setAction("com.soundrecoder.RecorderService"); serviceIntent.putExtra("state", false); startService(serviceIntent);
RecorderService.java文件
public class RecorderService extends Service{ private static final String TAG = null; private static MediaRecorder mRecorder; public voID onCreate() {}; public voID onStart(Intent intent, int startID) { boolean isstart = intent.getBooleanExtra("state", false); if (isstart) { mRecorder = new MediaRecorder(); mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); mRecorder.setoutputFormat(MediaRecorder.OutputFormat.THREE_GPP); mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); mRecorder.setoutputfile(intent.getStringExtra("audiofile")); try { mRecorder.prepare(); } catch (IllegalStateException e) { Log.e(TAG,e.getMessage()); } catch (IOException e) { Log.e(TAG,e.getMessage()); } try { mRecorder.start(); } catch (IllegalStateException e) { Log.e(TAG, e.getMessage()); } } else if (!isstart) { mRecorder.stop(); mRecorder.reset(); } } @OverrIDe public IBinder onBind(Intent intent) { return null; }}
解决方法:
我找到了RehearsalAudioRecorder解决方案
public voID onStart(Intent intent, int startID) { boolean isstart = intent.getBooleanExtra("state", false); if (isstart) { acquireWakeLock(); int i = 0; do { if (mRecorder != null) mRecorder.release(); mRecorder = new RehearsalAudioRecorder(false, 0, 0, 0, 0); } while ((++i < 44100) & !(mRecorder.getState() == RehearsalAudioRecorder.State.INITIAliZING)); mRecorder.setoutputfile(intent.getStringExtra("audiofile")); mRecorder.prepare(); mRecorder.start(); } else if (!isstart) { if(mRecorder != null) { mRecorder.stop(); mRecorder.release(); mRecorder = null; } releaseWakeLock(); } }
总结 以上是内存溢出为你收集整理的android-MediaRecorder以1秒的延迟开始.我如何摆脱沉默?全部内容,希望文章能够帮你解决android-MediaRecorder以1秒的延迟开始.我如何摆脱沉默?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)