0

我正在尝试制作一个简单的应用程序,只要按下按钮就可以录制声音,然后将录制的文件保存到某个位置。这是我到目前为止的代码,一旦我触摸图像按钮,它就会崩溃:

package com.whizzappseasyvoicenotepad;

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

import android.app.Activity;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageButton;

public class MainActivity extends Activity {

MediaRecorder recorder;
String storagePath;
String externalStoragePath;

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

    //AUDIO RECORDER
    recorder = new MediaRecorder();
    recorder.reset();       
    recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
    recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
    if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
    {
        externalStoragePath = Environment.getExternalStorageDirectory().getAbsolutePath();
        recorder.setOutputFile(externalStoragePath + "/easyvoicenotepad/test.3gp");
    }
    else
    {
        storagePath = Environment.getDataDirectory().getAbsolutePath();
        recorder.setOutputFile(storagePath + "/easyvoicenotepad/test.3gp");
    }

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

        @Override
        public boolean onTouch(View v, MotionEvent event) {

            if (event.getAction() == MotionEvent.ACTION_DOWN)
            {
                recBtn.setImageResource(R.drawable.record_btn_pressed);
                try {
                    recorder.prepare();
                    recorder.start();
                } catch (IllegalStateException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            else if (event.getAction() == MotionEvent.ACTION_UP)
            {
                recBtn.setImageResource(R.drawable.record_btn);
                try {
                    recorder.prepare();
                    recorder.stop();
                    recorder.reset();
                    recorder.release();
                    recorder = null;
                } catch (IllegalStateException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                Log.i(STORAGE_SERVICE, "File saved to: " + externalStoragePath + "/easyvoicenotepad/test.3gp");
            }
            return true;
        }

    });
} //END OF ONCREATE

public void playTest(View v){

}

/*@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}*/

}

权限:

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

日志文件:

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

2 回答 2

0

如果您的问题是 MediaRecorder 不应该是最终的,请将其设置为全局变量。

public class MainActivity extends Activity {
MediaRecorder recorder;

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

    //AUDIO RECORDER
    recorder = new MediaRecorder();
    recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
    recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
    recorder.setOutputFile("/easyvoicenotepad/test.3gp");
于 2013-07-30T21:47:52.850 回答
0

您的输出文件不完整

if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
{
    String externalStoragePath = Environment.getExternalStorageDirectory().getAbsolutePath();
    recorder.setOutputFile(externalStoragePath + "/easyvoicenotepad/test.3gp");
}
于 2013-07-30T22:03:16.960 回答