根据ID判断当前正在播放的
This commit is contained in:
parent
64bedfbfff
commit
ecb42b2c72
@ -26,7 +26,7 @@ export interface MusicLyricModel {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export interface MusicPlayerItem {
|
export interface MusicPlayerItem {
|
||||||
id?: number
|
id: number
|
||||||
title: string
|
title: string
|
||||||
artist: string | undefined
|
artist: string | undefined
|
||||||
album?: string | undefined
|
album?: string | undefined
|
||||||
|
|||||||
@ -142,7 +142,7 @@
|
|||||||
</template>
|
</template>
|
||||||
</el-dialog>
|
</el-dialog>
|
||||||
<el-drawer v-model="musicPlaying" :close-on-click-modal="false" size="40%" title="播放音乐">
|
<el-drawer v-model="musicPlaying" :close-on-click-modal="false" size="40%" title="播放音乐">
|
||||||
<a-player v-if="musicPlaying" ref="player" autoplay showLrc :list="musicList" :music="musicList[0]" @play="musicPlay"/>
|
<a-player v-if="musicPlaying" ref="player" autoplay showLrc :list="musicList" :music="currentMusic" @play="musicPlay"/>
|
||||||
</el-drawer>
|
</el-drawer>
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
@ -187,6 +187,7 @@ export default class Music extends BaseList<MusicPage> {
|
|||||||
// 是否正在播放音乐
|
// 是否正在播放音乐
|
||||||
musicPlaying: boolean = false
|
musicPlaying: boolean = false
|
||||||
musicList: MusicPlayerItem[] = []
|
musicList: MusicPlayerItem[] = []
|
||||||
|
currentMusic?: MusicPlayerItem
|
||||||
created() {
|
created() {
|
||||||
this.$http.get<never, any>('/api/v1/music/listLibs').then(data => {
|
this.$http.get<never, any>('/api/v1/music/listLibs').then(data => {
|
||||||
this.musicLibs = data
|
this.musicLibs = data
|
||||||
@ -215,8 +216,9 @@ export default class Music extends BaseList<MusicPage> {
|
|||||||
async playMusic() {
|
async playMusic() {
|
||||||
try {
|
try {
|
||||||
const data = await this.$http.get<any, any>('/api/v1/music/list/all', {params: selectedIds.length ? {ids: selectedIds} : this.search})
|
const data = await this.$http.get<any, any>('/api/v1/music/list/all', {params: selectedIds.length ? {ids: selectedIds} : this.search})
|
||||||
this.musicList = data.map((item: MusicModel) => {
|
this.musicList = data.map((item: MusicModel, index: number) => {
|
||||||
const musicItem: MusicPlayerItem = {
|
const musicItem: MusicPlayerItem = {
|
||||||
|
id: index,
|
||||||
title: item.title || item.name,
|
title: item.title || item.name,
|
||||||
artist: item.artist,
|
artist: item.artist,
|
||||||
src: `/api/v2/common/music/load/${item._id}`,
|
src: `/api/v2/common/music/load/${item._id}`,
|
||||||
@ -227,7 +229,7 @@ export default class Music extends BaseList<MusicPage> {
|
|||||||
}
|
}
|
||||||
return musicItem
|
return musicItem
|
||||||
})
|
})
|
||||||
// 删除原本可能存在的播放器配置(包含播放地址 时间进度等信息)
|
this.currentMusic = this.musicList[0]
|
||||||
this.musicPlaying = true
|
this.musicPlaying = true
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error(err)
|
console.error(err)
|
||||||
@ -307,23 +309,23 @@ export default class Music extends BaseList<MusicPage> {
|
|||||||
* 创建媒体信息
|
* 创建媒体信息
|
||||||
*/
|
*/
|
||||||
musicPlay() {
|
musicPlay() {
|
||||||
if(!('mediaSession' in window.navigator)) return;
|
if(!('mediaSession' in window.navigator) || !this.currentMusic) return;
|
||||||
const player = <any>this.$refs.player
|
const player = <any>this.$refs.player
|
||||||
const currentMusic = player.currentMusic
|
|
||||||
const mediaSession: any = navigator.mediaSession
|
const mediaSession: any = navigator.mediaSession
|
||||||
|
const currentId = this.currentMusic.id
|
||||||
mediaSession.metadata = new MediaMetadata({
|
mediaSession.metadata = new MediaMetadata({
|
||||||
title: currentMusic.title,
|
title: this.currentMusic.title,
|
||||||
artist: currentMusic.artist,
|
artist: this.currentMusic.artist,
|
||||||
album: currentMusic.album,
|
album: this.currentMusic.album,
|
||||||
artwork: [{src: currentMusic.pic}]
|
artwork: [{src: this.currentMusic.pic}]
|
||||||
});
|
})
|
||||||
|
const currentIndex = this.musicList.findIndex(item => item.id === currentId)
|
||||||
mediaSession.setActionHandler('play', () => { // 播放
|
mediaSession.setActionHandler('play', () => { // 播放
|
||||||
player.play()
|
player.play()
|
||||||
})
|
})
|
||||||
mediaSession.setActionHandler('pause', () => { // 暂停
|
mediaSession.setActionHandler('pause', () => { // 暂停
|
||||||
player.pause()
|
player.pause()
|
||||||
})
|
})
|
||||||
const currentIndex = this.musicList.findIndex(item => item.src === currentMusic.src)
|
|
||||||
mediaSession.setActionHandler('previoustrack', () => { // 上一首
|
mediaSession.setActionHandler('previoustrack', () => { // 上一首
|
||||||
if (currentIndex === 0) { // 已经是第一首
|
if (currentIndex === 0) { // 已经是第一首
|
||||||
player.switch(this.musicList.length - 1)
|
player.switch(this.musicList.length - 1)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user