1

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

3 回答 3

4

也许你想看看这个?在内部存储器中创建文件夹以保存文件并稍后检索它们

//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
于 2013-01-11T08:24:41.557 回答
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");
于 2013-01-11T05:33:32.247 回答
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)
于 2013-11-19T06:31:12.097 回答