停止时Android MediaRecorder异常 [英] Android MediaRecorder Exception when stopped

查看:375
本文介绍了停止时Android MediaRecorder异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个记录来电和去电的应用程序.通话结束时录音应该停止.但是我收到非法状态异常.异常在mediarecorder.stop().停止在无效状态4之间调用" .我没有知道这意味着什么.这是我第一次使用MediaRecorder.我检查了类似的问题..但是他们的回答没有帮助我.您的帮助将不胜感激.谢谢您..这个问题

I am creating an app that records phone calls, both incoming and outgoing. The recording should stop when call ends.But I get an Illegal State Exception.Th exception is at mediarecorder.stop()."Stop called between invalid state 4".I have no idea what that means.This is the first time I am using MediaRecorder. I have checked similar questions..but their answers did not help me.Your help would be appreciated.Thanks in advance..This question Error on MediaRecorder Stop : stop called in invalid state 4 did not help me as the problem in that question was the user called prepare() before he called the stop() method. It is not similar to my case.

代码:

import android.Manifest;
import android.content.Context;
import android.media.MediaRecorder;
import android.support.v4.app.ActivityCompat;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.widget.Toast;

import java.io.File;
import java.io.IOException;

public class TeleListener extends PhoneStateListener {
    private Context context;
    boolean isRinging=false;
    String filepath;
    String internalfilename="AUD";


    public TeleListener(Context context,String filepath) {
        this.context = context;
        this.filepath=filepath;
    }

    @Override
    public void onCallStateChanged(int state, String incomingNumber) {
        super.onCallStateChanged(state, incomingNumber);
        MediaRecorder mediaRecorder= new MediaRecorder();
        mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
        mediaRecorder.setOutputFile(filepath+internalfilename);
        switch (state) {
            case TelephonyManager.CALL_STATE_IDLE:

                if (isRinging) {
                    mediaRecorder.stop();
                    mediaRecorder.reset();
                    mediaRecorder.release();
                    isRinging=false;
                }
                Toast.makeText(context, "Idle", Toast.LENGTH_SHORT).show();
                break;
            case TelephonyManager.CALL_STATE_OFFHOOK:
                isRinging=false;
                Toast.makeText(context, "Offhook", Toast.LENGTH_SHORT).show();
                break;
            case TelephonyManager.CALL_STATE_RINGING:
                isRinging=true;
                try {
                    mediaRecorder.prepare();
                    mediaRecorder.start();

                } catch (IOException e) {
                    e.printStackTrace();
                }

                Toast.makeText(context, "Ringing", Toast.LENGTH_SHORT).show();
                break;

        }

    }
}

例外:

E/MediaRecorder: stop called in an invalid state: 4
03-E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.IllegalStateException
at android.media.MediaRecorder._stop(Native Method)
at android.media.MediaRecorder.stop(MediaRecorder.java:967)
at TeleListener.onCallStateChanged(TeleListener.java:43)
at android.telephony.PhoneStateListener$1.handleMessage
(PhoneStateListener.java:323)                                                                                            
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:7325) 
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run
(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

推荐答案

private void stopRecording() {
if (null != recorder){
    try {
        recorder.prepare(); 

停止 MediaRecorder 时,您不应调用准备. 准备方法是您在启动记录器之前调用的方法.请参阅 MediaRecorder 文档中的状态图.

You should not be calling prepare when you're stopping the MediaRecorder. The prepare method is something you call prior to starting the recorder. Refer to the state diagram in the MediaRecorder documentation.

这篇关于停止时Android MediaRecorder异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆