简体   繁体   English

iPhone 锁定时的音频画外音

[英]Audio voiceover when iPhone is locked

Solution below下面的解决方案

I am working on a running app which gives voiceover instruction at the start of each exercise.我正在开发一个正在运行的应用程序,它会在每次练习开始时提供画外音指导。 It works as intended when the device is active but doesn't work when the phone is locked.它在设备处于活动状态时按预期工作,但在手机锁定时不起作用。 [ Current audio continues but no future audio plays. [当前音频继续,但没有未来的音频播放。 ] ]

Question How can I start playing an audio voiceover while the users iPhone is locked.问题当用户 iPhone 被锁定时,如何开始播放音频画外音。

Currently I track the workout using Timer.scheduledTimer for 2 timers I display [current activity and the overall workout].目前,我使用Timer.scheduledTimer跟踪我显示的 2 个计时器的锻炼[当前活动和整体锻炼]。 When the timer hits a pre-defined time towards the end of one activity, the voiceover audio plays to introduce the next activity.当计时器达到一个活动结束时的预定义时间时,会播放画外音以介绍下一个活动。 The timer firing is what starts the audio and I believe this is the issue .计时器触发是启动音频的原因,我相信这就是问题所在 I can pause the workout, skip sections and all works fine - until the device is locked.我可以暂停锻炼,跳过部分,一切正常 - 直到设备被锁定。

If an audio voiceover is playing when I press the home button or lock the iPhone it continues to play as expected.如果在我按下主页按钮或锁定 iPhone 时正在播放音频画外音,它会继续按预期播放。 The issue is then that the timer doesn't fire so the next audio voiceover [in say 90 seconds] is never played.问题是计时器不会触发,因此永远不会播放下一个音频画外音 [例如 90 秒]。

Some of the answers and comments I've read have said that this functionality just isn't possible in iOS.我读过的一些答案和评论说这个功能在 iOS 中是不可能的。 The Couch to 5k app https://itunes.apple.com/gb/app/one-you-couch-to-5k/id1082307672?mt=8 uses this functionality so I know it's possible to achieve, I just don't know how they're doing it. Couch to 5k 应用程序https://itunes.apple.com/gb/app/one-you-couch-to-5k/id1082307672?mt=8使用此功能,所以我知道可以实现,我只是不知道他们是怎么做的。 [It will be a dedicated member that downloads it to see what I mean :)] [这将是一个专门的成员下载它以了解我的意思:)]

From searching SO I've read a lot of post saying that Timer or NSTimer can't run when the app is in the background or the phone is locked.通过搜索,我读了很多帖子,说当应用程序在后台或手机被锁定时, TimerNSTimer无法运行。 Any posts that say it can work are old and based on iOS4/5任何说它可以工作的帖子都是旧的并且基于 iOS4/5

I've read about suggestions of using silence and essentially having 1 long audio file.我已经阅读了有关使用静音和基本上拥有 1 个长音频文件的建议。 While this would pose some new challenges for skipping the sections in the workout I've also read comments that say this behaviour would not pass Apple's testing of the app for the AppStore.虽然这会给跳过锻炼中的部分带来一些新的挑战,但我也读过评论说这种行为不会通过 Apple 对 AppStore 应用程序的测试。 The 3rd downside being that my audio file size would increase.第三个缺点是我的音频文件大小会增加。

An option I've seen is local notifications, however I'm yet to see an example of one used to play audio voiceover when the app is in the background.我见过的一个选项是本地通知,但是我还没有看到一个用于在应用程序处于后台时播放音频画外音的示例。

How can I achieve the same functionality as the Couch to 5k app?如何实现与 Couch to 5k 应用程序相同的功能?

Thanks谢谢

Ok, I worked up a solution to play audio prompts to the user throughout the course of a 30 minute workout, even if the iPhone is locked or the app is in the background.好的,我想出了一个解决方案,在 30 分钟的锻炼过程中向用户播放音频提示,即使 iPhone 被锁定或应用程序在后台。 The app has been submitted and approved for the AppStore .该应用程序已提交并批准用于 AppStore

To get the app to perform as required I use 2 AVAudioPlayers ;为了让应用程序按要求执行,我使用了 2 个AVAudioPlayers one plays the voiceover, the other plays a 15 minute track of silence [as it's a separate track and just silent it's a low quality .mp3 and only added 900kb to the app size].一个播放画外音,另一个播放 15 分钟的静音曲目 [因为它是一个单独的曲目,只是静音它是一个低质量的 .mp3 并且只增加了 900kb 到应用程序大小]。 15 minutes well exceeds any interval between voiceovers so it works perfect. 15 分钟远远超过画外音之间的任何间隔,因此它完美无缺。

When the user starts an exercise the first audio prompt is played.当用户开始锻炼时,会播放第一个音频提示。 From that point, until the user stops the exercise there will be audio playing in the background.从那时起,直到用户停止锻炼,后台都会播放音频。 Because background audio is playing, the Timer still operates.由于正在播放背景音频,定时器仍然运行。

When a voiceover finishes, the AVAudioPlayerDelegate method audioPlayerDidFinishPlaying is called.当画外音结束时,会调用AVAudioPlayerDelegate方法audioPlayerDidFinishPlaying Within this method I reset the audio properties [I'll explain why shortly], instantiate my silent AVAudioPlayer , and start playing the 15 minute track of silence.在这个方法中,我重置了音频属性 [稍后我会解释原因],实例化我的静音AVAudioPlayer ,并开始播放 15 分钟的静音曲目。 Playing another track using this delegate method means that there is no real break in background audio so the app is kept alive.使用此委托方法播放另一首曲目意味着背景音频没有真正中断,因此应用程序保持活动状态。 With the app 'alive' the timer continues to countdown.随着应用程序“活着”,计时器继续倒计时。 When it's time for another voiceover the timer calls a method which resets the audio category, instantiates my voiceover AVAudioPlayer and plays a voiceover.当需要进行另一个画外音时,计时器会调用一个方法来重置音频类别,实例化我的画外音AVAudioPlayer并播放画外音。 This process is repeated until the final voiceover.重复这个过程直到最后的画外音。 When that completes no further silence is played and the app can safely be backgrounded.完成后,不再播放静音,应用程序可以安全地进入后台。

The audio voiceover needs to duck the users music [reduces the music volume so that the audio can be clearly heard].音频画外音需要躲避用户的音乐[降低音乐音量,让音频可以清晰地听到]。 The silent audio shouldn't do that, music should continue to play at full volume.静音音频不应该这样做,音乐应该继续以最大音量播放。 As both AVAudioPlayers use the AVAudioSession.sharedInstance I found that I had to deactivate the sharedInstance, reset the properties, with or without .duckOthers , and then reactivate the sharedInstance before instantiating my AVAudioSession.由于两个 AVAudioPlayers 都使用AVAudioSession.sharedInstance我发现我必须停用 sharedInstance,使用或不使用.duckOthers重置属性,然后在实例化我的 AVAudioSession 之前重新激活 sharedInstance。 That then gave me the desired result.然后给了我想要的结果。 Constant background audio that only ducked the users music when a voiceover played.持续的背景音频只会在播放画外音时避开用户的音乐。

do{
    try AVAudioSession.sharedInstance().setActive(false)
    try AVAudioSession.sharedInstance().setCategory(.playback, mode: .default, options: [.interruptSpokenAudioAndMixWithOthers, .duckOthers])
    try AVAudioSession.sharedInstance().setActive(true)
} catch{
        print(error.localizedDescription)
}

I know that some said Apple do not allow silent audio, however, in this circumstance the silent audio only plays for the duration of the exercise routine.我知道有人说 Apple 不允许静音音频,但是,在这种情况下,静音音频仅在锻炼期间播放。 As soon as it ends the app can terminate, thereby not keeping the app alive any longer than necessary.一旦结束,应用程序就可以终止,从而不会让应用程序保持活动状态超过必要时间。

My understanding of VoiceOver leads me to the way that only a foreground application can control what it reads out.我对 VoiceOver 的理解使我明白只有前台应用程序才能控制它读出的内容。

You need focusing with VoiceOver to make it speak and you focus nothing when the device is locked or your app is in the background mode.您需要使用 VoiceOver聚焦才能让它说话,而当设备被锁定或您的应用程序处于后台模式时,您什么也不会聚焦。

I suggest to take a look at the speech synthesis or the AVAudioSession class to enable background audio : both might be good workarounds to take over in the background mode: just try and let us know.我建议查看语音合成AVAudioSession类以启用背景音频:两者都可能是在后台模式下接管的好方法:请尝试让我们知道。 ;o) ;o)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM