Audio playing while app in background iOS

前端 未结 7 897
我寻月下人不归
我寻月下人不归 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 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;
    }
    

提交回复
热议问题