【问题标题】:Record audio and save into app's data folder录制音频并保存到应用程序的数据文件夹中
【发布时间】:2013-01-11 03:18:49
【问题描述】:

我对编程很陌生,我想学习如何制作一个允许用户录制音频并因此将其保存到应用程序的数据文件夹中的应用程序。我设法完成了录音部分,但只设法将其保存到了 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;

【问题讨论】:

    标签: android audio storage internal recording


    【解决方案1】:

    也许你想看看这个? 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
    

    【讨论】:

    • 这是完美的答案@Annabel!非常感谢(;
    • 好吧,别提了=)
    【解决方案2】:

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

    <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");
    

    【讨论】:

    • 嘿@Ivan Erl Elymar Cayaban erm.. 是的.. 上面的代码是可行的.. 我需要将它保存到我的数据文件夹.. 不是 sd 卡..
    • 对不起,你试过了吗link
    • 嗯..没关系!感谢您的链接和努力!会调查的^^
    【解决方案3】:

    建议将文件存储在您的数据目录中。 (即 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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-21
      • 2021-12-28
      相关资源
      最近更新 更多