如何更新默认歌曲元数据?

时间:2019-02-19 05:29:04

标签: android metadata media-player audio-player android-music-player

我想更新Track,专辑,流派,艺术家和歌曲封面图像(如Musicmatch)的歌曲元数据字段。

我试图寻找用于更新meta的代码找不到任何解决方案。

2 个答案:

答案 0 :(得分:4)

您的问题不是问题,也没有详细说明。但我可以从Google示例中给您提供一个出色的Media Player,该示例名为UAMP(通用Android Media Player),它可以处理有关Android Media Player的所有内容。 Link

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

}

通过此组件,您可以在通知,锁定屏幕和主屏幕中更新歌曲数据。

答案 1 :(得分:0)

要更新歌曲的元数据,我们可以使用ID3标签进行。我们可以使用Mp3Tag编辑器-https://github.com/aminb/id3r更新它们 ,MyID3()编辑器-https://github.com/ericfarng/jid3lib  和Jaudiotagger-https://github.com/Adonai/jaudiotagger

Mp3Tag编辑器-仅支持Mp3歌曲类型 MyID3编辑器-可以轻松编辑歌曲,但并非所有提供的字段都已更新 Jaudiotagger-支持     MP3,     Flac,     OggVorbis,     MP4,     阿菲     哇,     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
        }      

编写元数据

// After update refresh the file else the changes will not be reflected
AudioFileIO.write(audioFile)
MediaScannerConnection.scanFile(context, arrayOf(file?.absolutePath ?: ""), null, null)