iPhone 锁定时继续播放音频

发布于 2024-12-27 13:21:58 字数 217 浏览 3 评论 0原文

当 iPhone 自动锁定或用户锁定手机时,如何继续在 iPhone 中播放音频?

我已经尝试过了

session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryPlayback error:NULL];

,但这似乎不起作用。

How can I continue an audio playback in iPhone when it auto-locks or the user locks the phone?

I already tried

session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryPlayback error:NULL];

but that doesn't seem to work.

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

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

发布评论

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

评论(2

爱*していゐ 2025-01-03 13:21:58

我终于知道该怎么做了!

首先,将这些框架包含到您的项目中:AudioToolbox、CoreAudio、MediaPlayer 和 AVFoundation。将它们全部导入到将放置播放器的 viewController 中。分配并开始播放音频后,插入以下代码:

UInt32 sessionCategory = kAudioSessionCategory_MediaPlayback;
AudioSessionSetProperty (kAudioSessionProperty_AudioCategory, sizeof (sessionCategory), &sessionCategory);
AudioSessionSetActive(true);

最后,转到应用程序 Info.plist 文件并添加名为 UIBackgroundModes 的行。新行将是一个数组,包含 1 个项目,即项目 0。为此,您只需将值设置为音频。你就完成了!享受您的背景音频播放应用程序!

I finally figured out how to do this!

First of all, include these frameworks to your project: AudioToolbox, CoreAudio, MediaPlayer and AVFoundation. Import them all to the viewController where your player will be placed. After you allocated and started playing the audio, insert the following code:

UInt32 sessionCategory = kAudioSessionCategory_MediaPlayback;
AudioSessionSetProperty (kAudioSessionProperty_AudioCategory, sizeof (sessionCategory), &sessionCategory);
AudioSessionSetActive(true);

And finally, go to your app Info.plist file and add a row named UIBackgroundModes. The new row will be an array and will contain 1 item, the item 0. To this you just set the value as audio. And you're done! Enjoy you're background audio playing app!

诗笺 2025-01-03 13:21:58

此链接应该有所帮助。

您需要防止应用程序进入深度睡眠模式:

UInt32 category = kAudioSessionCategory_MediaPlayback;
OSStatus result = AudioSessionSetProperty(kAudioSessionProperty_AudioCategory,
                                                                            sizeof(category), &category);

if (result){
    DebugLog(@"ERROR SETTING AUDIO CATEGORY!\n");
}

result = AudioSessionSetActive(true);
if (result) {
    DebugLog(@"ERROR SETTING AUDIO SESSION ACTIVE!\n");
}

This link should help.

You need to prevent the app from entering a deep sleep mode:

UInt32 category = kAudioSessionCategory_MediaPlayback;
OSStatus result = AudioSessionSetProperty(kAudioSessionProperty_AudioCategory,
                                                                            sizeof(category), &category);

if (result){
    DebugLog(@"ERROR SETTING AUDIO CATEGORY!\n");
}

result = AudioSessionSetActive(true);
if (result) {
    DebugLog(@"ERROR SETTING AUDIO SESSION ACTIVE!\n");
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文