如何使用 AVFoundation for iPhone/iPoad Touch 在 iOS 4 上播放背景音频?
我认为从标题中可以清楚地看出......
问题: 我已经有一些 Objective-C 代码,它使用 AVFoundation 及其 AVAudioPlayer 在我的 iPhone 应用程序中播放一些声音,它采用文档中文件夹的路径,并使用 AVAudioPlayer 对象逐个播放其中的所有文件(我实现了 AVAudionPlayerDelegate,这样我就可以在当前文件完成后给播放器下一个文件)...
问题: 现在,当我的应用程序在后台运行时,如何才能保持这些声音播放,因为我将设备升级到了 iOS 4.1,还升级了 iOS SDK 和 Xcode...
我做了什么: 我将“UIBackgroundModes”添加到我的info.plist文件中,并在其中放入“audio”,然后将AVAudioSessionCategoryPlayback中的sharedInstance的类别设置为AVAudioSessionCategoryPlayback,并将setActive设置为YES,那里没有发生错误,但当我按下主页按钮时,音乐停止!
我还应该做什么?
提前致谢
I think that it's clear from the title...
The Problem:
I already have some objective-c code that uses AVFoundation and its AVAudioPlayer to play some sounds in my iPhone apps, it takes a path for a folder in the documents and plays all the files in it one after the other using an AVAudioPlayer object (I implemented AVAudionPlayerDelegate so I can give the player the next file when the current one is done)...
The Question:
Now how can I keep these sounds playing when my app is in the background cause I upgraded my devices to iOS 4.1 and also my iOS SDK and Xcode...
What I did:
I added the "UIBackgroundModes" to my info.plist file and put "audio" in it and I set the category for the sharedInstance in AVAudionSeession to AVAudioSessionCategoryPlayback and setActive to YES and no errors happened there but still when I press the home button, the music stops!
What else should I do?
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我遇到了这个问题,并通过以下方式解决了:我添加了 UIBackgroundModes 并将音频放在那里。然后在我的 AudioPlayer 类(我播放音频的类)中的“init”方法中,我放置了以下代码:
注意:在模拟器中这将不起作用!你需要真实的设备。
I had this problem,and solved this way: I added UIBackgroundModes and put audio there. Then in my AudioPlayer class (class where i play my audio) in "init" method i've put this code:
NOTE: In simulator this will not work! You need real device.