[英]How to update the default song metadata?
I wanted to update the song metadata fields of Track, album, genre, artist and song cover image like Musicmatch. 我想更新曲目,专辑,流派,艺术家和歌曲封面图像(如Musicmatch)的歌曲元数据字段。
I tried to look for the code to update the meta couldn't find any solutions. 我试图寻找更新元的代码找不到任何解决方案。
Your question isn't about a problem and is not detailed. 您的问题不是问题,也没有详细说明。 But I can give you a great Media Player from google samples named UAMP(Universal Android Media Player) with handle all about android media player.
但我可以从Google示例中给您提供一个出色的Media Player,该示例名为UAMP(通用Android Media Player),它可以处理有关Android Media Player的所有内容。 Link
链接
UAMP uses a MediaMetadataCompat to update the song metadata like below code segment. UAMP使用MediaMetadataCompat更新歌曲元数据,如下面的代码段所示。
fun MediaMetadataCompat.Builder.from(jsonMusic: JsonMusic): MediaMetadataCompat.Builder {
// The duration from the JSON is given in seconds, but the rest of the code works in
// milliseconds. Here's where we convert to the proper units.
val durationMs = TimeUnit.SECONDS.toMillis(jsonMusic.duration)
id = jsonMusic.id
title = jsonMusic.title
artist = jsonMusic.artist
album = jsonMusic.album
duration = durationMs
genre = jsonMusic.genre
mediaUri = jsonMusic.source
albumArtUri = jsonMusic.image
trackNumber = jsonMusic.trackNumber
trackCount = jsonMusic.totalTrackCount
flag = MediaItem.FLAG_PLAYABLE
// To make things easier for *displaying* these, set the display properties as well.
displayTitle = jsonMusic.title
displaySubtitle = jsonMusic.artist
displayDescription = jsonMusic.album
displayIconUri = jsonMusic.image
// Add downloadStatus to force the creation of an "extras" bundle in the resulting
// MediaMetadataCompat object. This is needed to send accurate metadata to the
// media session during updates.
downloadStatus = STATUS_NOT_DOWNLOADED
// Allow it to be used in the typical builder style.
return this
} }
By this component, you can update song data in the notification, lock screen, and home screen. 通过此组件,您可以在通知,锁定屏幕和主屏幕中更新歌曲数据。
To update the metadata of a song we can do by using ID3 tags. 要更新歌曲的元数据,我们可以使用ID3标签进行。 We can update these using Mp3Tag editor - https://github.com/aminb/id3r , MyID3() editor - https://github.com/ericfarng/jid3lib and Jaudiotagger - https://github.com/Adonai/jaudiotagger .
我们可以更新这些使用Mp3Tag编辑器- https://github.com/aminb/id3r ,MyID3()编辑器- https://github.com/ericfarng/jid3lib和Jaudiotagger - https://github.com/Adonai/jaudiotagger 。
Mp3Tag editor - Only Mp3 song type is supported MyID3 editor - Can edit song easily but not all field provided is updated Jaudiotagger - This supports Mp3, Flac, OggVorbis, Mp4, Aiff, Wav, Wma, Dsf audio formats It updated data without any issue Mp3Tag编辑器-仅支持Mp3歌曲类型MyID3编辑器-可以轻松编辑歌曲,但未更新提供的所有字段Jaudiotagger-支持Mp3,Flac,OggVorbis,Mp4,Aiff,Wav,Wma,Dsf音频格式它更新了数据而没有任何问题
try {
val audioFile = AudioFileIO.read(file)
val tag = audioFile?.tagOrCreateAndSetDefault
tag?.setField(FieldKey.ARTIST, binding?.tiArtist?.text?.toString())
tag?.setField(FieldKey.ALBUM, binding?.tiAlbum?.text?.toString())
tag?.setField(FieldKey.GENRE, binding?.tiGenre?.text?.toString())
tag?.setField(FieldKey.TITLE, binding?.tiTrack?.text?.toString())
// Handle the image setting
try {
val pfd = contentResolver.openFileDescriptor(imageUri, "r") ?: return
val fis = FileInputStream(pfd.fileDescriptor)
val imgBytes = JavaUtils.readFully(fis)
val cover = AndroidArtwork()
cover.binaryData = imgBytes
cover.mimeType = ImageFormats.getMimeTypeForBinarySignature(byteArray)
cover.description = ""
cover.pictureType = PictureTypes.DEFAULT_ID
tag?.deleteArtworkField()
tag?.setField(cover)
fis.close()
// to do check the file write option for both internal and external card
// Handle the Storage Access FrameWork API if song is from SD card
if (audioFile?.file?.let { SafUtils.isSafNeeded(it, this) } == true) {
// Handle writing into SD card
// Check if SAF permission is provided then only we can update metadata
// If SAF Permission is not provided. EACCESS : Permission Denied error is displayed
// After the permission success then only we can update meta.
writeIntoSDCard()
} else {
// Handle writing into internal card
writeInInternalStorage()
}
} catch (e: Exception) { }
} catch (e: Exception) {
// Show error on failure while writing
} catch (e: Error) {
// Show error on failure while writing
}
Writing the metadata 编写元数据
// After update refresh the file else the changes will not be reflected
AudioFileIO.write(audioFile)
MediaScannerConnection.scanFile(context, arrayOf(file?.absolutePath ?: ""), null, null)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.