我对编程很陌生,我想学习如何制作一个允许用户录制音频并因此将其保存到应用程序的数据文件夹中的应用程序。我设法完成了录音部分,但只设法将其保存到了 SD 卡中。任何愿意帮助我弄清楚如何将我的音频保存到内部存储器中的人?
private void playRecording() throws Exception {
ditchMediaPlayer();
mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(OUTPUT_FILE);
mediaPlayer.prepare();
mediaPlayer.start();
}
private void stopRecording() {
if(recorder != null)
recorder.stop();
}
private void beginRecording() throws Exception {
ditchMediaRecorder();
File outFile = new File(OUTPUT_FILE);
if(outFile.exists())
outFile.delete();
recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(OUTPUT_FILE);
recorder.prepare();
recorder.start();
}
private void ditchMediaRecorder() {
if (recorder != null)
recorder.release();
}
public void recordOnClick(View v) {
//when record button is pressed
try{
beginRecording();
}catch (Exception e){
e.printStackTrace();
}
btnRecord.setVisibility(View.INVISIBLE);
btnStop.setVisibility(View.VISIBLE);
}
public void stopOnClick(View v) {
//when stop button is pressed
try{
stopRecording();
}
catch (Exception e){
e.printStackTrace();
}
btnStop.setVisibility(View.INVISIBLE);
btnRecord.setVisibility(View.INVISIBLE);
btnDelete.setVisibility(View.VISIBLE);
btnPlay.setVisibility(View.VISIBLE);
btnShare.setVisibility(View.VISIBLE);
}
public void playOnClick(View v) {
//when play button is pressed
try{
playRecording();
}
catch (Exception e){
e.printStackTrace();
}
btnStop.setVisibility(View.INVISIBLE);
btnRecord.setVisibility(View.INVISIBLE);
}
这是我的 onClick,我将在其中指定每个按钮的输出(我正在做一个图表,所以我有大约 44 个按钮!)
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn1:
OUTPUT_FILE=Environment.getExternalStorageDirectory()+"/y1.3gpp";
File file1 = new File(Environment.getExternalStorageDirectory()+"/y1.3gpp");
if (file1.exists()) {
btnPlay.setVisibility(View.VISIBLE);
btnDelete.setVisibility(View.VISIBLE);
btnShare.setVisibility(View.VISIBLE);
}
else {
btnRecord.setVisibility(View.VISIBLE);
}
break;
case R.id.btn2:
OUTPUT_FILE=Environment.getExternalStorageDirectory()+"/y2.3gpp";
File file2 = new File(Environment.getExternalStorageDirectory()+ "/y2.3gpp" );
if (file2.exists()) {
btnPlay.setVisibility(View.VISIBLE);
btnDelete.setVisibility(View.VISIBLE);
btnShare.setVisibility(View.VISIBLE);
}
else {
btnRecord.setVisibility(View.VISIBLE);
}
break;