ios - How to resume playing audio after the camera goes away? -
i'm working on app plays audio in background while using other apps using avaudioplayer. when camera opened music silenced none of methods in appdelegate app lifecycle called can't save playlist position or playback time song.
in addition when camera dismissed have app resume playing background music again haven't found callback method allow observe change.
do know how observe camera did become active , camera dismissed while app running in background mode?
here's how solved it.
[[nsnotificationcenter defaultcenter] addobserver:self selector:@selector(handleaudiosessioninterruption:) name:avaudiosessioninterruptionnotification object:[avaudiosession sharedinstance]];
handle interuption.
-(void)handleaudiosessioninterruption:(nsnotification*)notification { //nslog(@"%@",notification); nsnumber *interruptiontype = [[notification userinfo] objectforkey:avaudiosessioninterruptiontypekey]; //1 interuption start, 0 interuption ends nsnumber *interruptionoption = [[notification userinfo] objectforkey:avaudiosessioninterruptionoptionkey]; if ([interruptiontype integervalue] == avaudiosessioninterruptiontypebegan) { nslog(@"player %d interupted",playernumber); // • audio has stopped, inactive // • change state of ui, etc., reflect non-playing state [self.playpausebutton settitle:@">" forstate:uicontrolstatenormal]; self.playingaudio = no; return; } if ([interruptiontype integervalue] == avaudiosessioninterruptiontypeended) { if ([interruptionoption integervalue] == avaudiosessioninterruptionoptionshouldresume) { nslog(@"player %d resume",playernumber); [self.playpausebutton settitle:@"||" forstate:uicontrolstatenormal]; self.playingaudio = yes; nserror *error = nil; avaudiosession *asession = [avaudiosession sharedinstance]; [asession setmode:avaudiosessionmodedefault error:&error]; //& means value @ address [asession setcategory:avaudiosessioncategoryplayback error:&error]; //[asession setcategory:avaudiosessioncategoryplayback withoptions:avaudiosessioncategoryoptionmixwithothers error:&error]; //[asession setmode:avaudiosessionmodespokenaudio error:&error]; [asession setactive: yes error: &error]; [self.audioplayer play]; } // • make session active // • update user interface // • avaudiosessioninterruptionoptionshouldresume option }