iOS background audio not playing

前端 未结 5 2139
悲哀的现实
悲哀的现实 2020-11-27 05:51

I have an app that uses CoreBluetooth background modes. When a certain event happens I need to play an alarm sound. Everything works fine in the foreground and

5条回答
  •  感动是毒
    2020-11-27 06:33

    Apart from plist settings you have to modify app delegate.

    - (void)applicationDidEnterBackground:(UIApplication *)application
    {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:NULL];
        [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
    }
    

    Also in your controller write the following code.

    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
    [[AVAudioSession sharedInstance] setActive: YES error: nil];
    [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
    

提交回复
热议问题