尝试录制音频时应用程序崩溃

时间:2013-07-30 11:24:40

标签: java android eclipse audio-recording

所以我正在尝试制作一个简单的应用程序,只要触摸并保持图像按钮就能录制音频。这是代码:

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //PREPARE MEDIA RECORDER
    final MediaRecorder recorder = new MediaRecorder();
    recorder.reset();
    recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
    recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
    recorder.setOutputFile("/easyvoicenotepad/test.mp3");

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

    //IMAGE BUTTON
    final ImageButton recBtn = (ImageButton) findViewById(R.id.recButton);
    recBtn.setOnTouchListener(new OnTouchListener() {

        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_DOWN ) {
                recBtn.setImageResource(R.drawable.record_btn_pressed);
                recorder.start();
            }
            else if(event.getAction() == MotionEvent.ACTION_UP) {
                recBtn.setImageResource(R.drawable.record_btn);
                recorder.stop();
                recorder.release();
            }

            return false;
        }
    });
}

编辑的Logcat文件:

07-30 13:53:09.423: E/MediaRecorder(10586): start called in an invalid state: 4
07-30 13:53:09.423: E/InputEventReceiver(10586): Exception dispatching input event.
07-30 13:53:09.423: E/MessageQueue-JNI(10586): Exception in MessageQueue callback: handleReceiveCallback
07-30 13:53:09.431: E/MessageQueue-JNI(10586): java.lang.IllegalStateException
07-30 13:53:09.431: E/MessageQueue-JNI(10586):  at android.media.MediaRecorder.start(Native Method)
07-30 13:53:09.431: E/MessageQueue-JNI(10586):  at com.whizzappseasyvoicenotepad.MainActivity$1.onTouch(MainActivity.java:48)
07-30 13:53:09.431: E/MessageQueue-JNI(10586):  at android.view.View.dispatchTouchEvent(View.java:7379)
07-30 13:53:09.431: E/MessageQueue-JNI(10586):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2209)
07-30 13:53:09.431: E/MessageQueue-JNI(10586):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1910)
07-30 13:53:09.431: E/MessageQueue-JNI(10586):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2209)
07-30 13:53:09.431: E/MessageQueue-JNI(10586):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1910)
07-30 13:53:09.431: E/MessageQueue-JNI(10586):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2209)
07-30 13:53:09.431: E/MessageQueue-JNI(10586):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1910)
07-30 13:53:09.431: E/MessageQueue-JNI(10586):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2209)
07-30 13:53:09.431: E/MessageQueue-JNI(10586):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1910)
07-30 13:53:09.431: E/MessageQueue-JNI(10586):  at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1966)
07-30 13:53:09.431: E/MessageQueue-JNI(10586):  at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1418)
07-30 13:53:09.431: E/MessageQueue-JNI(10586):  at android.app.Activity.dispatchTouchEvent(Activity.java:2424)
07-30 13:53:09.431: E/MessageQueue-JNI(10586):  at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1914)
07-30 13:53:09.431: E/MessageQueue-JNI(10586):  at android.view.View.dispatchPointerEvent(View.java:7564)
07-30 13:53:09.431: E/MessageQueue-JNI(10586):  at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:3883)
07-30 13:53:09.431: E/MessageQueue-JNI(10586):  at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3778)
07-30 13:53:09.431: E/MessageQueue-JNI(10586):  at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3379)
07-30 13:53:09.431: E/MessageQueue-JNI(10586):  at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3429)
07-30 13:53:09.431: E/MessageQueue-JNI(10586):  at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3398)
07-30 13:53:09.431: E/MessageQueue-JNI(10586):  at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3483)
07-30 13:53:09.431: E/MessageQueue-JNI(10586):  at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3406)
07-30 13:53:09.431: E/MessageQueue-JNI(10586):  at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3540)
07-30 13:53:09.431: E/MessageQueue-JNI(10586):  at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3379)
07-30 13:53:09.431: E/MessageQueue-JNI(10586):  at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3429)
07-30 13:53:09.431: E/MessageQueue-JNI(10586):  at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3398)
07-30 13:53:09.431: E/MessageQueue-JNI(10586):  at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3406)
07-30 13:53:09.431: E/MessageQueue-JNI(10586):  at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3379)
07-30 13:53:09.431: E/MessageQueue-JNI(10586):  at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5419)
07-30 13:53:09.431: E/MessageQueue-JNI(10586):  at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5399)
07-30 13:53:09.431: E/MessageQueue-JNI(10586):  at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5370)
07-30 13:53:09.431: E/MessageQueue-JNI(10586):  at     android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:5493)
07-30 13:53:09.431: E/MessageQueue-JNI(10586):  at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:182)
07-30 13:53:09.431: E/MessageQueue-JNI(10586):  at android.os.MessageQueue.nativePollOnce(Native Method)
07-30 13:53:09.431: E/MessageQueue-JNI(10586):  at android.os.MessageQueue.next(MessageQueue.java:132)
07-30 13:53:09.431: E/MessageQueue-JNI(10586):  at android.os.Looper.loop(Looper.java:124)
07-30 13:53:09.431: E/MessageQueue-JNI(10586):  at android.app.ActivityThread.main(ActivityThread.java:5103)
07-30 13:53:09.431: E/MessageQueue-JNI(10586):  at java.lang.reflect.Method.invokeNative(Native Method)
07-30 13:53:09.431: E/MessageQueue-JNI(10586):  at java.lang.reflect.Method.invoke(Method.java:525)
07-30 13:53:09.431: E/MessageQueue-JNI(10586):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
07-30 13:53:09.431: E/MessageQueue-JNI(10586):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
07-30 13:53:09.431: E/MessageQueue-JNI(10586):  at dalvik.system.NativeStart.main(Native Method)
07-30 13:53:09.438: E/AndroidRuntime(10586): FATAL EXCEPTION: main
07-30 13:53:09.438: E/AndroidRuntime(10586): java.lang.IllegalStateException
07-30 13:53:09.438: E/AndroidRuntime(10586):    at android.media.MediaRecorder.start(Native Method)
07-30 13:53:09.438: E/AndroidRuntime(10586):    at com.whizzappseasyvoicenotepad.MainActivity$1.onTouch(MainActivity.java:48)
07-30 13:53:09.438: E/AndroidRuntime(10586):    at android.view.View.dispatchTouchEvent(View.java:7379)
07-30 13:53:09.438: E/AndroidRuntime(10586):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2209)
07-30 13:53:09.438: E/AndroidRuntime(10586):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1910)
07-30 13:53:09.438: E/AndroidRuntime(10586):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2209)
07-30 13:53:09.438: E/AndroidRuntime(10586):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1910)
07-30 13:53:09.438: E/AndroidRuntime(10586):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2209)
07-30 13:53:09.438: E/AndroidRuntime(10586):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1910)
07-30 13:53:09.438: E/AndroidRuntime(10586):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2209)
07-30 13:53:09.438: E/AndroidRuntime(10586):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1910)
07-30 13:53:09.438: E/AndroidRuntime(10586):    at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1966)
07-30 13:53:09.438: E/AndroidRuntime(10586):    at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1418)
07-30 13:53:09.438: E/AndroidRuntime(10586):    at android.app.Activity.dispatchTouchEvent(Activity.java:2424)
07-30 13:53:09.438: E/AndroidRuntime(10586):    at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1914)
07-30 13:53:09.438: E/AndroidRuntime(10586):    at android.view.View.dispatchPointerEvent(View.java:7564)
07-30 13:53:09.438: E/AndroidRuntime(10586):    at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:3883)
07-30 13:53:09.438: E/AndroidRuntime(10586):    at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3778)
07-30 13:53:09.438: E/AndroidRuntime(10586):    at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3379)
07-30 13:53:09.438: E/AndroidRuntime(10586):    at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3429)
07-30 13:53:09.438: E/AndroidRuntime(10586):    at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3398)
07-30 13:53:09.438: E/AndroidRuntime(10586):    at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3483)
07-30 13:53:09.438: E/AndroidRuntime(10586):    at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3406)
07-30 13:53:09.438: E/AndroidRuntime(10586):    at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3540)
07-30 13:53:09.438: E/AndroidRuntime(10586):    at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3379)
07-30 13:53:09.438: E/AndroidRuntime(10586):    at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3429)
07-30 13:53:09.438: E/AndroidRuntime(10586):    at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3398)
07-30 13:53:09.438: E/AndroidRuntime(10586):    at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3406)
07-30 13:53:09.438: E/AndroidRuntime(10586):    at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3379)
07-30 13:53:09.438: E/AndroidRuntime(10586):    at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5419)
07-30 13:53:09.438: E/AndroidRuntime(10586):    at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5399)
07-30 13:53:09.438: E/AndroidRuntime(10586):    at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5370)
07-30 13:53:09.438: E/AndroidRuntime(10586):    at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:5493)
07-30 13:53:09.438: E/AndroidRuntime(10586):    at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:182)
07-30 13:53:09.438: E/AndroidRuntime(10586):    at android.os.MessageQueue.nativePollOnce(Native Method)
07-30 13:53:09.438: E/AndroidRuntime(10586):    at android.os.MessageQueue.next(MessageQueue.java:132)
07-30 13:53:09.438: E/AndroidRuntime(10586):    at android.os.Looper.loop(Looper.java:124)
07-30 13:53:09.438: E/AndroidRuntime(10586):    at android.app.ActivityThread.main(ActivityThread.java:5103)
07-30 13:53:09.438: E/AndroidRuntime(10586):    at java.lang.reflect.Method.invokeNative(Native Method)
07-30 13:53:09.438: E/AndroidRuntime(10586):    at java.lang.reflect.Method.invoke(Method.java:525)
07-30 13:53:09.438: E/AndroidRuntime(10586):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
07-30 13:53:09.438: E/AndroidRuntime(10586):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
07-30 13:53:09.438: E/AndroidRuntime(10586):    at dalvik.system.NativeStart.main(Native Method)

问题是,在我添加录音机功能后,应用程序在编译后立即崩溃。

我有权限设置,现在我的应用程序一碰到ImageButton

就会崩溃

4 个答案:

答案 0 :(得分:1)

permission

中添加AndroidManifest.xml
<uses-permission android:name="android.permission.RECORD_AUDIO" />

答案 1 :(得分:1)

根据问题下方的评论,更改整个代码,以便onTouchonCreate()隔离。它应该是这样的:

@Override
public boolean onTouchEvent(MotionEvent event)
{

  if (event.getAction() == MotionEvent.ACTION_DOWN ) {
      recBtn.setImageResource(R.drawable.record_btn_pressed);
      recorder.start();
      return true;
  }
  else if(event.getAction() == MotionEvent.ACTION_UP) {
      recBtn.setImageResource(R.drawable.record_btn);
      recorder.stop();
      recorder.reset();
      recorder.release();
      recorder = null;
  }

return true;
}

答案 2 :(得分:0)

在AndroidManifest.xml文件中添加权限,

  <uses-permission android:name="android.permission.RECORD_AUDIO" />

答案 3 :(得分:0)

您已声明记录器对象为最终版本 - 最终MediaRecorder recorder = new MediaRecorder();

final表示常量,使用代码无法更新常量成员。 因此,尝试将其设为静态或私有,我的意思是最终的。

私人MediaRecorder录音机=新MediaRecorder();