继续在MediaRecorder.Start上获取IllegalStateException

时间:2014-08-04 14:47:39

标签: android android-mediarecorder

好吧,伙计这很疯狂,不管我做什么,我似乎无法做到正确。它假设非常简单吗?

我在向用户播放声音文件后尝试从麦克风录音。

无论我做什么,我总是得到一些IllegalStateException,说" Start(Native Method)失败:

08-04 17:40:50.830: E/AndroidRuntime(22172): FATAL EXCEPTION: main
08-04 17:40:50.830: E/AndroidRuntime(22172): java.lang.IllegalStateException
08-04 17:40:50.830: E/AndroidRuntime(22172):    at android.media.MediaRecorder.start(Native Method)
08-04 17:40:50.830: E/AndroidRuntime(22172):    at Fragments.PhoneCall$2$1.onCompletion(PhoneCall.java:126)
08-04 17:40:50.830: E/AndroidRuntime(22172):    at android.media.MediaPlayer$EventHandler.handleMessage(MediaPlayer.java:2437)
08-04 17:40:50.830: E/AndroidRuntime(22172):    at android.os.Handler.dispatchMessage(Handler.java:99)
08-04 17:40:50.830: E/AndroidRuntime(22172):    at android.os.Looper.loop(Looper.java:176)
08-04 17:40:50.830: E/AndroidRuntime(22172):    at android.app.ActivityThread.main(ActivityThread.java:5419)
08-04 17:40:50.830: E/AndroidRuntime(22172):    at java.lang.reflect.Method.invokeNative(Native Method)
08-04 17:40:50.830: E/AndroidRuntime(22172):    at java.lang.reflect.Method.invoke(Method.java:525)
08-04 17:40:50.830: E/AndroidRuntime(22172):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
08-04 17:40:50.830: E/AndroidRuntime(22172):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
08-04 17:40:50.830: E/AndroidRuntime(22172):    at dalvik.system.NativeStart.main(Native Method)

权限:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
 <uses-permission android:name="android.permission.WRITE_I" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAPTURE_AUDIO_OUTPUT" />

片段......

public class PhoneCall extends Fragment {

    private Chronometer mTimer;
    private Typeface roboto;
    private MediaPlayer mDialTone;
    private MediaPlayer mVoice;
    private MediaRecorder mVoiceRecorder;
    private String mFileName;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.phone_call, container, false);

        TextView title = (TextView) view.findViewById(R.id.phoneCallTitle);
        title.setTypeface(roboto);

        roboto = Typeface.createFromAsset(getActivity().getAssets(),
                "fonts/roboto_regular.ttf");

        mTimer = (Chronometer) view.findViewById(R.id.chronometer1);
        mTimer.setVisibility(View.INVISIBLE);

        mDialTone = MediaPlayer.create(getActivity(), R.raw.dial_tone_0_5);
        mVoice = MediaPlayer.create(getActivity(), R.raw.papa_noel_english);

        if (mVoiceRecorder == null) {
            mVoiceRecorder = new MediaRecorder();
        }

        mVoiceRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        mVoiceRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        mVoiceRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);

        mFileName = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath();
        mFileName += "/Santa/Audio/" + "AUD_" + ".3gp";




        mVoiceRecorder.setOutputFile(mFileName);


        try {
            mVoiceRecorder.prepare();
        } catch (Exception e) {
            Log.e("PREP", "prepare() failed" + e.getMessage().toString());
        }

        return view;

    }




    @Override
    public void onResume() {

        // This thread starts and the chronometer on the UI thread final
        final Thread t = new Thread(new Runnable() {

            @Override
            public void run() {

                mTimer.post(new Runnable() {

                    @Override
                    public void run() {
                        mTimer.setVisibility(View.VISIBLE);
                        mTimer.setBase(SystemClock.elapsedRealtime());
                        mTimer.start();
                    }
                });
            }
        });

        // This Thread play the sounds and then starts the previous thread
        new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    Thread.sleep(1000);

                    // Play dial tones
                    mDialTone.start();
                    Thread.sleep(1000);
                    mDialTone.start();
                    Thread.sleep(1000);
                    t.start();

                    // Play voice and once finished, record audio
                    mVoice.start();
                    mVoice.setOnCompletionListener(new OnCompletionListener() {

                        @Override
                        public void onCompletion(MediaPlayer mp) {
                            mp.release();
                            mp = null;

                            mVoiceRecorder.start();

                        }
                    });

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

            }
        }).start();

        super.onResume();
    }

    @Override
    public void onDestroy() {
         mTimer.stop();
         mDialTone.stop();
         mVoice.stop();
         mVoiceRecorder.stop();

         mDialTone = null;
         mVoice = null;
         mVoiceRecorder = null;
        super.onDestroy();
    }

    @Override
    public void onPause() {
        mTimer.stop();
        mDialTone.stop();
        mVoiceRecorder.stop();

        mDialTone = null;
        mVoice = null;
        mVoiceRecorder = null;
        super.onPause();
    }
}

0 个答案:

没有答案