1

So I'm trying to make a simple app, that records audio as long as the imagebutton is touched and held. This is the code:

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;
        }
    });
}

Edited Logcat file:

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)

The problem is that the app crashes as soon as I compile it, after I added the audio recorder feature.

I have permissions set, now my app crashes as soon as I touch the ImageButton

4

4 回答 4

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;
}
于 2013-07-30T12:16:18.370 回答
1

添加permission里面AndroidManifest.xml

<uses-permission android:name="android.permission.RECORD_AUDIO" />
于 2013-07-30T11:27:42.160 回答
0

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

  <uses-permission android:name="android.permission.RECORD_AUDIO" />
于 2013-07-30T11:28:30.560 回答
0

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

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

私人 MediaRecorder 记录器 = new MediaRecorder();

于 2013-07-30T11:54:55.350 回答