Skip to content

Commit

Permalink
fix: safari下不能播放
Browse files Browse the repository at this point in the history
  • Loading branch information
sl1673495 committed Jul 31, 2019
1 parent 73471f2 commit f51842e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 24 deletions.
2 changes: 1 addition & 1 deletion src/components/song-table.vue
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ export default {
['row-click']: this.onRowClick
},
props: {
['table-cell-class-name']: this.tableCellClassName,
['cell-class-name']: this.tableCellClassName,
data: this.songs
},
style: { width: '99.9%' }
Expand Down
56 changes: 33 additions & 23 deletions src/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,30 @@ import { HISTORY_KEY } from '@/utils/config'

export default {
// 整合歌曲信息 并且开始播放
async startSong({ commit, state }, song) {
const { data } = await getSongUrl(song.id)
const [resultSong] = data
const canPlay = !!resultSong.url
if (canPlay) {
commit('setCurrentSong', {
...song,
url: resultSong.url
})
async startSong({ commit, state, dispatch }, song) {

// 历史记录
const { playHistory } = state
const playHistoryCopy = playHistory.slice()
const findedIndex = playHistoryCopy.findIndex(({ id }) => song.id === id)
if (findedIndex !== -1) {
// 删除旧那一项, 插入到最前面
playHistoryCopy.splice(findedIndex, 1)
}
playHistoryCopy.unshift(song)
commit('setPlayHistory', playHistoryCopy)
commit('setPlayingState', true)
storage.set(HISTORY_KEY, playHistoryCopy)
} else {
alert('暂时无法播放')
commit('setCurrentSong', {
...song,
url: genSongPlayUrl(song.id)
})

// 历史记录
const { playHistory } = state
const playHistoryCopy = playHistory.slice()
const findedIndex = playHistoryCopy.findIndex(({ id }) => song.id === id)
if (findedIndex !== -1) {
// 删除旧那一项, 插入到最前面
playHistoryCopy.splice(findedIndex, 1)
}
playHistoryCopy.unshift(song)
commit('setPlayHistory', playHistoryCopy)
commit('setPlayingState', true)
storage.set(HISTORY_KEY, playHistoryCopy)
// 检查是否能播放
const canPlay = await checkCanPlay(song.id)
if (!canPlay) {
alert('播放失败')
dispatch('clearCurrentSong')
}
},
clearCurrentSong({ commit }) {
Expand All @@ -48,3 +48,13 @@ export default {
}
}
}

function genSongPlayUrl(id) {
return `https://music.163.com/song/media/outer/url?id=${id}.mp3`
}

async function checkCanPlay(id) {
const { data } = await getSongUrl(id)
const [resultSong] = data
return !!resultSong.url
}

0 comments on commit f51842e

Please sign in to comment.