问题描述:

I created a file and use it to save my sound file, but this file cannot be added in Music

File f;

boolean recording=false;

public void startStopRecording(View view){

try {

if (!recording) {

do {

DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

Date date = new Date();

String filename = "rec_" + dateFormat.format(date)+".gpp";

outputFile = Environment.getExternalStorageDirectory().getAbsolutePath() + "/myRec/"+filename;

f = new File(outputFile);

f.getParentFile().mkdirs();

}while(f.exists());

myAudioRecorder = new MediaRecorder();

myAudioRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);

myAudioRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);

myAudioRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);

myAudioRecorder.setOutputFile(outputFile);

myAudioRecorder.prepare();

myAudioRecorder.start();

recording=!recording;

}else{

myAudioRecorder.stop();

myAudioRecorder.release();

myAudioRecorder = null;

recording=!recording;

Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);

mediaScanIntent.setData(Uri.fromFile(f));

sendBroadcast(mediaScanIntent);

MediaScannerConnection.scanFile(this, new String[]{f.getAbsolutePath()}, null, null);

}

} catch (IOException e) {

e.printStackTrace();

}

}

I also tried this, still not work

ContentValues values = new ContentValues(4);

long current = System.currentTimeMillis();

values.put(MediaStore.Audio.Media.TITLE, "audio file");

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, f.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));

How to add my sound file to Music?

网友答案:

Replace

 String filename = "rec_" + dateFormat.format(date)+".gpp";

with

String filename = "rec_" + dateFormat.format(date)+".3gp";

You can refer this tutorial

http://www.tutorialspoint.com/android/android_audio_capture.htm

If you still feel trouble download this sample

https://github.com/hiteshsahu/Android-Audio-Recorder-Visualization-Master

相关阅读:
Top