当前位置: 首页 > 面试题库 >

iOs 8,在后台闹钟应用程序时开始播放声音

米楚青
2023-03-14
问题内容

我知道关于SOF的问题很多,但这是“最新的”问题。事实是,我正在尝试创建和警报应用程序,并且我知道有一个警报应用程序可以很好地运行(某种程度上),即使该应用程序未运行且在后台。

我的问题是:你是怎么开始播放声音 后, 你的应用程序已经在后台?

UILocalNotification很棒,但是只有application:(_:didReceiveLocalNotification:)在用户单击通知后您才能获得,因此使用AVAudioPlayer播放声音不起作用,无论用户单击还是不单击,我都想播放声音。当然Required background modes: App plays audio or streams audio/video using AirPlayinfo.plist已经设置好了。音乐开始播放后,即使我进入后台,它也会继续播放。

我不想使用UILocalNotificaation声音,因为它仅限于30秒,只能播放与应用程序捆绑在一起的声音。

有什么见解吗?想法??

样例代码:

    var notif = UILocalNotification()
    notif.fireDate = self.datePicker.date
    notif.alertBody = "test"
    UIApplication.sharedApplication().scheduleLocalNotification(notif)

当用户选择并报警并单击保存时,将调用上述代码。

这是AppDelegate中发生的事情:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {
    NSLog("launched")
    application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: UIUserNotificationType.Sound | UIUserNotificationType.Alert |
        UIUserNotificationType.Badge, categories: nil
        ))
    AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, error: nil)
    AVAudioSession.sharedInstance().setActive(true, error: nil)

    self.sound = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("sound", ofType: "caf"))
    self.audioPlayer = AVAudioPlayer(contentsOfURL: sound, error: nil)

    return true
}

func application(application: UIApplication!, didReceiveLocalNotification notification: UILocalNotification!){
    audioPlayer.play()
    NSLog("received local notif")


}

您需要对AVAudioPlayerbtw 保持强烈的引用,以便它不会被释放,并且audioplayer.play()不会执行任何操作…


问题答案:

最终,我设法使用NSTimer做到了。

func applicationWillResignActive(application: UIApplication) {
    var timer = NSTimer(fireDate: NSDate(timeIntervalSinceNow: 20), interval: 60.0, target: self, selector: Selector("playAlarm"), userInfo: nil, repeats: false)
    NSRunLoop.currentRunLoop().addTimer(timer, forMode: NSDefaultRunLoopMode)
}
func playAlarm() {
    self.sound = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("sound", ofType: "caf"))
    self.audioPlayer = AVAudioPlayer(contentsOfURL: sound, error: nil)
    audioPlayer.play()
}

UILocalNotification与此计时器同步,以提供良好的用户体验。

尽管我不确定这是否会通过苹果,但是我看不出为什么它不会通过:\



 类似资料:
  • 我正在使用FCM在代码下推送通知,以便在收到通知时播放声音 我称之为OnMessageReceived方法,但声音仅在应用程序位于前台时播放,而不是在应用程序位于后台时播放

  • 本文向大家介绍Android自定义View 实现闹钟唤起播放闹钟铃声功能,包括了Android自定义View 实现闹钟唤起播放闹钟铃声功能的使用技巧和注意事项,需要的朋友参考一下 先上图看一下闹钟唤期页面的效果 实现的功能: 1:转动的图片根据天气情况更换 2:转动时间可以设置,转动结束,闹铃声音就结束 3:光圈颜色渐变效果 直接上代码啦: 以上所述是小编给大家介绍的Android自定义View

  • 我想在我的循环应用程序中持续播放音乐,就像商店中的许多游戏应用程序一样。 但是,我不确定何时初始化音乐循环开始以及如何停止它。我创建了一个类,其中包含开始和停止音乐的逻辑。 我的应用程序结构也是这样的 主飞镖 Wrapper.dart

  • 这是在它自己的函数中,在同一个文件中: 你知道如何使用YouTube SDK在后台继续播放声音吗?还有,是不是可以只播放背景中的声音,不播放视频? 感谢任何帮助。

  • null null null (void)applicationdidbeceeactive:(UIApplication*)application { } null { } -(无效)appdidbeceactive }

  • 我一直在做IOS报警应用。我将在应用程序中创建一个警报。所以我需要警报的工作,作为“时钟”应用程序在iOS的工作。 当告警时间达到且app未运行时。警报声音将播放,用户可以通过点击取消或打盹来停止它。