录制音频并保存到应用程序的数据文件夹中

时间:2013-01-11 03:18:49

标签: android audio storage internal recording

我对编程很陌生,我想学习如何做一个允许用户录制音频的应用程序,因此将其保存到应用程序的数据文件夹中。我设法做录音部分,但只设法将其保存到SD卡..任何人愿意帮我弄清楚如何将我的音频保存到内部存储?

private void playRecording() throws Exception {
    ditchMediaPlayer();
    mediaPlayer = new MediaPlayer();
    mediaPlayer.setDataSource(OUTPUT_FILE);
    mediaPlayer.prepare();
    mediaPlayer.start();
}

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

private void beginRecording() throws Exception {
    ditchMediaRecorder();
    File outFile = new File(OUTPUT_FILE);

    if(outFile.exists())
        outFile.delete();

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

    recorder.prepare();
    recorder.start();

}

private void ditchMediaRecorder() {
    if (recorder != null)
        recorder.release();
}

  public void recordOnClick(View v) {
      //when record button is pressed
      try{
            beginRecording();
        }catch (Exception e){
            e.printStackTrace();
        }
        btnRecord.setVisibility(View.INVISIBLE);
        btnStop.setVisibility(View.VISIBLE);
  }

  public void stopOnClick(View v) {
      //when stop button is pressed
      try{
            stopRecording();
        }
        catch (Exception e){
            e.printStackTrace();
        }

        btnStop.setVisibility(View.INVISIBLE);
        btnRecord.setVisibility(View.INVISIBLE);

        btnDelete.setVisibility(View.VISIBLE);
        btnPlay.setVisibility(View.VISIBLE);
        btnShare.setVisibility(View.VISIBLE);
  }

  public void playOnClick(View v) {
      //when play button is pressed
        try{
            playRecording();
        }
        catch (Exception e){
            e.printStackTrace();
        }

        btnStop.setVisibility(View.INVISIBLE);
        btnRecord.setVisibility(View.INVISIBLE);
  }

这是我的onClick,我将指定每个按钮的输出(即时图表,所以我有大约44个按钮!)

    public void onClick(View v) {
switch (v.getId()) {

case R.id.btn1:

      OUTPUT_FILE=Environment.getExternalStorageDirectory()+"/y1.3gpp";     
  File file1 = new File(Environment.getExternalStorageDirectory()+"/y1.3gpp");
        if (file1.exists()) {

            btnPlay.setVisibility(View.VISIBLE);
            btnDelete.setVisibility(View.VISIBLE);
            btnShare.setVisibility(View.VISIBLE);
        }
        else {
            btnRecord.setVisibility(View.VISIBLE);
        }   
 break;

case R.id.btn2:

OUTPUT_FILE=Environment.getExternalStorageDirectory()+"/y2.3gpp";   
File file2 = new File(Environment.getExternalStorageDirectory()+ "/y2.3gpp" );
        if (file2.exists()) {

            btnPlay.setVisibility(View.VISIBLE);
            btnDelete.setVisibility(View.VISIBLE);
            btnShare.setVisibility(View.VISIBLE);
        }
        else {
            btnRecord.setVisibility(View.VISIBLE);
        }   

 break;

3 个答案:

答案 0 :(得分:4)

也许你想看看这个? Creating folder in internal Memory to save files and retrieve them later

//Creating an internal directory
File mydir = context.getDir("mydir", Context.MODE_PRIVATE); 
//Getting a file within the dir.
File fileWithinMyDir = new File(mydir, "myfile"); 
FileOutputStream out = new FileOutputStream(fileWithinMyDir); 
//Use the stream as usual to write    into the file

答案 1 :(得分:1)

您需要在清单中添加此使用权限

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

将此用作SD卡的路径

File sdcard = Environment.getExternalStorageDirectory();
File storagePath = new File(sdcard.getAbsolutePath() + "/folderName");
File userimage = new File(storagePath + "/" + fileName + ".3gpp");

答案 2 :(得分:0)

建议将文件存储在数据目录中。 (即sdcard / Android / data / yourpackage /) 这样,一旦用户卸载您的应用程序,就可以自动删除该文件夹。

您可以使用以下代码获取数据文件夹: (如果你的Build.VERSION.SDK_INT小于Build.VERSION_CODES.FROYO)

Environment.getExternalStorageDirectory() +"/Android/data/" +context.getApplicationContext().getPackageName());

(如果你的Build.VERSION.SDK_INT大于Build.VERSION_CODES.FROYO)

context.getExternalFilesDir(Context.STORAGE_SERVICE)