From e2e6a7406845e11e779696111138a7b761409377 Mon Sep 17 00:00:00 2001 From: Ben Pye Date: Tue, 20 Nov 2018 03:13:57 +0000 Subject: [PATCH] Add AudioFile cancellation --- src/librespot/player/audiofile.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/librespot/player/audiofile.go b/src/librespot/player/audiofile.go index b35ce0c..fe65909 100644 --- a/src/librespot/player/audiofile.go +++ b/src/librespot/player/audiofile.go @@ -40,6 +40,7 @@ type AudioFile struct { cursor int chunks map[int]bool chunksLoading bool + cancelled bool } func newAudioFile(file *Spotify.AudioFile, player *Player) *AudioFile { @@ -155,6 +156,11 @@ func (a *AudioFile) Seek(offset int64, whence int) (int64, error) { return int64(a.cursor - a.headerOffset()), nil } +// Cancels the current audio file - no further data will be downloaded +func (a *AudioFile) Cancel() { + a.cancelled = true +} + func (a *AudioFile) headerOffset() int { // If the file format is an OGG, we skip the first kOggSkipBytes (167) bytes. We could implement despotify's // SpotifyOggHeader (https://sourceforge.net/p/despotify/code/HEAD/tree/java/trunk/src/main/java/se/despotify/client/player/SpotifyOggHeader.java) @@ -275,6 +281,10 @@ func (a *AudioFile) loadChunk(chunkIndex int) error { } func (a *AudioFile) loadNextChunk() { + if a.cancelled { + return + } + a.chunkLock.Lock() if a.chunksLoading {