2015-10-23 210 views
0

我想將錄製的音頻保存到/ storage/emulated/0/AudioRecorder,這是我的代碼:錄製的音頻無法保存在/ storage/emulated/0/AudioRecorder(有時保存的文件出現,有時不出現)

private String getFilename() { 
     File file = new File(Environment.getExternalStorageDirectory(), "/AudioRecorder"); 
     if (!file.exists()) { 
      file.mkdirs(); 
     } 

     createdDate = DateTools.getDate(System.currentTimeMillis(),"dd-MMM-yyyy hh:mm:ss"); 
     nameFile = createdDate + ".mp4"; 

     pathFile = (file.getAbsolutePath() + "/" + nameFile); 
     return pathFile; 
    } 

private void startRecording() { 
     if(RadioManager.getInstance().isPlaying()){ 
      RadioManager.getInstance().stopPlayer(); 
     } 
     Toast.makeText(VoiceNoteActivity.this, "Mulai Rekam", Toast.LENGTH_SHORT).show(); 
     recorder = new MediaRecorder(); 
     recorder.setAudioSource(MediaRecorder.AudioSource.MIC); 
     recorder.setOutputFormat(output_formats); 
     recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); 
     recorder.setOutputFile(getFilename()); 
     recorder.setOnErrorListener(errorListener); 
     recorder.setOnInfoListener(infoListener); 
     try { 
      recorder.prepare(); 
      recorder.start(); 
      isRecord = true; 
      countDown.start(); 
     } catch (IllegalStateException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 

    private void stopRecording() { 
     if (null != recorder) { 
      countDown.cancel(); 

      timer = 0; 
      seekbar.setProgress(timer); 
      try{ 
       recorder.stop(); 
       recorder.reset(); 
       recorder.release(); 
       recorder = null; 
      }catch(RuntimeException stopException){ 
       //handle cleanup here 
       Log.e("Voice Note Activity ","Voice Recording Error : " +stopException.getMessage()); 
      } 

      isRecord = false; 
      time.setText("00:00"); 
      enableButtons(false); 
     } 
    } 

的文件有時會出現在/存儲/模擬/ 0/AudioRecorder,但有時不顯示(更經常地不會出現)。任何人都可以幫忙?

+0

可以給你一些想法如何解決它現在我面臨同樣的問題@Redturbo –

回答

0

我已經通過將此代碼解決了這個:

File file = new File(getFilename()); 
     if (!file.exists()){ 
      try { 
       file.createNewFile(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 

所以,當該文件是存在的,它並不需要重新創建該文件,但如果保存的文件不存在,它需要創建文件。

0

下襬..我有同樣的問題,但與圖片不聲音。在我的情況下,這張照片沒有出現,因爲我沒有把它扔到畫廊。基於Android開發者網站上的教程是它的圖片庫的代碼:

private void galleryAddPic() { 
    Intent mediaScanIntent = new Intent("android.intent.action.MEDIA_SCANNER_SCAN_FILE"); 
    File f = new File(fileUri.getPath()); 
    Uri contentUri = Uri.fromFile(f); 
    mediaScanIntent.setData(contentUri); 
    this.sendBroadcast(mediaScanIntent); 
} 

可能是你可以使用它來語音文件。但我沒有嘗試語音文件。

+0

謝謝@mas_bejo,我從線索:文件f =新的文件(fileUri.getPath());但問題是創建文件不存在時,你的答案給了我一個線索.. – Redturbo

+0

可以給你一些想法,如何解決它現在我面臨同樣的問題@Redturbo –

相關問題