From 91997852020f6c7f5183f1024fc97c1aa6bc4a2c Mon Sep 17 00:00:00 2001 From: Thong Nguyen Date: Sat, 15 Feb 2014 17:04:52 +0000 Subject: [PATCH] Better backgroundTask start/stopping using blocks to safely avoid referencing STKAudioPlayer --- StreamingKit/StreamingKit/STKAudioPlayer.m | 34 +++++++++++----------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/StreamingKit/StreamingKit/STKAudioPlayer.m b/StreamingKit/StreamingKit/STKAudioPlayer.m index 6b3621c..d09a6df 100644 --- a/StreamingKit/StreamingKit/STKAudioPlayer.m +++ b/StreamingKit/StreamingKit/STKAudioPlayer.m @@ -229,10 +229,8 @@ static AudioStreamBasicDescription canonicalAudioStreamBasicDescription; AudioFileStreamID audioFileStream; NSConditionLock* threadStartedLock; NSConditionLock* threadFinishedCondLock; - -#if TARGET_OS_IPHONE - UIBackgroundTaskIdentifier backgroundTaskId; -#endif + + void(^stopBackBackgroundTaskBlock)(); int32_t seekVersion; OSSpinLock seekLock; @@ -565,30 +563,32 @@ static void AudioFileStreamPacketsProc(void* clientData, UInt32 numberBytes, UIn -(void) startSystemBackgroundTask { #if TARGET_OS_IPHONE - if (backgroundTaskId != UIBackgroundTaskInvalid) - { - pthread_mutex_unlock(&playerMutex); - - return; - } + __block UIBackgroundTaskIdentifier backgroundTaskId = UIBackgroundTaskInvalid; - __block UIBackgroundTaskIdentifier identifier; - - identifier = backgroundTaskId = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^ + backgroundTaskId = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^ { [[UIApplication sharedApplication] endBackgroundTask:backgroundTaskId]; + backgroundTaskId = UIBackgroundTaskInvalid; }]; + + stopBackBackgroundTaskBlock = [^ + { + if (backgroundTaskId != UIBackgroundTaskInvalid) + { + [[UIApplication sharedApplication] endBackgroundTask:backgroundTaskId]; + backgroundTaskId = UIBackgroundTaskInvalid; + } + } copy]; #endif } -(void) stopSystemBackgroundTask { #if TARGET_OS_IPHONE - if (backgroundTaskId != UIBackgroundTaskInvalid) + if (stopBackBackgroundTaskBlock != NULL) { - [[UIApplication sharedApplication] endBackgroundTask:backgroundTaskId]; - - backgroundTaskId = UIBackgroundTaskInvalid; + stopBackBackgroundTaskBlock(); + stopBackBackgroundTaskBlock = NULL; } #endif }