Android 如何暂停和恢复音频录制

Android 如何暂停和恢复音频录制,android,Android,如何暂停和恢复音频录制。在此情况下,如果我暂停录制并再次开始录制,则应从暂停位置开始录制并保存在sd卡中。尝试此 MediaRecorder recorder = new MediaRecorder(); recorder.setAudioSource(MediaRecorder.AudioSource.MIC); recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); recorder.setAudioEncoder(Me

如何暂停和恢复音频录制。在此情况下,如果我暂停录制并再次开始录制,则应从暂停位置开始录制并保存在sd卡中。

尝试此

MediaRecorder recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(PATH_NAME);
recorder.prepare();
recorder.start();   
recorder.stop();
recorder.reset();    
recorder.release();

使用下面的代码,记住在AndroidManifest.xml中添加录制音频和写入外部存储权限

在XML布局中:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/btnRecord"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="Record" />

    <Button
        android:id="@+id/btnPlay"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="Play" />

</LinearLayout>

我已经用过了,但它再次从开始状态开始录制,而不是从暂停状态开始录制。我想从暂停状态开始。
public class MainActivity extends AppCompatActivity {

    private static final String TAG = "my_log";
    Button btnRecord, btnPlay;
    public static String audioPath = "";

    private MediaRecorder myAudioRecorder;
    private boolean isRecording = false;

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

        btnRecord = (Button) findViewById(R.id.btnRecord);
        btnPlay = (Button) findViewById(R.id.btnPlay);
        btnPlay.setEnabled(false);

        btnRecord.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!isRecording) {
                    btnPlay.setEnabled(false);

                    // If it exists, delete it
                    File file = new File(audioPath);
                    if (file.exists()) {
                        file.delete();
                    }

                    // this is directory path to store my audio
                    File dirPath = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/MyAudio/");
                    dirPath.mkdirs(); // make this as directory

                    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
                    String audioFileName = "audio_" + timeStamp + ".3gp";
                    audioPath = dirPath + "/" + audioFileName;
                    Log.i(TAG, "audioPath = " + audioPath);

                    isRecording = true;
                    btnRecord.setText("Stop");
                    recordAudio(); // call this method to record
                } else {
                    stopRecordAudio();
                    isRecording = false;
                    btnRecord.setText("Record");
                    btnPlay.setEnabled(true);
                }
            }
        });

        btnPlay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                playAudio(audioPath);
            }
        });
    }

    public void recordAudio() {
        try {
            myAudioRecorder = new MediaRecorder();
            myAudioRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
            myAudioRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
            myAudioRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
            myAudioRecorder.setOutputFile(audioPath);

            myAudioRecorder.prepare();
            myAudioRecorder.start();
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        Toast.makeText(getApplicationContext(), "is recording...", Toast.LENGTH_LONG).show();
    }

    public void stopRecordAudio() {
        myAudioRecorder.stop();
        myAudioRecorder.release();

        Toast.makeText(getApplicationContext(), "Audio recorded successfully", Toast.LENGTH_LONG).show();
    }

    public void playAudio(String audioPath) {
        MediaPlayer m = new MediaPlayer();

        try {
            m.setDataSource(audioPath);
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            m.prepare();
        } catch (IOException e) {
            e.printStackTrace();
        }

        m.start();
        Toast.makeText(MainActivity.this, "Playing audio...", Toast.LENGTH_SHORT).show();
    }

}