2017-09-03 128 views
0

我目前正在嘗試長時間錄製音頻並按住。當我點擊並按住該應用程序時,該應用程序仍然有效,但是,當我單擊該按鈕時,該應用程序會崩潰。請幫我弄清楚爲什麼會發生這種情況。應用程序使用onTouch,崩潰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主:

<?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) 

回答

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區分

+0

好,它的工作原理!最後一件事,你能告訴我如何在開始錄製之前延遲或者任何鏈接都可以做到。謝謝你,Atef Hares。 –

+0

@ S.k.Surain不用客氣。你可以使用這個[answer](https://stackoverflow.com/a/9166354/5993410)這是一個簡單的方法來延遲執行一些代碼。 –

+0

我也很好奇。它從技術上調用Action_down(它又調用startRecording())是如何不記錄的?再次感謝 –

1

裹在try/catch語句您stopRecording()內容

1

您的mRecorder在調用stopRecording時爲null。做以下修改

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

在調用stop()方法之前,您必須檢查mRecorder對象是否爲null它。由於該對象爲空,因此您將遇到崩潰。

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