【问题标题】:How to make a folder for my audios records files?如何为我的音频记录文件创建一个文件夹?
【发布时间】:2014-05-28 20:49:37
【问题描述】:

我正在开发一些具有录音功能的应用程序,我只想将它保存在我的外部存储中,现在我知道如何在没有文件夹的情况下保存它,但我希望我的应用程序为我录制的文件创建唯一的文件夹

我的代码:

public class MainActivity extends Activity {

MediaRecorder recorder = null;
MediaPlayer player = null;
public static String OUTPUT_FILE;


protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    OUTPUT_FILE = Environment.getExternalStorageDirectory()+"audiorecorder.acc";

}


public void record(View v) {
    try {

        beginRecording();

    } catch (IllegalStateException | IOException e) {
        e.printStackTrace();
    }
}


public void stop(View v) {
        stopRecording();
}


public void play(View v) {
    try {
        beginPlay();
    } catch (IllegalArgumentException | SecurityException
            | IllegalStateException | IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}


public void stopPlayback(View v) {
    StopPlay();
}





private void beginRecording() throws IllegalStateException, IOException {
    dictchMediaRecorder();      


    File file = new File(OUTPUT_FILE);


    if(file.exists()){
        file.delete();
    }


    recorder = new MediaRecorder();
    recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
    recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_WB);
    recorder.setAudioEncodingBitRate(9600);
    recorder.setAudioSamplingRate(44100);
    recorder.setOutputFile(OUTPUT_FILE);
    recorder.prepare();
    recorder.start();


}



private void beginPlay() throws IllegalArgumentException, SecurityException, IllegalStateException, IOException  {
    ditchMediaPlayer();
    player = new MediaPlayer();
    player.setDataSource(OUTPUT_FILE);
    player.prepare();
    player.start();

}

我只添加了这几行,而且效果很好:

File dir_image;

public static String OUTPUT_FILE;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


       dir_image = new  File(Environment.getExternalStorageDirectory()+File.separator+"folderName");
       dir_image.mkdirs();

      OUTPUT_FILE = dir_image.getPath()+"/acapella.wav";

【问题讨论】:

  • 干得好。祝你的应用好运
  • 谢谢哥们。你帮了我很多

标签: android mediarecorder android-external-storage


【解决方案1】:

添加这个:

 private File dir_image;

 String myfile="the name of the song.acc";

 dir_image = new  File(Environment.getExternalStorageDirectory()+
                            File.separator+"The name of your folder");
 dir_image.mkdirs();

 File tmpFile = new File(dir_image,myfile);

我想补充一点,您需要将以下内容添加到您的清单中

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

或者尝试将其添加到您的代码中: 而不是这个:

OUTPUT_FILE = Environment.getExternalStorageDirectory()+"audiorecorder.acc";

试试:

OUTPUT_FILE = Environment.getExternalStorageDirectory()+
                            File.separator+"The name of your folder"+
                            File.separator+"audiorecorder.acc";

【讨论】:

  • Tnx 但是呢:recorder.setOutputFile(OUTPUT_FILE);和 player.setDataSource(OUTPUT_FILE);??
  • 我认为您应该尝试 setOutputfile(tmpFile) 或 (Environment.getExternalStorageDirectory()+ File.separator+"您的文件夹名称"+ File.separator+"歌曲名称.acc");
  • 我不能因为:MediaRecorder 类型中的方法 setOutputFile(FileDescriptor) 不适用于参数(文件)
  • @Matt 您可以查看我的其他答案之一,我在其中回答了如何捕获、保存和图像。查看代码主要是 takescreenshot 和 onpicturetaken 方法,你会看到更多关于文件和输出流的信息。只需寻找代码最多的答案即可找到我的。
  • @Matt stackoverflow.com/questions/23921128/… 希望这会有所帮助:)
猜你喜欢
  • 1970-01-01
  • 2021-04-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-08
  • 2018-07-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多