我有一个示例应用程序,当我按下“开始”按钮时,它会启动MediaRecorder
和MediaPlayer
作为录制音乐的指导音乐。然后在按下“停止”按钮后,它将保存录制的音频。
然后,我有第三个按钮,可播放录制的音频和指南音频。问题是它们不同步。引导音频在录制的音频之前播放。
我的代码:
public class MainActivity extends Activity {
MediaRecorder recorder;
MediaPlayer mediaPlayer;
MediaPlayer mediaRecorded = new MediaPlayer();;
File audiofile = null;
private static final String TAG = "SoundRecordingActivity";
private View startButton;
private View stopButton;
private View playButton;
private View stopPlaybackButton;
private Uri uri;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startButton = findViewById(R.id.start);
stopButton = findViewById(R.id.stop);
playButton = findViewById(R.id.play);
stopPlaybackButton = findViewById(R.id.stop_all);
}
public void startRecording(View view) throws IOException {
startButton.setEnabled(false);
stopButton.setEnabled(true);
playButton.setEnabled(false);
mediaPlayer = MediaPlayer.create(this, R.raw.old_mac);
File sampleDir = Environment.getExternalStorageDirectory();
try {
audiofile = File.createTempFile("sound", ".mp4", 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();
mediaPlayer.start();
}
public void stopRecording(View view) {
startButton.setEnabled(true);
stopButton.setEnabled(false);
playButton.setEnabled(true);
mediaPlayer.stop();
recorder.stop();
recorder.release();
addRecordingToMediaLibrary();
}
protected void addRecordingToMediaLibrary() {
ContentValues values = new ContentValues(4);
long current = System.currentTimeMillis();
values.put(MediaStore.Audio.Media.TITLE, "audio" + audiofile.getName());
values.put(MediaStore.Audio.Media.DATE_ADDED, (int) (current / 1000));
values.put(MediaStore.Audio.Media.MIME_TYPE, "audio/3gpp");
values.put(MediaStore.Audio.Media.DATA, audiofile.getAbsolutePath());
ContentResolver contentResolver = getContentResolver();
Uri base = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
Uri newUri = contentResolver.insert(base, values);
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, newUri));
Toast.makeText(this, "Added File " + newUri, Toast.LENGTH_LONG).show();
setNewUri(newUri);
}
public void setNewUri(Uri uri){
this.uri = uri;
}
public Uri getNewUri(){
return this.uri;
}
public void playRecording(View view){
stopPlaybackButton.setEnabled(true);
playButton.setEnabled(false);
mediaPlayer = MediaPlayer.create(this, R.raw.old_mac);
try{
mediaRecorded.setDataSource(this, getNewUri());
mediaRecorded.prepare();
mediaRecorded.start();
mediaRecorded.setVolume(0, 1);
}catch(Exception e){
}
mediaPlayer.start();
mediaPlayer.setVolume(1, 0);
}
public void stopAll(View view){
stopPlaybackButton.setEnabled(false);
playButton.setEnabled(true);
mediaPlayer.stop();
mediaRecorded.stop();
}
}