3

我有两个按钮,一个用于开始录制,另一个用于停止录制。我成功录制声音并存储在 sdcard 中:

现在我想要的是,如果我在 15 秒之前按下停止按钮,我应该保持原样,但如果录制时间超过 15 秒,它应该自动停止录制并将录制的文件存储在 sd 卡中:

我的录音代码在这里:

**

public void startRecording(View view) throws IOException {
        startButton.setEnabled(false);
        stopButton.setEnabled(true);
        File sampleDir = Environment.getExternalStorageDirectory();
        try {
            //audiofile = File.createTempFile("sound", ".aac", sampleDir);
            audiofile = File.createTempFile("sound", ".m4a", sampleDir);
        } catch (IOException e) {
            Log.e(TAG, "sdcard access error");
            return;
        }
        recorder = new MediaRecorder();
        recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
        recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
        recorder.setOutputFile(audiofile.getAbsolutePath());
        recorder.prepare();
        recorder.start();


    }

**

并且停止录制是

public void stopRecording(View view) {

        startButton.setEnabled(true);
        stopButton.setEnabled(false);
        recorder.stop();
        recorder.release();

    } 

我该怎么办 ?

4

2 回答 2

5

基本上你需要为此实现 TimeTask 。

 if (view.getId() == R.id.stop) {
            new Timer().schedule(new TimerTask() {

                @Override
                public void run() {
                    runOnUiThread(new Runnable() {    
                        @Override
                        public void run() {
                            mediaRecorder.stop();
                            mediaRecorder.reset();
                            mediaRecorder.release();
                        }
                    });

                }

            }, 15000); //<-- Execute code after 15000 ms i.e after 15 Seconds.

        }
于 2012-06-12T06:08:25.017 回答
0

Android 多媒体框架支持捕获和编码各种常见的音频和视频格式。如果设备硬件支持,您可以使用 MediaRecorder API。

package com.kapil.admin.scrollapplication;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;

import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;

public class AudioRecordTest extends AppCompatActivity {

    private static final String LOG_TAG = "AudioRecordTest";
    private static final int REQUEST_RECORD_AUDIO_PERMISSION = 200;
    private static String mFileName = null;

    private RecordButton mRecordButton = null;
    private MediaRecorder mRecorder = null;
    String file;
    private MediaPlayer   mPlayer = null;

    int i=0;
    boolean mRecord=true;
    Timer timer;

    // Requesting permission to RECORD_AUDIO
    private boolean permissionToRecordAccepted = false;
    private String [] permissions = {Manifest.permission.RECORD_AUDIO};

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode){
            case REQUEST_RECORD_AUDIO_PERMISSION:
                permissionToRecordAccepted  = grantResults[0] == PackageManager.PERMISSION_GRANTED;
                break;
        }
        if (!permissionToRecordAccepted ) finish();

    }

    private void onRecord(boolean start,int i) {
        if (start) {
            startRecording(mFileName+"/audiorecordtest"+i+".3gp");
        } else {
            stopRecording();// stop recording
            startRecording(mFileName+"/audiorecordtest"+i+".3gp");//imeadiatly start recording
        }
    }


    private void startRecording(String file) {
        mRecorder = new MediaRecorder();
        mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        mRecorder.setOutputFile(file);
        mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

        try {
            mRecorder.prepare();
        } catch (IOException e) {
            Log.e(LOG_TAG, "prepare() failed");
        }

        mRecorder.start();
    }

    private void stopRecording() {
        mRecorder.stop();
        mRecorder.release();
        mRecorder = null;
    }

    class RecordButton extends android.support.v7.widget.AppCompatButton {
        boolean mStartRecording = true;

        OnClickListener clicker = new OnClickListener() {
            public void onClick(View v) {

                if(!mRecord)
                {
                    if(timer!=null)
                    {
                        timer.cancel();
                        stopRecording();
                        mRecord=true;
                    }
                }


                mRecord=false;
                // start record,at every 10 seconds
                 timer = new Timer();
                timer.schedule(new TimerTask() {
                    @Override
                    public void run() {

                        i++;

                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                onRecord(mStartRecording,i);
                                mStartRecording = !mStartRecording; // setting false



                            }
                        });

                    }
                },0,10000);

            }
        };

        public RecordButton(Context ctx) {
            super(ctx);
            setText("Start recording");
            setOnClickListener(clicker);
        }
    }



    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);

        // Record to the external cache directory for visibility
        mFileName = getExternalCacheDir().getAbsolutePath();
       // mFileName += "/audiorecordtest"+i+".3gp";

        ActivityCompat.requestPermissions(this, permissions, REQUEST_RECORD_AUDIO_PERMISSION);

        LinearLayout ll = new LinearLayout(this);
        mRecordButton = new RecordButton(this);
        ll.addView(mRecordButton,
                new LinearLayout.LayoutParams(
                        ViewGroup.LayoutParams.WRAP_CONTENT,
                        ViewGroup.LayoutParams.WRAP_CONTENT,
                        0));
    }

    @Override
    public void onStop() {
        super.onStop();
        if (mRecorder != null) {
            mRecorder.release();
            mRecorder = null;
        }

        if (mPlayer != null) {
            mPlayer.release();
            mPlayer = null;
        }
    }
}

根据您的要求修改类

对于基本概念验证此链接

https://developer.android.com/guide/topics/media/mediarecorder.html#audio-record-permission

于 2017-10-26T06:06:16.897 回答