~zanneth/StreamPlayer

7c20b18de41c86547744fb4423d19c971b1b10d2 — Charles Magahern 7 years ago a938d25
Correctly handle pause/resume
1 files changed, 31 insertions(+), 9 deletions(-)

M StreamPlayer/Source/ZANStreamPlayer.m
M StreamPlayer/Source/ZANStreamPlayer.m => StreamPlayer/Source/ZANStreamPlayer.m +31 -9
@@ 93,14 93,36 @@ static void _ZANAudioQueueOutputCallback(void *clientData,

- (void)play
{
    if (!_playing) {
    _playing = YES;
    
    if (_audioQueue) {
        // reset so that we don't hear previously queued buffers
        OSStatus status = AudioQueueReset(_audioQueue);
        if (status != noErr) {
            [self _logError:@"Failed to reset audio queue (OSStatus = %d)", status];
            [self _handleError:[self _errorFromOSStatus:status]];
        }
    }
    
    if (!_dataTask) {
        [self _openReadStream];
        // the first couple bytes encountered will start the audio queue again
    }
}

- (void)pause
{
    if (_playing) {
    _playing = NO;
    
    if (_audioQueue) {
        OSStatus status = AudioQueuePause(_audioQueue);
        if (status != noErr) {
            [self _logError:@"Failed to pause audio queue (OSStatus = %d)", status];
            [self _handleError:[self _errorFromOSStatus:status]];
        }
    }
    
    if (_dataTask) {
        [self _closeReadStream];
    }
}


@@ 109,7 131,7 @@ static void _ZANAudioQueueOutputCallback(void *clientData,

- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error
{
    if (error) {
    if (error && error.code != NSURLErrorCancelled) {
        [self _logError:@"URL session data task failed to complete. %@", error];
        [self _handleError:error];
    }


@@ 158,7 180,6 @@ static void _ZANAudioQueueOutputCallback(void *clientData,
    _dataTask = [_urlSession dataTaskWithRequest:request];
    [_dataTask resume];
    
    _playing = YES;
    return YES;
}



@@ 168,7 189,6 @@ static void _ZANAudioQueueOutputCallback(void *clientData,
    
    [_dataTask cancel];
    _dataTask = nil;
    _playing = NO;
    
    return YES;
}


@@ 305,10 325,12 @@ static void _ZANAudioQueueOutputCallback(void *clientData,
        }
        
        // start the audio queue processing hardware if necessary
        status = AudioQueueStart(_audioQueue, NULL);
        if (status != noErr) {
            [self _logError:@"Failed to start audio queue hardware (OSStatus = %d)", status];
            break;
        if (_playing) {
            status = AudioQueueStart(_audioQueue, NULL);
            if (status != noErr) {
                [self _logError:@"Failed to start audio queue hardware (OSStatus = %d)", status];
                break;
            }
        }
        
        // go to next buffer