要更新歌曲的元数据,我们可以使用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, 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 }
编写元数据
// After update refresh the file else the changes will not be reflected AudioFileIO.write(audioFile) MediaScannerConnection.scanFile(context, arrayOf(file?.absolutePath ?: ""), null, null)