AVAudioPlayer doesn't start playing in background

前端 未结 4 1320
我寻月下人不归
我寻月下人不归 2021-01-16 18:39

I am using AVAudioPlayer and NSFileHandle to do audio streaming. The basic idea is that I save the streaming audio data to the device with file sys

4条回答
  •  时光取名叫无心
    2021-01-16 19:21

    Please use this code in - (void)applicationDidEnterBackground:(UIApplication *)application

    UIApplication *app = [UIApplication sharedApplication];
    UIBackgroundTaskIdentifier bgTask = 0;
    
    bgTask = [app beginBackgroundTaskWithExpirationHandler:^{ 
    [app endBackgroundTask:bgTask]; 
    }];
    AudioSessionSetActive(false);
    

    and use this code in - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

    AudioSessionInitialize (NULL,NULL,NULL,NULL);
    UInt32 sessionCategory = kAudioSessionCategory_MediaPlayback;
    AudioSessionSetProperty (kAudioSessionProperty_AudioCategory,sizeof (sessionCategory),&sessionCategory);
    AudioSessionSetActive(true);
    

    hope this helps.

提交回复
热议问题