当我尝试停止时,后台录音会抛出IllegalStateException

问题描述 投票:0回答:3

我正在使用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();
    }


}

我无法弄清楚代码有什么问题。请任何人都可以帮忙????

android audiorecord
3个回答
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" />

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


0
投票

我在AndroidManifest.xml上添加了这两个权限,它不会再崩溃了。 P / S:应用程序将在启动时请求您录制音频的许可。

<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
© www.soinside.com 2019 - 2024. All rights reserved.