如何使用 AVFoundation for iPhone/iPoad Touch 在 iOS 4 上播放背景音频?

发布于 2024-09-24 01:14:19 字数 587 浏览 6 评论 0原文

我认为从标题中可以清楚地看出......

问题: 我已经有一些 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 技术交流群。

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

发布评论

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

评论(1

挽手叙旧 2024-10-01 01:14:19

我遇到了这个问题,并通过以下方式解决了:我添加了 UIBackgroundModes 并将音频放在那里。然后在我的 AudioPlayer 类(我播放音频的类)中的“init”方法中,我放置了以下代码:

[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error: nil];

注意:在模拟器中这将不起作用!你需要真实的设备。

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:

[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error: nil];

NOTE: In simulator this will not work! You need real device.

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