iOS background audio not playing

前端 未结 5 2120
悲哀的现实
悲哀的现实 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:50

    By default, AVAudioPlayer uses the AVAudioSessionCategorySoloAmbient category, which is silenced by the ringer switch. The AVAudioSessionCategoryPlayback category is more appropriate for your situation, since it is not silenced by the switch, and will continue to play in the background:

    NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
                                        pathForResource:soundName
                                        ofType:@"caf"]];
    
    player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
    [player setCategory:AVAudioSessionCategoryPlayback error:nil];
    player.numberOfLoops = -1;
    [player setVolume:1.0];
    

提交回复
热议问题