ios - AVAudioPlayer doesn't start playing in background -
i using avaudioplayer , nsfilehandle audio streaming. basic idea save streaming audio data device file system , use avaudioplayer playback file saved. avaudioplayer can still play file streaming in progress.
i have uibutton start streaming/downloading. when downloaded data accumulated specific amount of bytes, avaudioplayer play (the "play" method of avaudioplayer triggered) automatically. question is: tap button start streaming/downloading, press iphone's home button make app go background. download keeps working in background "play" method of avaudioplayer returns "no" means "play" method doesn't play audio.
i added avaudiosession related code audio player's init method:
- (id) initwithurl:(nsstring *) downloadedmusicurl { if ((self = [super init])) { [[uiapplication sharedapplication] beginreceivingremotecontrolevents]; cfurlref url = cfurlcreatewithfilesystempath (null, (cfstringref)downloadedmusicurl, kcfurlposixpathstyle, false); audioplayer = [[avaudioplayer alloc] initwithcontentsofurl:cfbridgingrelease(url) error:nil]; avaudiosession *session = [avaudiosession sharedinstance]; [session setactive:yes error:nil]; [session setcategory:avaudiosessioncategoryplayback error:nil]; } return self; } also in info.plist added item "app plays audio" key "required background modes". however, "play" still doesn't called in background. else did miss? lot in advance.
your app cannot start playing audio in background. , makes sense. think device if app user not actively using start producing sound! remember, user, sending app background naive equivalent of quitting app. app must not rise zombie dead , start making noise!
the background audio setting allows app producing audio while in foreground continue when then goes the background. can continue being in charge of audio (if receives remote events) until other app produces sound; after that, app once again out of running until brought foreground , produces sound.
so you'll need start playing sound before app goes background, if want work.
Comments
Post a Comment