-
Notifications
You must be signed in to change notification settings - Fork 177
/
Song.kt
270 lines (238 loc) · 5.65 KB
/
Song.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
package remix.myplayer.bean.mp3
import android.content.ContentUris
import android.media.MediaMetadataRetriever
import android.net.Uri
import android.provider.MediaStore
import okhttp3.Credentials
import remix.myplayer.App
import remix.myplayer.util.SPUtil
import timber.log.Timber
/**
* Created by Remix on 2015/11/30.
*/
/**
* 歌曲信息
*/
sealed class Song(
val id: Long,
var displayName: String,
var title: String,
var album: String,
var albumId: Long,
var artist: String,
var artistId: Long,
private var _duration: Long,
var data: String,
var size: Long,
var year: String,
private var _genre: String?,
var track: String?,
var dateModified: Long
) : APlayerModel {
fun isLocal(): Boolean {
return this is Local
}
fun isRemote(): Boolean {
return this is Remote
}
fun updateMetaData(
title: String,
album: String,
artist: String,
duration: Long,
year: String,
genre: String,
track: String,
dateModified: Long
) {
this.title = title
this.album = album
this.artist = artist
this._duration = duration
this.year = year
this._genre = genre
this.track = track
this.dateModified = dateModified
}
val duration: Long
get() {
if (_duration <= 0 && data.isNotEmpty() && this !is Remote) {
val metadataRetriever = MediaMetadataRetriever()
try {
metadataRetriever.setDataSource(data)
_duration =
metadataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION)!!
.toLong()
} catch (e: Exception) {
Timber.v("Fail to get duration: $e")
} finally {
metadataRetriever.release()
}
}
return _duration
}
val genre: String
get() {
if (_genre.isNullOrEmpty() && id > 0 && data.isNotEmpty()) {
val metadataRetriever = MediaMetadataRetriever()
try {
metadataRetriever.setDataSource(data)
_genre = metadataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_GENRE)!!
} catch (e: Exception) {
Timber.v("Fail to get genre: $e")
} finally {
metadataRetriever.release()
}
}
return _genre ?: ""
}
val contentUri: Uri
get() = if (isLocal()) {
ContentUris.withAppendedId(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, id)
} else {
Uri.parse(data)
}
val artUri: Uri
get() = ContentUris.withAppendedId(
Uri.parse("content://media/external/audio/albumart/"),
albumId
)
val showName: String
get() = if (!SHOW_DISPLAYNAME) title else displayName
override fun getKey(): String {
return albumId.toString()
}
fun copy(): Song {
return when (this) {
is Local -> {
Local(
id,
displayName,
title,
album,
albumId,
artist,
artistId,
_duration,
data,
size,
year,
_genre,
track,
dateModified
)
}
is Remote -> {
Remote(title, album, artist, duration, data, size, year, genre, track, dateModified, account, pwd)
}
}
}
class Local(
id: Long,
displayName: String,
title: String,
album: String,
albumId: Long,
artist: String,
artistId: Long,
_duration: Long,
data: String,
size: Long,
year: String,
_genre: String?,
track: String?,
dateModified: Long
) : Song(
id,
displayName,
title,
album,
albumId,
artist,
artistId,
_duration,
data,
size,
year,
_genre,
track,
dateModified
) {
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false
other as Song
if (id != other.id) return false
return true
}
override fun hashCode(): Int {
return id.hashCode()
}
override fun toString(): String {
return "LocalSong(id='$id', data='$data')"
}
}
class Remote(
title: String,
album: String,
artist: String,
duration: Long,
data: String,
size: Long,
year: String,
genre: String,
track: String?,
dateModified: Long,
val account: String,
val pwd: String
) : Song(
data.hashCode().toLong(), title, title, album, 0L, artist, 0L, duration, data, size, year, genre, track, dateModified
) {
val headers by lazy {
mapOf(
"Authorization" to Credentials.basic(account, pwd)
)
}
var bitRate: String = ""
var sampleRate: String = ""
constructor(title: String, data: String, account: String, pwd: String): this(title, data, 0L, 0L, account, pwd)
constructor(title: String, data: String, size: Long, dateModified: Long, account: String, pwd: String) : this(
title,
"",
"",
0L,
data,
size,
"",
"",
"",
dateModified,
account,
pwd
)
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false
other as Song
if (data != other.data) return false
return true
}
override fun hashCode(): Int {
return data.hashCode()
}
override fun toString(): String {
return "RemoteSong(data='$data')"
}
}
companion object {
@JvmStatic
val EMPTY_SONG = Local(-1, "", "", "", -1, "", -1, -1, "", -1, "", "", "", -1)
//所有列表是否显示文件名
@JvmStatic
var SHOW_DISPLAYNAME = SPUtil
.getValue(
App.context, SPUtil.SETTING_KEY.NAME, SPUtil.SETTING_KEY.SHOW_DISPLAYNAME,
false
)
}
}