每次按下按钮时如何创建Crete新文件?

时间:2017-03-04 08:48:27

标签: android file storage audio-recording

我只掌握有关android的基本知识。今天我开发了一个录音应用程序,但问题是用户可以录制音频并且可以播放音频,当它第二次运行时(或用户再次按下录制按钮)新录制的文件将替换旧存储的文件,我想为每个录制操作创建新文件,该怎么做?

这是我的主要活动

package com.hackerinside.jaisonjoseph.sample_recorder;

   import android.media.MediaPlayer;
   import android.media.MediaRecorder;
   import android.os.Bundle;
   import android.os.Environment;
   import android.provider.MediaStore;
   import android.support.design.widget.FloatingActionButton;
   import android.support.design.widget.Snackbar;
   import android.support.v7.app.AppCompatActivity;
   import android.support.v7.widget.Toolbar;
   import android.view.View;
   import android.view.Menu;
   import android.view.MenuItem;
   import android.widget.Button;
   import android.widget.Toast;

    import java.io.IOException;

public class MainActivity extends AppCompatActivity {

private Button play,stop,record;
private MediaRecorder myAudioRecorder;
private String outputfile;


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

    play=(Button)findViewById(R.id.play);
    stop=(Button)findViewById(R.id.stop);
    record=(Button)findViewById(R.id.record);
    stop.setEnabled(false);
    play.setEnabled(false);


    outputfile= Environment.getExternalStorageDirectory().getAbsolutePath()+"/recording.mp3";


    myAudioRecorder=new MediaRecorder();
    myAudioRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    myAudioRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
    myAudioRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
    myAudioRecorder.setOutputFile(outputfile);


 record.setOnClickListener(new View.OnClickListener() {
     @Override
     public void onClick(View view) {

         try {

             myAudioRecorder.prepare();
             myAudioRecorder.start();
     }
     catch (IllegalStateException ise){



     }catch (IOException ioe){



         }
            record.setEnabled(false);
            stop.setEnabled(true);
            Toast.makeText(getApplicationContext(),"record startded",Toast.LENGTH_LONG).show();

     }

 });





    stop.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {



            myAudioRecorder.stop();
            record.setEnabled(true);
            myAudioRecorder.release();
            myAudioRecorder=null;
            stop.setEnabled(false);
            play.setEnabled(true);
            Toast.makeText(getApplicationContext(),"recorded audio",Toast.LENGTH_LONG).show();

        }

    });
            play.setOnClickListener(new View.OnClickListener() {
                 @Override
                public void onClick(View view) {
                     MediaPlayer mediaPlayer=new MediaPlayer();

                     try {
                             mediaPlayer.setDataSource(outputfile);
                             mediaPlayer.prepare();
                             mediaPlayer.start();
                             Toast.makeText(getApplicationContext(),"playing  audio",Toast.LENGTH_LONG).show();
    }
    catch (Exception e)
    {
 // here
    }

}
   });








    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;











}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
}

2 个答案:

答案 0 :(得分:1)

使用Calendar.getInstance()。getTimeInMillis()为每个录制创建唯一的文件名。

outputfile=Environment.getExternalStorageDirectory().getAbsolutePath()+"/recording_"+ Calendar.getInstance().getTimeInMillis()+".mp3";

还创建一个功能来创建新的音频文件并使用新的文件引用初始化记录器。

private void prepareNewRecording() {
   outputfile=Environment.getExternalStorageDirectory().getAbsolutePath()+"/recording_"+ Calendar.getInstance().getTimeInMillis()+".mp3";
   myAudioRecorder=newMediaRecorder();
   myAudioRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
   myAudioRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
   myAudioRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
   myAudioRecorder.setOutputFile(outputfile);
}

现在从开始录制中调用上述功能

prepareNewRecording();
myAudioRecorder.prepare();
myAudioRecorder.start();

答案 1 :(得分:1)

在您的录制按钮上,单击“使用此行

outputfile= Environment.getExternalStorageDirectory().getAbsolutePath()+"/recording_"+System.currentTimeMillis()+".mp3";

如果您提供相同的文件名,该文件将覆盖旧文件。