2

I have designed a music player for windows phone 8.1. When i clicked the play button foreground app sends the messsage to background audio class. background audio class plays my music. All is ok. But i have a problem. When i close my app (pressin back button and slide down) back ground music is still playing. How i can close it ? thanks.

 public void Run(IBackgroundTaskInstance taskInstance)
    {
        Debug.WriteLine("Background Audio Task " + taskInstance.Task.Name + " starting...");
        // Initialize SMTC object to talk with UVC. 
        //Note that, this is intended to run after app is paused and 
        //hence all the logic must be written to run in background process
        systemmediatransportcontrol = SystemMediaTransportControls.GetForCurrentView();
        systemmediatransportcontrol.ButtonPressed += systemmediatransportcontrol_ButtonPressed;
        systemmediatransportcontrol.PropertyChanged += systemmediatransportcontrol_PropertyChanged;
        systemmediatransportcontrol.IsEnabled = true;
        systemmediatransportcontrol.IsPauseEnabled = true;
        systemmediatransportcontrol.IsPlayEnabled = true;
        systemmediatransportcontrol.IsNextEnabled = true;
        systemmediatransportcontrol.IsPreviousEnabled = true;

        // Associate a cancellation and completed handlers with the background task.
        taskInstance.Canceled += new BackgroundTaskCanceledEventHandler(OnCanceled);
        taskInstance.Task.Completed += Taskcompleted;

        var value = ApplicationSettingsHelper.ReadResetSettingsValue(Constants.AppState);
        if (value == null)
            foregroundAppState = ForegroundAppStatus.Unknown;
        else
            foregroundAppState = (ForegroundAppStatus)Enum.Parse(typeof(ForegroundAppStatus), value.ToString());

        //Add handlers for MediaPlayer
        BackgroundMediaPlayer.Current.CurrentStateChanged += Current_CurrentStateChanged;

        //Add handlers for playlist trackchanged
        Playlist.TrackChanged += playList_TrackChanged;

        //Initialize message channel 
        BackgroundMediaPlayer.MessageReceivedFromForeground += BackgroundMediaPlayer_MessageReceivedFromForeground;

        //Send information to foreground that background task has been started if app is active
        if (foregroundAppState != ForegroundAppStatus.Suspended)
        {
            ValueSet message = new ValueSet();
            message.Add(Constants.BackgroundTaskStarted, "");
            BackgroundMediaPlayer.SendMessageToForeground(message);
        }
        BackgroundTaskStarted.Set();
        backgroundtaskrunning = true;

        ApplicationSettingsHelper.SaveSettingsValue(Constants.BackgroundTaskState, Constants.BackgroundTaskRunning);
        deferral = taskInstance.GetDeferral();           
    }

1 Answers1

0

This article describes the application lifecycle of a Windows Store App.

If you look at the very first figure, you can see that there are only 3 events related to application lifecycle:

Activated - Raised when program first starts Suspended - Raised when program is suspended (i.e. the user returns to the Start Screen or another app) Resuming - Raised when the program is awakened from its suspended state. The fourth transition - the one to the "Not Running" state - has no such notification event. The reason is: you don't really know when the app will fully close. Nor should you - Microsoft wants you to perform all of your state-saving logic in the transition from "Running" to "Suspended." In this way, they can free up resources when they deem necessary.

Even if the user forces the program to terminate (by right-clicking it and selecting "Close" from the task menu), you will enter the "Suspended" state for exactly 10 seconds before the program is terminated. So you can rest easy that your state-saving logic will always be executed.

So, You can close music in App Suspending event or state saving event.

Rohit
  • 552
  • 3
  • 15