Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add AudioFile cancellation #13

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/librespot/player/audiofile.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down