objective c - iOS background audio not playing -


i have app uses corebluetooth background modes. when event happens need play alarm sound. works fine in foreground , bluetooth functionality works fine in background. have working schedules uilocalnotification's in background sound alarm, don't lack of volume control these want play alarm sound using avaudioplayer.

i've added background mode audio .plist file can't sound play properly.

i using singleton class alarm , initialise sound this:

nsurl *url = [nsurl fileurlwithpath:[[nsbundle mainbundle]                                     pathforresource:soundname                                     oftype:@"caf"]];  player = [[avaudioplayer alloc] initwithcontentsofurl:url error:nil]; player.numberofloops = -1; [player setvolume:1.0]; 

i start sound this:

-(void)startalert {     playing = yes;     [player play];     if (vibrate)         [self vibratepattern]; } 

and use vibration:

-(void)vibratepattern {     if (vibrate && playing) {         audioservicesplaysystemsound(ksystemsoundid_vibrate);         [self performselector:@selector(vibratepattern) withobject:nil afterdelay:0.75];     } } 

the vibration works fine in background, no sound. if use systemsound play sound this, plays fine (but no volume control):

audioservicescreatesystemsoundid((__bridge cfurlref)url, &_sound); audioservicesplaysystemsound(_sound); 

so reason why avaudioplayer not playing sound file?

thanks

edit -------

the sound play if it's playing when app backgrounded. making start play whilst backgrounded not working.

add key named required background modes in property list (.plist) file ..

as following picture..

enter image description here may help..

and add following code in

appdelegate.h

#import <avfoundation/avfoundation.h> #import <audiotoolbox/audiotoolbox.h> 

appdelegate.m

in application didfinishlaunchingwithoptions

[[avaudiosession sharedinstance] setdelegate:self]; [[avaudiosession sharedinstance] setcategory:avaudiosessioncategoryplayback error:nil]; [[avaudiosession sharedinstance] setactive:yes error:nil]; [[uiapplication sharedapplication] beginreceivingremotecontrolevents];  uint32 size = sizeof(cfstringref); cfstringref route; audiosessiongetproperty(kaudiosessionproperty_audioroute, &size, &route); nslog(@"route = %@", route); 

if want changes per events have add following code in appdelegate.m

- (void)remotecontrolreceivedwithevent:(uievent *)theevent {      if (theevent.type == uieventtyperemotecontrol)  {         switch(theevent.subtype)        {             case uieventsubtyperemotecontrolplay:                 [[nsnotificationcenter defaultcenter] postnotificationname:@"toggleplaypause" object:nil];                 break;             case uieventsubtyperemotecontrolpause:                 [[nsnotificationcenter defaultcenter] postnotificationname:@"toggleplaypause" object:nil];                 break;             case uieventsubtyperemotecontrolstop:                 break;             case uieventsubtyperemotecontroltoggleplaypause:                 [[nsnotificationcenter defaultcenter] postnotificationname:@"toggleplaypause" object:nil];                 break;             default:                 return;         }     } } 

based on notification have work on it..

code.tutsplus.com provides tutorial.

for handsetbluetooth have add following code in appdelegate

uint32 size = sizeof(cfstringref);     cfstringref route;     audiosessiongetproperty(kaudiosessionproperty_audioroute, &size, &route);     nslog(@"route = %@", route);     nsstring *routestring=[nsstring stringwithformat:@"%@",route];     if([routestring isequaltostring:@"headsetbt"]){         uint32 allowbluetoothinput = 1;         audiosessionsetproperty (kaudiosessionproperty_overridecategoryenablebluetoothinput,sizeof (allowbluetoothinput),&allowbluetoothinput);     } 

Comments

Popular posts from this blog

java - WrongTypeOfReturnValue exception thrown when unit testing using mockito -

php - Magento - Deleted Base url key -

android - How to disable Button if EditText is empty ? -