gpt4 book ai didi

iphone - 在后台模式下播放音效

转载 作者:行者123 更新时间:2023-12-02 22:43:57 29 4
gpt4 key购买 nike

我需要在 iPhone 应用程序的背景中播放一些零星的音效。根据我在 iOS 4 上阅读和体验的所有内容,只要我通过将“位置”指定为后台模式来运行 GPS,我就可以让我的应用程序在后台运行。这确实有效。但有时我想播放声音效果......换句话说,它不是我看到的“连续”声音。

但是应用程序正在运行,为什么我不能只使用 AVAudioPlayer 播放一些音效?另一个声音 API 会起作用吗?

啊啊啊!

    NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:@"BEEP" ofType:@"aiff"];
NSURL *fileURL = [[[NSURL alloc] initFileURLWithPath: soundFilePath] autorelease];

AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:nil];

player.volume = 1.0;
[player prepareToPlay];

// play
[player play];

最佳答案

我认为澄清您的应用程序没有在后台运行很重要。相反,您的应用程序处于“休眠”状态。 info.plist您指定的后台进程值告诉操作系统它应该唤醒您的应用程序并允许它响应特定类型的事件。来自 Apple Documentation

Each of the preceding values [audio, location, voip] lets the system know that your application should be woken up at appropriate times to respond to relevant events.



在您的情况下,您的应用程序被卡住并且只能响应您指定的事件类型(位置或 GPS)。

您不清楚这段代码在什么上下文中,所以在这一点上很难告诉您为什么音频没有播放。此外,您需要确保您的应用程序出于指定目的在后台运行。如果您使用 location机制而不在您的应用程序中使用 GPS,您可能会在提交申请时被拒绝。

您可能还想引用 Checklist for Supporting Multitasking以确保您的应用程序的结构实现他们的要求。

关于iphone - 在后台模式下播放音效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3261182/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com