如何在android中保存带有客户文件名的录制文件

时间:2016-10-14 10:40:26

标签: android audio audio-recording

when user click on stop recording button open popup and ask for file name enter file name then save to sdcard.right now i don't how to do it below my code..please guys help

在此代码中定义4个按钮和xml文件,当用户点击开始按钮开始录制时,用户点击停止按钮停止录制然后类似播放和停止播放按钮 但问题是如何保存具有特定名称的特定音频文件

Audio.java
public class HomeAdminfgt extends Fragment {
    private MediaRecorder myRecorder;
    private MediaPlayer myPlayer;
    private String outputFile = null;
    private Button startBtn;
    private Button stopBtn;
    private Button playBtn;
    private Button stopPlayBtn, Showaudio;
    private TextView text;
    private ImageView mic_image;
    private long startTime = 0L;
    private Handler customHandler = new Handler();
    long timeInMilliseconds = 0L;
    long timeSwapBuff = 0L;
    long updatedTime = 0L;
    TextView timervalue;
    File file;
    private String[] FilePathStrings;
    private String[] FileNameStrings;
    private File[] listFile;
    ProgressDialog progressDialog;
    public static String AudioUrl = "http://1biz.in/voice-call/voice.php?method=voiceCall";
    public static String voice_extra = "voice-call";
    public static String voice_string = "recording";

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_home_adminfgt, container, false);
        Showaudio = (Button) view.findViewById(R.id.showaudio);
//        Showaudio.setOnClickListener();
        if (!Environment.getExternalStorageState().equals(
                Environment.MEDIA_MOUNTED)) {

            Toast.makeText(getActivity(), "Error! No SDCARD Found!", Toast.LENGTH_LONG).show();
        } else {
            // Locate the image folder in your SD Card
            file = new File(Environment.getExternalStorageDirectory()
                    + File.separator + "Amuwa");
            // Create a new folder if no folder named SDImageTutorial exist
            file.mkdirs();
        }

        //File file = new File(dir, "recordvoice");
        text = (TextView) view.findViewById(R.id.text1);
        mic_image = (ImageView) view.findViewById(R.id.micImage);
      //  timervalue = (TextView) view.findViewById(R.id.timerValue);
        java.util.Date date = new java.util.Date();
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss")
                .format(date.getTime());
        outputFile = Environment.getExternalStorageDirectory().
                getAbsolutePath() + "/Amuwa/" + "AUD_" + timeStamp + ".wav";

        myRecorder = new MediaRecorder();
        myRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        myRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        myRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
        myRecorder.setOutputFile(outputFile);
        startBtn = (Button) view.findViewById(R.id.start);
        startBtn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                start(v);
            }
        });

        stopBtn = (Button) view.findViewById(R.id.stop);
        stopBtn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v)
            {
                // TODO Auto-generated method stub
                stop(v);
            }
        });

        playBtn = (Button) view.findViewById(R.id.play);
        playBtn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v)
            {
                // TODO Auto-generated method stub
                play(v);
            }
        });

        stopPlayBtn = (Button) view.findViewById(R.id.stopPlay);
        stopPlayBtn.setOnClickListener(new View.OnClickListener()
        {

            @Override
            public void onClick(View v)
            {
                // TODO Auto-generated method stub
                stopPlay(v);
            }
        });
        return view;
    }





    public void start(View view)
    {
        try
        {
            myRecorder.prepare();
            myRecorder.start();
         //   startTime = SystemClock.uptimeMillis();
           // customHandler.postDelayed(updateTimerThread, 0);
            Animation animation = new AlphaAnimation(1, 0); // Change alpha
            animation.setDuration(500); // duration - half a second
            animation.setRepeatCount(Animation.INFINITE); // Repeat animation
            animation.setRepeatMode(Animation.REVERSE); // Reverse animation at

            mic_image.startAnimation(animation);
        } catch (IllegalStateException e)
        {
            // start:it is called before prepare()
            // prepare: it is called after start() or before setOutputFormat()
            e.printStackTrace();
        } catch (IOException e) {
            // prepare() fails
            e.printStackTrace();
        }

        text.setText("Recording Point: Recording");
        startBtn.setEnabled(false);
        stopBtn.setEnabled(true);

        Toast.makeText(getActivity(), "Start recording...",
                Toast.LENGTH_SHORT).show();
    }

    public void stop(View view)
    {
        try
        {

            myRecorder.stop();
            myRecorder.reset();
            myRecorder.release();
            stopBtn.setEnabled(false);
            playBtn.setEnabled(true);
            myRecorder  = null;

            mic_image.clearAnimation();
            text.setText("Recording Point: Stop recording");

            Toast.makeText(getActivity(), "Stop recording...",
                    Toast.LENGTH_SHORT).show();
        } catch (IllegalStateException e)
        {
            //  it is called before start()
            e.printStackTrace();
        } catch (RuntimeException e) {
            // no valid audio/video data has been received
            e.printStackTrace();
        }
    }

    public void play(View view)
    {
        try{
            myPlayer = new MediaPlayer();
            myPlayer.setDataSource(outputFile);
            myPlayer.prepare();
            myPlayer.start();

            playBtn.setEnabled(false);
            stopPlayBtn.setEnabled(true);
            text.setText("Recording Point: Playing");
            Toast.makeText(getActivity(), "Start play the recording...",
                    Toast.LENGTH_SHORT).show();
        }
        catch (Exception e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public void stopPlay(View view)
    {

        try
        {
            if (myPlayer != null)
            {

                myPlayer.stop();
                myPlayer.release();
                myPlayer = null;
                playBtn.setEnabled(true);
                stopPlayBtn.setEnabled(false);
                text.setText("Recording Point: Stop playing");

                Toast.makeText(getActivity(), "Stop playing the recording...",
                        Toast.LENGTH_SHORT).show();
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

1 个答案:

答案 0 :(得分:0)

何时可以创建一个方法来保存文件,并提供用户名。当用户按下停止按钮时调用该方法。以下是示例代码:

private void saveRecording() {
    LayoutInflater li = LayoutInflater.from(this);
    View promptsView = li.inflate(R.layout.set_file_name_dialog, null);

    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);

    // set prompts.xml to alertdialog builder
    alertDialogBuilder.setView(promptsView);

    final EditText userInput = (EditText) promptsView.findViewById(R.id.editTextDialogUserInput);
    Button cancel = (Button) promptsView.findViewById(R.id.save_cancel);
    Button ok = (Button) promptsView.findViewById(R.id.save_ok);


    // set dialog message
    alertDialogBuilder
            .setCancelable(false);

    // create alert dialog
    final AlertDialog alertDialog = alertDialogBuilder.create();
    cancel.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            alertDialog.dismiss();
        }
    });
    ok.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String newFileName = userInput.getText().toString();
            if (newFileName != null && newFileName.trim().length() > 0) {
                File newFile = new File(outputFileDir, newFileName);
                File oldFile = new File(outputFileDir, oldFileName);
                oldFile.renameTo(newFile);
                alertDialog.dismiss();
            }
        }
    });
    // show it
    alertDialog.show();

}

这是AlertDialog的布局文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout_root"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="10dp" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Enter file name : "
    android:textAppearance="?android:attr/textAppearanceLarge" />

<EditText
    android:id="@+id/editTextDialogUserInput"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="20dp">

    <requestFocus />

</EditText>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:weightSum="2"
    android:layout_marginTop="20dp">
    <Button
        android:id="@+id/save_cancel"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Cancel"
        android:layout_weight="1"/>
    <Button
        android:id="@+id/save_ok"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="OK"
        android:layout_weight="1"/>
</LinearLayout>

</LinearLayout>