在 Android 中录制音频

发布于 2024-11-03 22:32:31 字数 1073 浏览 0 评论 0原文

我已经以 3gp 格式录制了音频并将其保存在 SD 卡中。这是我的代码,它运行成功。我需要向该音频文件添加图像,以便添加的图像将成为专辑封面和专辑封面。当我尝试在 Android 的默认媒体播放器中播放从 SD 卡位置获取的录制音频时..就像这样..

图片中显示的图像是专辑封面,我必须将其提供给我录制的音频文件

/**
 * 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




}

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..

enter image description here

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




}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

幸福%小乖 2024-11-10 22:32:31

如果您只需要文件在 Android 媒体播放器中显示艺术作品,请考虑使用 Google 内容提供商添加专辑艺术。创建 3GP 文件后,使用 MediaScannerConnection API 扫描它。将其添加到数据库后,您可以在 Activity 或服务中使用 ContentResolver 访问它。 MediaScannerConnection 将为您提供新创建的文件的 URI。您可以尝试将您想要的新艺术作品放置在 SD 卡上的某个位置。然后,调用:getContentResolver().udpate(uri, newArtContentValue, null, null)newArtContentValueContentValue 对象的实例,键为 MediaStore.Audio.AudioColumns.ALBUM_ART ,值为 file ///sdcard/yourartfile.png

警告:我实际上还没有尝试过这个,所以可能需要一些小的调整。您可能需要更改文件的扩展名,以便 Google 将其视为音频文件而不是视频文件,以便 ALBUM_ART 字段存在。可能还有其他我没有考虑到的情况。

如果您绝对必须将艺术嵌入到文件本身中:

3gp 文件(据我所知)不支持嵌入在文件中的隐蔽艺术元数据。因此,您必须录制到另一种容器格式。即便如此,我认为支持隐蔽艺术元数据的唯一可用的 OutputFormat 是 MP4,而且我不知道有任何 Android 功能允许您直接修改媒体文件以添加此类标签。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文