oOOMIAO 2013-02-28 05:28 采纳率: 10%
浏览 2183

在静音模式中停止播放声音

代码:

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!");
    }
}

在将iphone调到静音模式时声音还会播放,不知道怎么让它停止呢?

  • 写回答

1条回答 默认 最新

  • ChatGPTᴼᴾᴱᴺᴬᴵ 2022-12-19 14:51
    关注

    ChatGPT尝试为您解答,仅供参考
    如果你想在 iPhone 处于静音模式时停止播放声音,你可以在播放声音之前检查 iPhone 的音量设置。


    你可以使用 [AVAudioSession sharedInstance] 的 outputVolume 属性来获取当前的音量值。如果音量值为 0,则表示 iPhone 处于静音模式,你可以在此时停止播放声音。


    例如,你可以这样检查 iPhone 的音量设置并停止播放声音:

    AVAudioSession *audioSession = [AVAudioSession sharedInstance];
    if (audioSession.outputVolume == 0) {
        // iPhone 处于静音模式,停止播放声音
        [self.backgroundPlayer stop];
    } else {
        // iPhone 处于非静音模式,继续播放声音
        [self.backgroundPlayer play];
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler