Only count a song as played after 10s (closes #619)

This commit is contained in:
Phan An 2017-08-06 10:55:02 +01:00
parent f3143d7193
commit 24eeebde06
2 changed files with 42 additions and 24 deletions

View file

@ -30,15 +30,17 @@ export const playback = {
this.audio = document.querySelector('audio')
this.volumeInput = document.getElementById('volumeRange')
const player = document.querySelector('.plyr')
/**
* Listen to 'error' event on the audio player and play the next song if any.
*/
document.querySelector('.plyr').addEventListener('error', () => this.playNext(), true)
player.addEventListener('error', () => this.playNext(), true)
/**
* Listen to 'ended' event on the audio player and play the next song in the queue.
*/
document.querySelector('.plyr').addEventListener('ended', e => {
player.addEventListener('ended', e => {
if (sharedStore.state.useLastfm && userStore.current.preferences.lastfm_session_key) {
songStore.scrobble(queueStore.current)
}
@ -49,7 +51,7 @@ export const playback = {
/**
* Attempt to preload the next song.
*/
document.querySelector('.plyr').addEventListener('canplaythrough', e => {
player.addEventListener('canplaythrough', e => {
const nextSong = queueStore.next
if (!nextSong || nextSong.preloaded || (isMobile.any && preferences.transcodeOnMobile)) {
// Don't preload if
@ -66,6 +68,18 @@ export const playback = {
nextSong.preloaded = true
})
player.addEventListener('timeupdate', e => {
const song = queueStore.current
if (this.player.media.currentTime > 10 && !song.registeredPlayCount) {
// After 10 seconds, register a play count and add it into "recently played" list
songStore.addRecentlyPlayed(song)
songStore.registerPlay(song)
song.registeredPlayCount = true
}
})
/**
* Listen to 'input' event on the volume range control.
* When user drags the volume control, this event will be triggered, and we
@ -113,9 +127,6 @@ export const playback = {
// Set the song as the current song
queueStore.current = song
// Add it into the "recently played" list
songStore.addRecentlyPlayed(song)
// Manually set the `src` attribute of the audio to prevent plyr from resetting
// the audio media object and cause our equalizer to malfunction.
this.player.media.src = songStore.getSourceUrl(song)
@ -128,22 +139,11 @@ export const playback = {
},
/**
* Restart playing a song.
* Show the "now playing" notification for a song.
*
* @param {Object} song
*/
restart () {
const song = queueStore.current
// Record the UNIX timestamp the song start playing, for scrobbling purpose
song.playStartTime = Math.floor(Date.now() / 1000)
event.emit('song:played', song)
this.player.restart()
this.player.play()
// Register the play to the server
songStore.registerPlay(song)
showNotification (song) {
// Show the notification if we're allowed to
if (!window.Notification || !preferences.notify) {
return
@ -177,6 +177,25 @@ export const playback = {
}
},
/**
* Restart playing a song.
*/
restart () {
const song = queueStore.current
this.showNotification(song)
// Record the UNIX timestamp the song start playing, for scrobbling purpose
song.playStartTime = Math.floor(Date.now() / 1000)
song.registeredPlayCount = false
event.emit('song:played', song)
this.player.restart()
this.player.play()
},
/**
* The next song in the queue.
* If we're in REPEAT_ALL mode and there's no next song, just get the first song.

View file

@ -191,11 +191,10 @@ export const songStore = {
/**
* Add a song into the "recently played" list.
*
* @param {Object}
* @param {Object} song
*/
addRecentlyPlayed (song) {
// First we make sure that there's no duplicate.
this.state.recentlyPlayed = without(this.state.recentlyPlayed, song)
remove(this.state.recentlyPlayed, s => s.id === song.id)
// Then we prepend the song into the list.
this.state.recentlyPlayed.unshift(song)