ios - Play a background sound in WatchKit app -
i have button in watchkit sends notification main iphone app this.
-(ibaction) startsound { //turn sound on nsstring *requeststring = [nsstring stringwithformat:@"startsound"]; // string arbitrary, must match here , @ iphone side of implementation. nsdictionary *applicationdata = [[nsdictionary alloc] initwithobjects:@[requeststring] forkeys:@[@"startsound"]]; [wkinterfacecontroller openparentapplication:applicationdata reply:^(nsdictionary *replyinfo, nserror *error) { //nslog(@"\nreply info: %@\nerror: %@",replyinfo, error); }]; }
in iphone app delegate have added following code.
- (void)application:(uiapplication *)application handlewatchkitextensionrequest:(nsdictionary *)userinfo reply:(void(^)(nsdictionary *replyinfo))reply { nslog(@"handlewatchkitextensionrequest ..."); nsmutabledictionary *mutdic = [[nsmutabledictionary alloc] init]; //this block asks code put after run in background 10 mins max __block uibackgroundtaskidentifier bgtask; bgtask = [application beginbackgroundtaskwithname:@"mytask" expirationhandler:^{ bgtask = uibackgroundtaskinvalid; }]; nsstring *request = [userinfo objectforkey:@"startsound"]; if ([request isequaltostring:@"startsound"]) { nsstring *soundfilepath = [[nsbundle mainbundle] pathforresource: @"warning" oftype: @"mp3"]; nsurl *fileurl = [[nsurl alloc] initfileurlwithpath: soundfilepath]; myaudioplayer1 = [[avaudioplayer alloc] initwithcontentsofurl:fileurl error:nil]; myaudioplayer1.numberofloops = -1; //inifinite [myaudioplayer1 play]; } reply(nil); //must reply no matter //once code done , reply has been sent end bg-handler [application endbackgroundtask:bgtask]; }
yet, when app went apple review, got rejected reasons app had running in foreground sound feature work. did miss?
10.6 - apple , our customers place high value on simple, refined, creative, thought through interfaces. take more work worth it. apple sets high bar. if user interface complex or less good, may rejected
10.6 details
we still found apple watch app requires containing app running in foreground on iphone in order play siren sounds, provides poor user experience.
next steps
please see uiapplicationdelegate protocol reference implement method , use respond requests apple watch app.
because method called while app in background, call beginbackgroundtaskwithname:expirationhandler: method @ start of implementation , endbackgroundtask: method after have processed reply , executed reply block. starting background task ensures app not suspended before has chance send reply.
how long audio clip? in code you've shared looks reply() called immediately, wouldn't give clip chance play. should delay calling reply() until audio clip has completed.
Comments
Post a Comment