应用程序使用onTouch,崩溃onClick

时间:2017-09-03 17:52:22

标签: java android button audio onclick

我目前正试图在长按并保持录音。当我点击并按住时应用程序工作,但是当我单击按钮时应用程序崩溃。请帮我弄清楚为什么会这样。

p / s:另外,有人可以告诉我如何格式化日志显示在这里吗?我很难用那个

代码

  public class MainActivity extends AppCompatActivity {

    private static final String LOG_TAG = "Hello";
    TextView recordDisplay;
    Button recordButton;
    private MediaRecorder mRecorder = null;
    private String mFileName = null;


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

        recordDisplay = (TextView) findViewById(R.id.dislayRecordView);
        recordButton = (Button) findViewById(R.id.recordButton);
        mFileName = Environment.getExternalStorageDirectory().getAbsolutePath();
        mFileName += "/SKBeon_recording.3gp";

        recordButton.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                if(motionEvent.getAction()==MotionEvent.ACTION_DOWN){
                    startRecording();
                    recordDisplay.setText("Recording Started!");
                }else if (motionEvent.getAction() == MotionEvent.ACTION_UP){
                    stopRecording();
                    recordDisplay.setText("Recording Sopped!");
                }
                return false;
            }
        });

    }

    private void startRecording() {
        mRecorder = new MediaRecorder();
        mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        mRecorder.setOutputFile(mFileName);
        mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

        try {
            mRecorder.prepare();
        } catch (IOException e) {
            Log.e(LOG_TAG, "prepare() failed");
        }

        mRecorder.start();
    }

    private void stopRecording() {
        mRecorder.stop();
        mRecorder.release();
        mRecorder = null;
    }
}

Xml main:

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="match_parent"
    android:layout_width="match_parent">

    <TextView
        android:id="@+id/dislayRecordView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/recordButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Record"
         />

</LinearLayout>

logcat的:

- 09-04 01:28:51.618 22535-22535/english_malay.my.apps.com.recordaudio E/MediaRecorder: stop failed: -1007


- 09-04 01:28:51.619 22535-22535/english_malay.my.apps.com.recordaudio E/InputEventReceiver: Exception dispatching input event.
- 09-04 01:28:51.619 22535-22535/english_malay.my.apps.com.recordaudio E/MessageQueue-JNI: Exception in MessageQueue callback: handleReceiveCallback
- 09-04 01:28:51.621 22535-22535/english_malay.my.apps.com.recordaudio E/MessageQueue-JNI: java.lang.RuntimeException: stop failed.

  * at android.media.MediaRecorder.stop(Native Method)

  * at english_malay.my.apps.com.recordaudio.MainActivity.stopRecording(MainActivity.java:70)


  * at english_malay.my.apps.com.recordaudio.MainActivity.access$100(MainActivity.java:18)


- 09-04 01:28:51.621 22535-22535/english_malay.my.apps.com.recordaudio D/AndroidRuntime: Shutting down VM


- 09-04 01:28:51.622 22535-22535/english_malay.my.apps.com.recordaudio E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                       - Process: english_malay.my.apps.com.recordaudio, PID: 22535
                                                                                     java.lang.RuntimeException: stop failed.


   * at android.media.MediaRecorder.stop(Native Method)



   * at english_malay.my.apps.com.recordaudio.MainActivity.stopRecording(MainActivity.java:70)

4 个答案:

答案 0 :(得分:2)

只是因为你试图阻止非录音录音机!

你应该在停止录音机前进行检查。

  private void startRecording() {
        mRecorder = new MediaRecorder();
        mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        mRecorder.setOutputFile(mFileName);
        mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

        try {
            mRecorder.prepare();
        } catch (IOException e) {
            Log.e(LOG_TAG, "prepare() failed");
        }
        isRecording = true;
        mRecorder.start();
    }

和:

private void stopRecording() {
      try{
       if(mRecorder!=null && isRecording){
         mRecorder.stop();
         mRecorder.release();
         mRecorder = null;
       }
     }catch (Exception e){//handle this exception} 
    }

此外,您应该在开始录制之前添加一些延迟时间(例如1秒):action == MotionEvent.ACTION_DOWN,以区分clickclick&hold

答案 1 :(得分:1)

将您的stopRecording()内容包装在try / catch语句

答案 2 :(得分:1)

  

当调用stopRecording时,你的mRecorder为null。进行以下更改

  private void stopRecording() {
    if(mRecorder!=null ){
            mRecorder.stop();
            mRecorder.release();
            mRecorder = null;
        }
}

答案 3 :(得分:1)

在调用mRecorder方法之前,您必须检查stop()对象是否为空。由于对象为null,因此崩溃。

private void stopRecording() {
        if(null != mRecorder){
            mRecorder.stop();
            mRecorder.release();
            mRecorder = null;
        }
}