MediaRecorder.start()中的IllegalStateException

时间:2014-07-21 08:54:02

标签: android broadcastreceiver illegalstateexception phone-state-listener

我多次问过这个问题。但我没有找到解决方案。

我有一个广播接收器,我在其中记录呼叫。 recorder.start()抛出IllegalStateException。我在Manifest中授予了所有权限。我打电话给recorder.prepare()(它不会抛出任何异常。)

我错过了什么吗?

这是我的简化代码:

case TelephonyManager.CALL_STATE_OFFHOOK:
   recordState = true;

                  MediaRecorder mRecorder = new MediaRecorder();
                        mRecorder
                                .setAudioSource(MediaRecorder.AudioSource.VOICE_CALL);
                        mRecorder
                                .setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
                        mRecorder
                                .setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
                        mRecorder.setOutputFile(filePath);
                        mRecorder.setOnErrorListener(new OnErrorListener() {

                            @Override
                            public void onError(MediaRecorder mr, int what,
                                    int extra) {

                            }
                        });
                        mRecorder.setOnInfoListener(new OnInfoListener() {

                            @Override
                            public void onInfo(MediaRecorder mr, int what,
                                    int extra) {
                                // TODO Auto-generated method stub

                            }
                        });
                        mRecorder.prepare();
                        mRecorder.start();
                        recorder = mRecorder;

case TelephonyManager.CALL_STATE_IDLE:
if (recorder != null && recordState) {
                            recorder.stop();
                            recorder.reset();
                            recorder.release();
                            recorder = null;

                        }

这是我的logcat:

07-21 14:23:08.807: E/MediaRecorder(4202): start failed: -38
07-21 14:23:08.807: W/System.err(4202): java.lang.IllegalStateException
07-21 14:23:08.807: W/System.err(4202):     at android.media.MediaRecorder.start(Native Method)
07-21 14:23:08.807: W/System.err(4202):     at com.labelle.calllogsmanagement.MyReceiver$MyPhoneStateListener.onCallStateChanged(MyReceiver.java:165)
07-21 14:23:08.807: W/System.err(4202):     at android.telephony.PhoneStateListener$2.handleMessage(PhoneStateListener.java:393)
07-21 14:23:08.807: W/System.err(4202):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-21 14:23:08.807: W/System.err(4202):     at android.os.Looper.loop(Looper.java:137)
07-21 14:23:08.807: W/System.err(4202):     at android.app.ActivityThread.main(ActivityThread.java:4856)
07-21 14:23:08.807: W/System.err(4202):     at java.lang.reflect.Method.invokeNative(Native Method)
07-21 14:23:08.807: W/System.err(4202):     at java.lang.reflect.Method.invoke(Method.java:511)
07-21 14:23:08.817: W/System.err(4202):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
07-21 14:23:08.817: W/System.err(4202):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
07-21 14:23:08.817: W/System.err(4202):     at dalvik.system.NativeStart.main(Native Method)

修改 在给定路径上创建一个空文件。

修改

现在我创建了一个带有两个按钮的虚拟应用程序,一个用于启动,另一个用于停止录制。这是代码并在调用start()时抛出相同的异常:

package com.example.recordtest;

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

import android.app.Activity;
import android.media.MediaRecorder;
import android.media.MediaRecorder.OnErrorListener;
import android.media.MediaRecorder.OnInfoListener;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {
    MediaRecorder mRecorder;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button btn1 = (Button) findViewById(R.id.button1);
        Button btn2 = (Button) findViewById(R.id.button2);

        btn1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                mRecorder = new MediaRecorder();
                mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
                mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
                mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
                File file = new File(Environment.getExternalStorageDirectory(),
                        "1234.3gp");
                mRecorder.setOutputFile(file.getAbsolutePath());
                mRecorder.setOnErrorListener(new OnErrorListener() {

                    @Override
                    public void onError(MediaRecorder mr, int what, int extra) {

                    }
                });
                mRecorder.setOnInfoListener(new OnInfoListener() {

                    @Override
                    public void onInfo(MediaRecorder mr, int what, int extra) {
                        // TODO Auto-generated method stub

                    }
                });
                try {
                    mRecorder.prepare();
                    mRecorder.start();
                } catch (IllegalStateException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            }
        });

        btn2.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                mRecorder.stop();
                mRecorder.reset();
                mRecorder.release();
            }
        });

    }

}

1 个答案:

答案 0 :(得分:1)

经过几个小时的磨头,我意识到了。这是一个设备问题。上面的代码在其他设备中运行正常。

我知道无法在所有设备中完成通话记录(制造商限制它)。但从来不知道它会引发IllegalStateException