开发者

How to play background audio using AVFoundation for iPhone/iPoad Touch with iOS 4?

开发者 https://www.devze.com 2023-01-17 04:50 出处:网络
I think that it\'s clear f开发者_JS百科rom 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

I think that it's clear f开发者_JS百科rom 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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号