0

此問題已被問到,但無法找到我的解決方案。嘗試在setOnTouchListener上錄製音頻時,應用程序崩潰

我在做什麼,MotionEvent.ACTION_DOWNOnTouchListener我開始錄製音頻,並在MotionEvent.ACTION_UP我停止錄音。

長時間觸摸該圖標時,它工作正常,但onclick應用程序崩潰。 這裏是我的

audio.setOnClickListener(new View.OnClickListener(){ 
     @Override 
     public void onClick(View v) { 
      Toast.makeText(Chat.this, "You Have to hold the Button for audio recording.", Toast.LENGTH_LONG).show(); 
     } 
    }); 

audio.setOnTouchListener(new View.OnTouchListener() { 
     @Override 
     public boolean onTouch(View v, MotionEvent event) { 
      if (event.getAction() == MotionEvent.ACTION_DOWN) { 
       MediaRecorderReady(); 
       try { 
        mediaRecorder.prepare(); 
        mediaRecorder.start(); 
       } catch (IllegalStateException e) { 
        e.printStackTrace(); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 

      } else if (event.getAction() == MotionEvent.ACTION_UP) { 
       if (mediaRecorder != null) { 
        mediaRecorder.stop(); 
       } 
      } 
      return true; 
     } 
    }); 

public void MediaRecorderReady() { 
    mediaRecorder = new MediaRecorder(); 
    mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); 
    mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); 
    mediaRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB); 
    mediaRecorder.setOutputFile(AudioSavePathInDevice); 
} 

這裏是我的問題,當我點擊該按鈕的應用程序崩潰

+4

在此處發佈錯誤日誌 – UltimateDevil

+0

java.lang.RuntimeException:stop failed。在android.media.MediaRecorder.stop(本地方法) – manjari

+0

請將完整的logcat和檢查,當你按onclick onTouch稱爲ActionDown和ActionUp也被稱爲。 –

回答

3

檢查這個答案 Android mediarecorder stop failed 也閱讀mediaRecorder及其狀態的官方文檔。 https://developer.android.com/reference/android/media/MediaRecorder.html 由於您停止媒體播放器和媒體播放器未處於錄製狀態而導致崩潰。 未處於錄製狀態時無法直接呼叫停止。

try { 
    mRecorder.stop(); 
    } 
    catch(RuntimeException e) { 
    } 
     finally { 
       mRecorder.release(); 
       mRecorder = null; 
    } 
+0

謝謝。這個嘗試catch塊解決了崩潰的應用程序。 – manjari

+0

你可以告訴我如何顯示烤麪包,當我只點擊按鈕和音頻將長時間按下記錄? – manjari

+1

這工作得很好 – manjari

0

做像下面。

private MediaRecorder myAudioRecorder; 
    private MediaPlayer mPlayer; 

audio.setOnTouchListener(new View.OnTouchListener() { 
     @Override 
     public boolean onTouch(View v, MotionEvent event) { 
      if (event.getAction() == MotionEvent.ACTION_DOWN) { 
       initializeRecorder();     
       recordFile(); 

      } else if (event.getAction() == MotionEvent.ACTION_UP) { 
       stopRecording(); 
      } 
      return true; 
     } 
    }); 

private void initializeRecorder() { 
     File cacheDir = new File(Utils.SD_CARD, Utils.RECORDING_CACHE); 
     if (!(cacheDir.exists())) { 
      cacheDir.mkdirs(); 
     } 

     outputFile = cacheDir + "/" + String.format("%s.m4a", System.currentTimeMillis()); 

     mPlayer = new MediaPlayer(); 
     myAudioRecorder = new MediaRecorder(); 
     myAudioRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); 

     //original 
     myAudioRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4); 
     myAudioRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB); 
     myAudioRecorder.setOutputFile(outputFile); 

    } 

private void recordFile() { 
     try { 
      myAudioRecorder.prepare(); 
      myAudioRecorder.start(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 

private void stopRecording(){ 
    myAudioRecorder.stop(); 
    myAudioRecorder.release(); 
} 
+0

我的代碼工作正常,當我長時間觸摸,然後刪除 – manjari

+0

然後是什麼問題? –

+0

當我剛剛觸摸音頻按鈕,然後應用程序崩潰。長按並釋放音頻錄音良好 – manjari

相關問題