使 AVFoundation 框架在屏幕淡出时不淡出

发布于 2024-07-26 10:41:53 字数 702 浏览 5 评论 0原文

我正在为 iPhone 实现一些有声读物。 我用的是AVFoundation。 像这样:

NSString *path = [[NSBundle mainBundle] pathForResource:@"intro" ofType:@"mp3"];
        player=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];

我有一个问题。 当屏幕变暗时(单个音频文件可能很长),音频将停止播放。

我用这串代码解决了这个问题

- (void)applicationDidFinishLaunching:(UIApplication *)application {

    // something else here...

    [[UIApplication sharedApplication] setIdleTimerDisabled:YES];

}

这不允许iPhone“睡眠”。 然而你可以猜到这是多么愚蠢:你的电池电量在几分钟内就会下降,而对于持续超过 20 小时的有声读物来说这是不可能的,例如......

那么,你知道一种方法来防止当屏幕休眠 AVAudioPlayer 时发生这种情况吗?不停止播放?

谢谢... 法比奥

I am implementing some audiobooks for iPhone. I used AVFoundation. Something like this:

NSString *path = [[NSBundle mainBundle] pathForResource:@"intro" ofType:@"mp3"];
        player=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];

I have a problem. When the screen goes dark (single audio files can be very long) the audio stops playing.

I solved this problem with this string of code

- (void)applicationDidFinishLaunching:(UIApplication *)application {

    // something else here...

    [[UIApplication sharedApplication] setIdleTimerDisabled:YES];

}

This does not allow the iPhone to "sleep". However you can guess how this is foolish: your battery level goes down in minutes and this is not possible by audiobooks lasting more than 20 hours, for example...

So, do you know a way to prevent that when the screen sleeps the AVAudioPlayer does not stop playing?

Thanks...
Fabio

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

身边 2024-08-02 10:41:53

将音频会话设置为 kAudioSessionCategory_MediaPlayback 以在屏幕锁定时播放。

Set your audio session to kAudioSessionCategory_MediaPlayback to playback while the screen is locked.

缺⑴份安定 2024-08-02 10:41:53

我是否可以建议,一旦您开始播放音频文件,请告诉用户按电源按钮(而不是主页按钮),这将锁定手机。 它不会关闭您的应用程序,但会关闭屏幕并让您的应用程序在后台运行。 目前有几个应用程序可以做到这一点。

Could I suggest that once you start playing the audio file that you tell the user to press the power button (not home button) which will lock the phone. It will not close you app but it will power off the screen with you application running in the background. Currently several apps do this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文