简体   繁体   English

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

[英]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.

相关问题 Android - 如何更新 MediaSession 元数据,以便在蓝牙连接的设备上反映歌曲更改? - Android - How to update MediaSession Metadata so song changes are reflected on bluetooth connected device? 如何更新任何特定歌曲的专辑封面 - How to update album art of any specific song 如何在Android中启动默认音乐应用程序以播放特定歌曲? - How To Launch the default Music App in android to play a particular song? 如何在不使用Android SDK的情况下不使用playUri的情况下获取歌曲元数据? - How can I get song metadata without using playUri using the Android SDK? 如何从shoutcast流中检索元数据时如何获知新的歌曲信息 - how to get informed of new song info while retrieving metadata from shoutcast stream 如何更新显示歌曲当前时间位置的文本视图 - How to update textview which shows current time position from a song 投射连续音频流时如何更新歌曲信息 - How to Update the Song Information When Casting Continuous Audio Streams 完成歌曲后如何播放下一首歌曲? - How to play next song on completion of a song? 播放另一首歌曲时如何停止歌曲 - how to Stop the song when another song is play 如何播放下一首和上一首歌? - How to play next song and previous song?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM