像 iPod App 一样在 iPhone 锁定时继续播放音频
即使用户锁定手机,我也需要继续播放。我在互联网上看到了一些解决方案,但似乎都不起作用,例如:
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
);
我包括 AVFoundation
、AudioToolbox
和 MediaPlayer
代码>.
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我想出了如何做到这一点。
首先,将这些框架包含到您的项目中:
AudioToolbox
、CoreAudio
、MediaPlayer
和AVFoundation
。将它们全部导入到将放置播放器的 viewController 中。分配并开始播放音频后,插入以下代码:最后,转到应用程序 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
andAVFoundation
. Import them all to the viewController where your player will be placed. After you allocated and started playing the audio, insert the following code: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 asaudio
. And you're done! Enjoy you're background audio playing app!例如,如果您有一个播放列表,并且想要在后台模式或锁定模式下播放下一首歌曲,请在 viewDidLoad 上添加以下代码行:
这使得应用程序支持远程控制事件。
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:
This makes the app supports remote control events.
请参阅以下链接:
如何继续音乐屏幕自动锁定后播放
在 iPhone 锁定时播放音频如何在屏幕操作系统锁定时使用 AVAudioPlayer 播放音乐
尝试 :
Refer following Links:
How to Continue Music Play after Screen Auto-Lock
Playing audio while iPhone is lockedhow to play music using AVAudioPlayer when screen os locked
Try :