From 5aa3f7dd0abcfb7767aa3ab7bb8ca1db2b09f252 Mon Sep 17 00:00:00 2001 From: Christopher Snowhill Date: Fri, 14 Jan 2022 00:29:02 -0800 Subject: [PATCH] Cog Audio: Properly support seeking within the file that appears to be playing, even if it has already finished decoding --- Audio/AudioPlayer.h | 2 ++ Audio/AudioPlayer.m | 34 ++++++++++++++++++++++++++++------ 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/Audio/AudioPlayer.h b/Audio/AudioPlayer.h index e16095885..bae92cf57 100644 --- a/Audio/AudioPlayer.h +++ b/Audio/AudioPlayer.h @@ -39,6 +39,8 @@ atomic_bool resettingNow; atomic_int refCount; + + int currentPlaybackStatus; } - (id)init; diff --git a/Audio/AudioPlayer.m b/Audio/AudioPlayer.m index 2cd7482b2..964c993fc 100644 --- a/Audio/AudioPlayer.m +++ b/Audio/AudioPlayer.m @@ -158,11 +158,31 @@ - (void)seekToTime:(double)time { - //Need to reset everything's buffers, and then seek? - /*HACK TO TEST HOW WELL THIS WOULD WORK*/ - [output seek:time]; - [bufferChain seek:time]; - /*END HACK*/ + if (endOfInputReached) + { + // This is a dirty hack in case the playback has finished with the track + // that the user thinks they're seeking into + CogStatus status = (CogStatus) currentPlaybackStatus; + NSURL *url; + id userInfo; + NSDictionary *rgi; + + @synchronized (chainQueue) { + url = [bufferChain streamURL]; + userInfo = [bufferChain userInfo]; + rgi = [bufferChain rgInfo]; + } + + [self stop]; + + [self play:url withUserInfo:userInfo withRGInfo:rgi startPaused:(status == CogStatusPaused) andSeekTo:time]; + } + else + { + // Still decoding the current file, safe to seek within it + [output seek:time]; + [bufferChain seek:time]; + } } - (void)setVolume:(double)v @@ -446,7 +466,9 @@ - (void)setPlaybackStatus:(int)status waitUntilDone:(BOOL)wait -{ +{ + currentPlaybackStatus = status; + [self sendDelegateMethod:@selector(audioPlayer:didChangeStatus:userInfo:) withObject:[NSNumber numberWithInt:status] withObject:[bufferChain userInfo] waitUntilDone:wait]; }