my app has 2 views 1 mantraas list , playmantraview. whenever user clicks on mantra show playmantraview have play button. when user click on play button initiate background audio task , play mantra. when clicks on arrow close play mantra , end background task calling
backgroundmediaplayer.shutdown();
which stop background task , move mantras view. here after few seconds app closes without error. have hockey app configured log crashes, don't see crash in hockey app.
could please me nail down error background app start code
private async task startbackgroundaudiotask(string filename) { addmediaplayereventhandlers(); log.logdebug("startbackgroundaudiotask - " + filename); serverinitialized = new autoresetevent(false); await task.run(() => { bool result = serverinitialized.waitone(2000); log.logdebug("autoresetevent result - " + result); if (result == true) { var message = new valueset(); message.add(constants.startplayback, new inputmantra() { mantraid = viewmodel.currentmantra.mantraid, reciteid = viewmodel.currentrecitation.recitationid, actualcount = viewmodel.currentrecitation.actualcount, currentcount = viewmodel.currentrecitation.completedcount, filepath = filename, mantraname = viewmodel.currentmantra.mantraname }.tostring()); backgroundmediaplayer.sendmessagetobackground(message); currentmantraid = viewmodel.currentmantra.mantraid; } else { log.logdebug("background audio task didn't start in expected time"); } }); }
when user clicks send message background task
public void stop() { applicationsettingshelper.removekey(constants.currentmantra); currentmantraid = 0; var message = new valueset(); message.add(constants.stopplayback, constants.stopplayback); backgroundmediaplayer.sendmessagetobackground(message); }
and handle message in background task
void saveandstop() { try { if (backgroundtaskrunning) { backgroundmediaplayer.shutdown(); backgroundtaskrunning = false; } } catch (exception ex) { log.logerror("while stopping", ex); } }
will save current state in backgroung task oncancelled method
private void oncanceled(ibackgroundtaskinstance sender, backgroundtaskcancellationreason reason) { // time here save state before process , resources reclaimed log.logdebug("mybackgroundaudiotask " + sender.task.taskid + " cancel requested..."); // set not running backgroundtaskstarted.reset(); applicationsettingshelper.removekey(constants.currentmantra); sendtoforeground(constants.backgroundtaskcancelled, ""); applicationsettingshelper.savesettingsvalue(constants.backgroundtaskstate, constants.backgroundtaskcancelled); //unregister message channel backgroundmediaplayer.messagereceivedfromforeground -= backgroundmediaplayer_messagereceivedfromforeground; //remove handlers mediaplayer backgroundmediaplayer.current.currentstatechanged -= mediaplayer_currentstatechanged; backgroundmediaplayer.current.mediaended -= mediaplayer_mediaended; if (backgroundtaskrunning) { backgroundmediaplayer.shutdown(); backgroundtaskrunning = false; } log.logdebug("mybackgroundaudiotask cancel complete..."); }
Comments
Post a Comment