Record audio in Android
I have recorded audio in 3gp format and keep it in sd card.. this is my code and it run sucessfull.. I need to add an image to that audio file so that the added image will be the album cover & when i try to play the recorded audio taken from the sd card location in default media player of android..like this..
the image shown in the picture is the album cover i have to give it to my recorded audio file
/**
* Starts a new recording.
*/
public void start() throws IOException {
String state = android.os.Environment.getExternalStorageState();
if (!state.equals(android.os.Environment.MEDIA_MOUNTED)) {
throw new IOException("SD Card is not mounted. It is " + state
+ ".");
}
// make sure the directory we plan to store the recording in exists
File directory = new File(path).getParentFile();
if (!directory.exists() && !directory.mkdirs()) {
throw new开发者_开发技巧 IOException("Path to file could not be created.");
}
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
System.out.println("audio_file_path : = " + path);
recorder.setOutputFile(path);
recorder.prepare();
recorder.start(); // Recording is now started
}
If you just need the file to show art in the Android Media player, consider adding album art using the Google Content Provider. After creating your 3GP file, scan it using the MediaScannerConnection API. After it's been added to the database, you can access it using a ContentResolver
in your activity or service. The MediaScannerConnection will give you a URI for your newly created file. You can try placing the new art you'd like in a location on the sdcard. Then, call: getContentResolver().udpate(uri, newArtContentValue, null, null)
. The newArtContentValue
is an instance of a ContentValue
object, with a key of MediaStore.Audio.AudioColumns.ALBUM_ART
and a value of file:///sdcard/yourartfile.png
.
Caveat: I haven't actually tried this, so it may need small tweaks. You may need to change the extension of your file so that Google treats it as an audio file rather than a video file, so that the ALBUM_ART field exists. There may be other cases I haven't considered, as well.
If you absolutely must embed the art in the file itself:
3gp files do not (to my knowledge) support covert art metadata embedded in the file. So, you'l have to record to another container format that does. Even then, I think the only available OutputFormat that supports covert art metadata is MP4, and I'm not aware of any Android functionality that allows you to modify media files directly to add such tags.
精彩评论