Audio playing while app in background iOS

前端 未结 7 899
我寻月下人不归
我寻月下人不归 2021-02-05 17:19

I have an app mostly based around Core Bluetooth. When something specific happens, the app is woken up using Core Bluetooth background modes and it fires off an alarm, however I

相关标签:
7条回答
  • 2021-02-05 17:58

    I have an App than also needs background audio but my App works with the App background mode "Voice over IP" as it needs to record sometimes. I play background audio telling the App singleton I need to play audio in background:

    UIBackgroundTaskIdentifier newTaskId = UIBackgroundTaskInvalid;
    if([thePlayer play]){
        newTaskId = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:NULL];   
    }
    

    EDIT: You must call [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:NULL]; before your app goes to background. In my app, it is at the same time you start playing, in yours, if the player might be started in background, you should do:

    - (void)applicationDidEnterBackground:(UIApplication *)application{
      // You should retain newTaskId to check for background tasks and finish them 
      newTaskId = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:NULL];   
    }
    
    0 讨论(0)
  • 2021-02-05 17:59

    I take it you have the audio background mode specified for the app. Even so, I'm not sure you can set an audio session to be active while in the background. You need to have activated it before going into the background. You may also need to play some silent audio to keep this active, but this is seems like bad practice (it may drain the battery). Looking at the docs for notifications there seems to be a way to have a local notification play an audio sample that's included in your bundle, which seems to be what you want to do, so maybe that's the way to go.

    0 讨论(0)
  • 2021-02-05 18:05

    Try this :

    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:nil];
    

    link

    Try this http://www.sagorin.org/ios-playing-audio-in-background-audio/

    0 讨论(0)
  • 2021-02-05 18:07

    I was also facing the same problem, but i was only facing it only during initial time when i was trying to play a sound while app was in background, once the app comes in foreground and i play the sound once than it works in background also.

    So as soon as app is launched/ login is successful in my case, i was running this code:

    [self startRingcall];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3.0f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self stopRingcall];
        });
    - (void)startRingcall
    {
        if( self.audioPlayer )
            [self.audioPlayer stop];
        NSURL* musicFile = [NSURL fileURLWithPath:[[[UILayer sharedInstance] getResourceBundle] pathForResource:@"meetcalling" ofType:@"caf"]];
        NSError *error;
        self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:musicFile error:&error];
        [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
        [[AVAudioSession sharedInstance] setActive: YES error: nil];
        if (error != nil) {
            NSLog(@"meetringer sound error -> %@",error.localizedDescription);
        }
        self.audioPlayer.volume = 0;
        [self.audioPlayer play];
        self.audioPlayer.numberOfLoops = 1;
    }
    
    - (void)stopRingcall
    {
        if( self.audioPlayer )
            [self.audioPlayer stop];
        self.audioPlayer = nil;
    }
    
    0 讨论(0)
  • 2021-02-05 18:13

    Apple has a nice Technical Q&A article about this in its documentation (see also Playing and Recording Background Audio).

    I think one big thing missing is that you haven't activated the Audio Background Mode in the Xcode settings: enter image description here

    Maybe also adding [self.player prepareToPlay] in your alert method is helpful.

    0 讨论(0)
  • 2021-02-05 18:13

    Apple docs

    Either enable audio support from the Background modes section of the Capabilities tab

    Or enable this support by including the UIBackgroundModes key with the audio value in your app’s Info.plist file

    0 讨论(0)
提交回复
热议问题