如果打开静音模式,如何停止播放声音

Rom*_*use 0 iphone objective-c avaudioplayer ios

我的代码:

NSString *soundName = [[NSBundle mainBundle] pathForResource:@"music" ofType:@"mp3"];
NSURL *soundURL = [NSURL fileURLWithPath:soundName];
NSError *error = [[NSError alloc] init];
self.backgroundPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundURL error:&error];

if (self.backgroundPlayer == nil) {
    NSLog(@"error = %@",[error description]);
} else {
    [self.backgroundPlayer setDelegate:self];
    [self.backgroundPlayer setNumberOfLoops:HUGE_VAL];
    [self.backgroundPlayer setVolume:0.5f];
    [self.backgroundPlayer prepareToPlay];
    if ([self.backgroundPlayer prepareToPlay]) {
        if ([self.backgroundPlayer play]) {
            NSLog(@"playing");
        }
    } else {
        NSLog(@"error!");
    }
}
Run Code Online (Sandbox Code Playgroud)

当我将iPhone切换到静音模式时,声音仍在播放.我怎么解决这个问题?

jjv*_*360 6

我没试过这个,但你可以在播放文件之前设置音频类别:

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategorySoloAmbient error:nil];
Run Code Online (Sandbox Code Playgroud)