1
+ package com.skyd.anivu.model.bean.playlist
2
+
3
+ import android.media.MediaMetadataRetriever
4
+ import androidx.room.ColumnInfo
5
+ import androidx.room.Entity
6
+ import androidx.room.ForeignKey
7
+ import androidx.room.Ignore
8
+ import com.skyd.anivu.base.BaseBean
9
+ import com.skyd.anivu.ext.isLocalFile
10
+ import kotlinx.serialization.Serializable
11
+
12
+ const val PLAYLIST_MEDIA_TABLE_NAME = " PlaylistMedia"
13
+
14
+ @Serializable
15
+ @Entity(
16
+ tableName = PLAYLIST_MEDIA_TABLE_NAME ,
17
+ primaryKeys = [PlaylistMediaBean .PLAYLIST_ID_COLUMN , PlaylistMediaBean .URL_COLUMN ],
18
+ foreignKeys = [
19
+ ForeignKey (
20
+ entity = PlaylistBean ::class ,
21
+ parentColumns = [PlaylistBean .PLAYLIST_ID_COLUMN ],
22
+ childColumns = [PlaylistMediaBean .PLAYLIST_ID_COLUMN ],
23
+ onDelete = ForeignKey .CASCADE
24
+ )
25
+ ],
26
+ )
27
+ data class PlaylistMediaBean (
28
+ @ColumnInfo(name = PLAYLIST_ID_COLUMN )
29
+ val playlistId : String ,
30
+ @ColumnInfo(name = URL_COLUMN )
31
+ val url : String ,
32
+ @ColumnInfo(name = ARTICLE_ID_COLUMN )
33
+ val articleId : String? ,
34
+ @ColumnInfo(name = ORDER_POSITION_COLUMN )
35
+ val orderPosition : Double ,
36
+ @ColumnInfo(name = CREATE_TIME_COLUMN )
37
+ val createTime : Long ,
38
+ ) : BaseBean {
39
+ fun isSamePlaylistMedia (other : PlaylistMediaBean ? ): Boolean {
40
+ other ? : return false
41
+ return playlistId == other.playlistId && url == other.url
42
+ }
43
+
44
+ @Ignore
45
+ val isLocalFile = url.isLocalFile()
46
+
47
+ @Ignore
48
+ var title: String? = null
49
+
50
+ @Ignore
51
+ var duration: Long? = null
52
+
53
+ @Ignore
54
+ var artist: String? = null
55
+
56
+ @Ignore
57
+ var thumbnail: String? = null
58
+
59
+ fun updateLocalMediaMetadata () {
60
+ val retriever = MediaMetadataRetriever ()
61
+ try {
62
+ with (retriever) {
63
+ setDataSource(url)
64
+ duration =
65
+ extractMetadata(MediaMetadataRetriever .METADATA_KEY_DURATION )?.toLongOrNull()
66
+ title = extractMetadata(MediaMetadataRetriever .METADATA_KEY_TITLE )
67
+ artist = extractMetadata(MediaMetadataRetriever .METADATA_KEY_ARTIST )
68
+ }
69
+ } catch (e: Exception ) {
70
+ e.printStackTrace()
71
+ }
72
+ retriever.release()
73
+ }
74
+
75
+ companion object {
76
+ const val PLAYLIST_ID_COLUMN = " playlistId"
77
+ const val URL_COLUMN = " url"
78
+ const val ARTICLE_ID_COLUMN = " articleId"
79
+ const val ORDER_POSITION_COLUMN = " orderPosition"
80
+ const val CREATE_TIME_COLUMN = " createTime"
81
+ }
82
+ }
0 commit comments