在我的应用程序中,我必须用音乐录制声音。当我点击录音按钮时,音乐开始,录音也将开始。用户将唱这首歌,这首歌将用音乐录制。
在我的代码歌曲中没有录制音乐。它只记录用户的声音。请检查我的代码并建议我。
recorder = new MediaRecorder();
record_btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
try {
mPlayer = MediaPlayer.create(RecordVoiceActivity.this, R.raw.intro);
mPlayer.start();
long dtMili = System.currentTimeMillis();
primarySongName=""+dtMili;
// Date dt = new Date(dtMili);
Toast.makeText(RecordVoiceActivity.this,
"Recording starting", Toast.LENGTH_LONG).show();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
currentDateTimeString = DateFormat.getDateInstance()
.format(new Date());
Log.d("##################################", "" + dtMili);
recorder.setOutputFile("/sdcard/Recordedsong_" + dtMili + ".3gp");
recorder.prepare();
recorder.start();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
stop_btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
try {
// TODO Auto-generated method stub
Toast.makeText(RecordVoiceActivity.this,
"Recording stopped", Toast.LENGTH_LONG).show();
mPlayer.stop();
recorder.stop();
recorder.release();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
replay_btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
try {
// TODO Auto-generated method stub
Log.d("############################", currentDateTimeString);
Uri path = Uri.parse(Environment.getExternalStorageDirectory()
+ "/" + "Recordedsong_" + primarySongName
+ ".3gp");
MediaPlayer player = MediaPlayer.create(
RecordVoiceActivity.this, path);
player.setLooping(false);
player.start();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
在此代码中,M录制歌曲并保存在我的Sd卡中。然后从Sdcard重播。 但是歌曲正在播放没有我正在播放的音乐。