我正在使用Service
课程创建背景音频录音机。当我开始录制时,它可以正常工作,但是当我停止录制时,会在IllegalStateException
上抛出onStop()
。
这是我的服务类代码:
public class AudioRecorderService extends Service {
MediaRecorder mAudioRecorder;
String fileName = null;
public AudioRecorderService() {
}
@Override
public void onCreate() {
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
setupRecorder();
Toast.makeText(this, "Recording Started", Toast.LENGTH_LONG).show();
return START_STICKY;
}
private void setupRecorder(){
fileName = Environment.getDataDirectory().getAbsolutePath() + "/" + "test.mp4";
mAudioRecorder = new MediaRecorder();
mAudioRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mAudioRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mAudioRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
mAudioRecorder.setAudioEncodingBitRate(256);
mAudioRecorder.setAudioChannels(1);
mAudioRecorder.setAudioSamplingRate(44100);
mAudioRecorder.setOutputFile(fileName);
try {
mAudioRecorder.prepare();
mAudioRecorder.start();
} catch (IOException e) {
e.printStackTrace();
}
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onDestroy() {
if (mAudioRecorder != null) {
mAudioRecorder.stop();
mAudioRecorder.reset();
mAudioRecorder.release();
}
Toast.makeText(this, "Recording Done", Toast.LENGTH_LONG).show();
}
}
我无法弄清楚代码有什么问题。请任何人都可以帮忙????
答案 0 :(得分:0)
启动和停止时应该按此顺序排列。任何错误通知都是必要的
MediaRecorder recorder = null;
private void startRecording(File file) {
if (recorder != null) {
recorder.release();
}
recorder = new MediaRecorder();
recorder.setAudioSource(AudioSource.MIC);
recorder.setOutputFormat(OutputFormat.THREE_GPP);
recorder.setAudioEncoder(AudioEncoder.AMR_WB);
recorder.setOutputFile(file.getAbsolutePath());
try {
recorder.prepare();
recorder.start();
} catch (IOException e) {
Log.e("Error", "io problems while preparing [" +
file.getAbsolutePath() + "]: " + e.getMessage());
}
}
现在java.lang.IllegalStateException
如果您不按照上述顺序发生错误,请忽略一些重要步骤或prepare()
失败,但无论如何都会继续。后者是最容易出错的地方。您应该只在start()
退出时无异常地致电prepare()
。否则你将获得IllegalStateException
。由于它是RuntimeException
,您可能无法捕获它,您的应用程序将强制关闭。
停止遵循此顺序
private void stopRecording() {
if (recorder != null) {
recorder.stop();
recorder.release();
recorder = null;
}
}
并且最后但不是Manifest中的租约,许可
<uses-permission
android:name="android.permission.RECORD_AUDIO" />
答案 1 :(得分:0)
由于mAudioRecorder.reset();
@Override
public void onDestroy() {
if (mAudioRecorder != null) {
mAudioRecorder.stop();
// here is error
mAudioRecorder.reset();
mAudioRecorder.release();
}
Toast.makeText(this, "Recording Done", Toast.LENGTH_LONG).show();
}
更新。
@Override
public void onDestroy() {
if (mAudioRecorder != null) {
mAudioRecorder.stop();
mAudioRecorder.release();
}
Toast.makeText(this, "Recording Done", Toast.LENGTH_LONG).show();
}
参考官方网站:https://developer.android.com/guide/topics/media/audio-capture.html
答案 2 :(得分:0)
我在AndroidManifest.xml上添加了这2个权限,它不再崩溃。 P / S:启动后,应用程序会要求您许可录制音频。
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>