2012-01-28 35 views
2

我試圖將錄製的音頻文件保存在我想讓它成爲默認文件夾的文件夾中。但不知何故,我沒有這樣做。如何以編程方式將錄製的音頻文件保存在另一個文件夾中?

我的代碼:

Intent recordIntent = new Intent(MediaStore.Audio.Media.RECORD_SOUND_ACTION); 
Uri mUri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(), "/Record/sound_"+ String.valueOf(System.currentTimeMillis()) + ".amr")); 
recordIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, mUri); 
startActivityForResult(recordIntent, RESULT_OK); 

它確實調用錄音筆的應用程序。還有當我按下停止按鈕時,它會返回到我的應用程序,並有一個吐司出現說它保存。但是,而不是保存在我的記錄文件夾中,它保存在默認文件夾中。

我意識到有在logcat的錯誤信息:

01-29 01:34:23.900: E/ActivityThread(10824): Activity com.sec.android.app.voicerecorder.VoiceRecorderMainActivity has leaked ServiceConnection [email protected]e7c8 that was originally bound here 

我不知道的代碼工作時,我調用攝像頭應用的哪些方面出了問題。

回答

0

我找到了一種解決這個問題的方法,即使它需要一輪完成而不是直接完成,但是它是我獲得的最好結果,同時也是有效的。的

而不是調用額外的錄音筆應用程序在內,我只是把它在沒有任何輸入:

Intent recordIntent = new Intent(MediaStore.Audio.Media.RECORD_SOUND_ACTION); 
startActivityForResult(recordIntent, 1111); 

然後,添加一個onActivityResult,與請求代碼== 1111(取決於你放什麼)和檢索的最後修改的文件,包括從記錄的默認文件夾的擴展名「3GA」的「聲音」

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) 
{ 
    super.onActivityResult(requestCode, resultCode, data); 

    if(requestCode == 1111) 
    { 
     File folder = new File(Environment.getExternalStorageDirectory(), "/Sounds"); 
     long folderModi = folder.lastModified(); 

    FilenameFilter filter = new FilenameFilter() 
    { 
     public boolean accept(File dir, String name) 
     { 
      return (name.endsWith(3ga)); 
     } 
    }; 

    File[] folderList = folder.listFiles(filter); 

    String recentName = ""; 

    for(int i=0; i<folderList.length;i++) 
    { 
     long fileModi = folderList[i].lastModified(); 

     if(folderModi == fileModi) 
     { 
      recentName = folderList[i].getName(); 
     } 
    } 
} 

這種方式,我可以得到該文件的名稱,也做了修改(例如,重命名)用它。

希望這可以幫助其他人。 =)

9

做這種方式,記錄與MediaRecorder:

要開始記錄:

public void startRecording() 
     { 


       MediaRecorder recorder = new MediaRecorder(); 

       recorder.setAudioSource(MediaRecorder.AudioSource.MIC); 
       recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); 
       recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); 
       recorder.setOutputFile(getFilename()); 

       recorder.setOnErrorListener(errorListener); 
       recorder.setOnInfoListener(infoListener); 

       try 
       { 
         recorder.prepare(); 
         recorder.start(); 
       } 
       catch (IllegalStateException e) 
       { 
         e.printStackTrace(); 
       } catch (IOException e) 
       { 
         e.printStackTrace(); 
       } 
     } 

要停止:

private void stopRecording() 
    { 


      if(null != recorder) 
      {  
        recorder.stop(); 
        recorder.reset(); 
        recorder.release(); 
        recorder = null; 
      } 

對於選定的文件夾:

private String getFilename() 
     { 
       String filepath = Environment.getExternalStorageDirectory().getPath(); 
       File file = new File(filepath,AUDIO_RECORDER_FOLDER); 

       if(!file.exists()){ 
         file.mkdirs(); 
       } 

       return (file.getAbsolutePath() + "/" + System.currentTimeMillis() + ".mp3"); 
     } 
+0

呃...感謝,但我寧願調用默認的應用程序,因爲它有所有的GUI和方法我需要。 =) – Jovi 2012-01-28 10:13:19

+0

來吧和張貼您的需求在這裏http://chat.stackoverflow.com/rooms/5098/android-people – Abhi 2012-01-28 10:55:24

+0

只需添加'<使用權限android:name =「android.permission.RECORD_AUDIO」/>' – 2017-02-13 22:32:55

0

我以前用過這種方式,對我來說還行!

private MediaRecorder mRecorder = null; 
    public void startRecording() { 
     if (mRecorder == null) { 
      mRecorder = new MediaRecorder(); 
      mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); 
      mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); 
      mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); 
      mRecorder.setOutputFile(getFilename()); 
      try { 
       mRecorder.prepare(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
      mRecorder.start(); 
     } 
    } 

停止錄音:

 public void stopRecording() { 

     if (mRecorder != null) { 
      mRecorder.stop(); 
      timer.cancel(); 
      mRecorder.release(); 
      mRecorder = null; 
    } 
    } 

要保存文件:

 @SuppressLint("SdCardPath") 
    private String getFilename() { 
     file = new File("/sdcard", "MyFile"); 

     if (!file.exists()) { 
      file.mkdirs(); 
     } 

     return (file.getAbsolutePath() + "/" + System.currentTimeMillis() + ".mp3"); 
    } 

if you want to delete folder after recording use this in function of stopping: 

    boolean deleted = file.delete(); 

I hope it can be helpful. 
+0

它不適合我 – 2016-07-21 07:49:25

相關問題