【问题标题】:Why does AVAudioPlayer does not play?为什么 AVAudioPlayer 不播放?
【发布时间】:2016-04-13 15:46:36
【问题描述】:

我们开发了这个 iOS 场景:

1) iOS App 安排本地推送通知
2) Push 是启动 AVAudioPlayer 音频播放的触发器 - 即使 iPhone 被锁定(后台应用程序)

现在有两种情况:

a) 推送调度时差超过3小时: 解锁并启动应用后,音频确实开始播放

b) 推送调度时间差小于3小时: 即使没有解锁,音频也会开始播放

我们希望 case b 作为默认情况。 iOS 会在几个小时后停止/冻结我们的应用程序吗?有趣:在印度设备上似乎没有 case a - 即使时差超过 12 小时。

有什么想法吗? 非常感谢!

【问题讨论】:

    标签: ios iphone audio avfoundation avaudioplayer


    【解决方案1】:

    【讨论】:

      【解决方案2】:

      一旦在 appdelegate 中触发了 didReceiveLocalNotification 方法,然后在 appdelegate 或全局中触发一个方法,然后使用下面的代码在后台或设备锁定中使用 AVAudioPlayer 播放音频文件

      NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:@“filename”
                                                                ofType:@“type”];
      NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];
      
      player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL
                                                      error:nil];
      
      [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
      [[AVAudioSession sharedInstance] setActive: YES error: nil];
      [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
      
      
      player.numberOfLoops = -1; //Infinite
      
      [player prepareToPlay];
      
      [player play];
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-12-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多