如何暂停和恢复录音。在此,如果我暂停录制并再次开始录制,那么它应该从暂停的位置开始并保存在SD卡中。
答案 0 :(得分:0)
试试这个............
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();
答案 1 :(得分:0)
使用以下代码,请记住在AndroidManifest.xml中添加RECORD_AUDIO和WRITE_EXTERNAL_STORAGE权限。
在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>
在Java代码中:
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();
}
}