|
| 1 | +package remix.myplayer.helper |
| 2 | + |
| 3 | +import android.os.ParcelFileDescriptor |
| 4 | +import com.kyant.taglib.AudioProperties |
| 5 | +import com.kyant.taglib.Metadata |
| 6 | +import com.kyant.taglib.Picture |
| 7 | +import com.kyant.taglib.PropertyMap |
| 8 | +import com.kyant.taglib.TagLib |
| 9 | +import java.io.File |
| 10 | +import java.io.IOException |
| 11 | + |
| 12 | +/** |
| 13 | + * File-based access to audio metadata through TagLib. |
| 14 | + * |
| 15 | + * TagLib takes ownership of the supplied raw file descriptor, so every call uses a detached |
| 16 | + * duplicate and leaves the [ParcelFileDescriptor] owned by Android untouched. |
| 17 | + */ |
| 18 | +object AudioTagFile { |
| 19 | + |
| 20 | + const val TITLE = "TITLE" |
| 21 | + const val ALBUM = "ALBUM" |
| 22 | + const val ARTIST = "ARTIST" |
| 23 | + const val ALBUM_ARTIST = "ALBUMARTIST" |
| 24 | + const val COMPOSER = "COMPOSER" |
| 25 | + const val GENRE = "GENRE" |
| 26 | + const val DATE = "DATE" |
| 27 | + const val TRACK_NUMBER = "TRACKNUMBER" |
| 28 | + const val DISC_NUMBER = "DISCNUMBER" |
| 29 | + const val LYRICS = "LYRICS" |
| 30 | + |
| 31 | + fun readAudioProperties(file: File): AudioProperties? = |
| 32 | + withFileDescriptor(file, ParcelFileDescriptor.MODE_READ_ONLY) { fd -> |
| 33 | + TagLib.getAudioProperties(fd) |
| 34 | + } |
| 35 | + |
| 36 | + fun readMetadata(file: File, readPictures: Boolean = true): Metadata? = |
| 37 | + withFileDescriptor(file, ParcelFileDescriptor.MODE_READ_ONLY) { fd -> |
| 38 | + TagLib.getMetadata(fd, readPictures) |
| 39 | + } |
| 40 | + |
| 41 | + fun readFrontCover(file: File): Picture? = |
| 42 | + withFileDescriptor(file, ParcelFileDescriptor.MODE_READ_ONLY) { fd -> |
| 43 | + TagLib.getFrontCover(fd) |
| 44 | + } |
| 45 | + |
| 46 | + fun savePropertyMap(file: File, propertyMap: PropertyMap): Boolean = |
| 47 | + withFileDescriptor(file, ParcelFileDescriptor.MODE_READ_WRITE) { fd -> |
| 48 | + TagLib.savePropertyMap(fd, propertyMap) |
| 49 | + } |
| 50 | + |
| 51 | + fun savePictures(file: File, pictures: Array<Picture>): Boolean = |
| 52 | + withFileDescriptor(file, ParcelFileDescriptor.MODE_READ_WRITE) { fd -> |
| 53 | + TagLib.savePictures(fd, pictures) |
| 54 | + } |
| 55 | + |
| 56 | + fun firstValue(propertyMap: Map<String, Array<String>>?, key: String): String = |
| 57 | + propertyMap?.get(key)?.firstOrNull().orEmpty() |
| 58 | + |
| 59 | + fun setValue(propertyMap: PropertyMap, key: String, value: String) { |
| 60 | + if (value.isBlank()) { |
| 61 | + propertyMap.remove(key) |
| 62 | + } else { |
| 63 | + propertyMap[key] = arrayOf(value) |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + fun requireSaved(saved: Boolean, operation: String) { |
| 68 | + if (!saved) { |
| 69 | + throw IOException("TagLib failed to $operation") |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + private inline fun <T> withFileDescriptor( |
| 74 | + file: File, |
| 75 | + mode: Int, |
| 76 | + block: (Int) -> T |
| 77 | + ): T = ParcelFileDescriptor.open(file, mode).use { descriptor -> |
| 78 | + block(descriptor.dup().detachFd()) |
| 79 | + } |
| 80 | +} |
0 commit comments