像 iPod App 一样在 iPhone 锁定时继续播放音频

发布于 2024-12-27 20:25:40 字数 593 浏览 1 评论 0原文

即使用户锁定手机,我也需要继续播放。我在互联网上看到了一些解决方案,但似乎都不起作用,例如:

AudioSessionSetActive(true);
    // Set up audio session, to prevent iPhone from deep sleeping, while playing sounds
    UInt32 sessionCategory = kAudioSessionCategory_MediaPlayback;
    AudioSessionSetProperty (
                             kAudioSessionProperty_AudioCategory,
                             sizeof (sessionCategory),
                             &sessionCategory
                             );

我包括 AVFoundationAudioToolboxMediaPlayer代码>.

I need to continue the playback even when the user locks the phone. I've seen some solution over the internet, but none of them seem to work, such as:

AudioSessionSetActive(true);
    // Set up audio session, to prevent iPhone from deep sleeping, while playing sounds
    UInt32 sessionCategory = kAudioSessionCategory_MediaPlayback;
    AudioSessionSetProperty (
                             kAudioSessionProperty_AudioCategory,
                             sizeof (sessionCategory),
                             &sessionCategory
                             );

I'm inlcuding AVFoundation, AudioToolbox, and MediaPlayer.

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

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

发布评论

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

评论(3

溺渁∝ 2025-01-03 20:25:40

我想出了如何做到这一点。

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

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

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

I 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 20:25:40

例如,如果您有一个播放列表,并且想要在后台模式或锁定模式下播放下一首歌曲,请在 viewDidLoad 上添加以下代码行:

[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];

这使得应用程序支持远程控制事件。

If you have a playlist for example and want to play the next song while in background mode or lock mode add this line of code on your viewDidLoad:

[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];

This makes the app supports remote control events.

盗心人 2025-01-03 20:25:40

请参阅以下链接:

如何继续音乐屏幕自动锁定后播放

在 iPhone 锁定时播放音频

如何在屏幕操作系统锁定时使用 AVAudioPlayer 播放音乐

尝试 :

// Set up audio session, to prevent iPhone from deep sleeping, while playing sounds
UInt32 sessionCategory = kAudioSessionCategory_MediaPlayback;

AudioSessionSetProperty (kAudioSessionProperty_AudioCategory, sizeof (sessionCategory), &sessionCategory);

AudioSessionSetActive(true);

Refer following Links:

How to Continue Music Play after Screen Auto-Lock

Playing audio while iPhone is locked

how to play music using AVAudioPlayer when screen os locked

Try :

// Set up audio session, to prevent iPhone from deep sleeping, while playing sounds
UInt32 sessionCategory = kAudioSessionCategory_MediaPlayback;

AudioSessionSetProperty (kAudioSessionProperty_AudioCategory, sizeof (sessionCategory), &sessionCategory);

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