1
我想在服務中使用MediaRecorder來錄製聲音。但它在開始時會產生1秒延遲(靜音)。如何擺脫這個?我試過用RehearsalAudioRecorder還是沒有運氣。如果有人在請建議之前解決了這個問題。MediaRecorder以1秒的延遲開始。我如何擺脫沉默?
開始
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;
}
}