/// <summary> /// The Run method is the entry point of a background task. /// </summary> /// <param name="taskInstance"></param> 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; deferral = taskInstance.GetDeferral(); }
/// <summary> /// The Run method is the entry point of a background task. /// </summary> /// <param name="taskInstance"></param> public void Run(IBackgroundTaskInstance taskInstance) { Debug.WriteLine("Background Audio Task " + taskInstance.Task.Name + " starting..."); // Initialize SystemMediaTransportControls (SMTC) for integration with // the Universal Volume Control (UVC). // // The UI for the UVC must update even when the foreground process has been terminated // and therefore the SMTC is configured and updated from the background task. smtc = BackgroundMediaPlayer.Current.SystemMediaTransportControls; smtc.ButtonPressed += smtc_ButtonPressed; smtc.PropertyChanged += smtc_PropertyChanged; smtc.IsEnabled = true; smtc.IsPauseEnabled = true; smtc.IsPlayEnabled = true; smtc.IsNextEnabled = true; smtc.IsPreviousEnabled = true; DataObject.DisableDispatcher = true; // Read persisted state of foreground app var value = ApplicationSettingsHelper.ReadResetSettingsValue(ApplicationSettingsConstants.AppState); if (value == null) { foregroundAppState = AppState.Unknown; } else { foregroundAppState = EnumHelper.Parse <AppState>(value.ToString()); } // Add handlers for MediaPlayer BackgroundMediaPlayer.Current.CurrentStateChanged += Current_CurrentStateChanged; BackgroundMediaPlayer.Current.MediaEnded += Current_MediaEnded; // Initialize message channel BackgroundMediaPlayer.MessageReceivedFromForeground += BackgroundMediaPlayer_MessageReceivedFromForeground; // Send information to foreground that background task has been started if app is active if (foregroundAppState != AppState.Suspended) { MessageService.SendMessageToForeground(new BackgroundAudioTaskStartedMessage()); } ApplicationSettingsHelper.SaveSettingsValue(ApplicationSettingsConstants.BackgroundTaskState, BackgroundTaskState.Running.ToString()); deferral = taskInstance.GetDeferral(); // This must be retrieved prior to subscribing to events below which use it // Mark the background task as started to unblock SMTC Play operation (see related WaitOne on this signal) backgroundTaskStarted.Set(); // Associate a cancellation and completed handlers with the background task. taskInstance.Task.Completed += TaskCompleted; taskInstance.Canceled += new BackgroundTaskCanceledEventHandler(OnCanceled); // event may raise immediately before continung thread excecution so must be at the end }
/// <summary> /// The Run method is the entry point of a background task. /// </summary> /// <param name="taskInstance"></param> 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 = false; systemmediatransportcontrol.IsPreviousEnabled = false; systemmediatransportcontrol.IsFastForwardEnabled = false; // 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, "BC Started"); BackgroundMediaPlayer.SendMessageToForeground(message); } BackgroundTaskStarted.Set(); backgroundtaskrunning = true; ApplicationSettingsHelper.SaveSettingsValue(Constants.BackgroundTaskState, Constants.BackgroundTaskRunning); deferral = taskInstance.GetDeferral(); }
/// <summary> /// Start playlist and change UVC state /// </summary> private async Task StartPlayback(int InternalID = 0) { if (InternalID != 0) { CurrentMix = Playlist.Single(m => m.InternalID == InternalID); if (CurrentMix != null) { playbackStartedPreviously = true; await PlayCurrentMix(); return; } } // If playback was already started once we can just resume playing. if (!playbackStartedPreviously) { playbackStartedPreviously = true; // If the task was cancelled we would have saved the current track and its position. We will try playback from there. var currentTrackId = ApplicationSettingsHelper.ReadResetSettingsValue(ApplicationSettingsConstants.TrackId); if (currentTrackId != null) { CurrentMix = Playlist.Single(m => m.InternalID == (int)currentTrackId); if (CurrentMix == null) { CurrentMix = Playlist.FirstOrDefault(); } await PlayCurrentMix(); } } else { BackgroundMediaPlayer.Current.Play(); } }
/// <summary> /// Start playlist and change UVC state /// </summary> private void StartPlayback() { try { if (Playlist.CurrentTrackName == string.Empty) { //If the task was cancelled we would have saved the current track and its position. We will try playback from there var currenttrackname = ApplicationSettingsHelper.ReadResetSettingsValue(Constants.CurrentTrack); var currenttrackposition = ApplicationSettingsHelper.ReadResetSettingsValue(Constants.Position); if (currenttrackname != null) { if (currenttrackposition == null) { // play from start if we dont have position Playlist.StartTrackAt((string)currenttrackname); } else { // play from exact position otherwise Playlist.StartTrackAt((string)currenttrackname, TimeSpan.Parse((string)currenttrackposition)); } } else { //If we dont have anything, play from beginning of playlist. Playlist.PlayAllTracks(); //start playback } } else { BackgroundMediaPlayer.Current.Play(); } } catch (Exception ex) { Debug.WriteLine(ex.ToString()); } }
/// <summary> /// Start playlist and change UVC state /// </summary> private void StartPlayback() { try { //If the task was cancelled we would have saved the current track and its position. We will try playback from there var currenttrackname = ApplicationSettingsHelper.ReadResetSettingsValue(Constants.CurrentTrack); Debug.WriteLine("Start Playback! || TrackName: " + Playlist.CurrentTrackName.ToString()); Playlist.StartSelectedStream((string)currenttrackname); if (Playlist.CurrentTrackName == "128") { currentTitle = "Vocaloid Radio 128 kbps"; } else { currentTitle = "Vocaloid Radio 192 kbps"; } UpdateUVCOnNewTrack(); BackgroundMediaPlayer.Current.Play(); } catch (Exception ex) { Debug.WriteLine(ex.ToString()); } }
/// <summary> /// Start playlist and change UVC state /// </summary> private void StartPlayback() { try { // If playback was already started once we can just resume playing. if (!playbackStartedPreviously) { playbackStartedPreviously = true; // If the task was cancelled we would have saved the current track and its position. We will try playback from there. var currentTrackId = ApplicationSettingsHelper.ReadResetSettingsValue(ApplicationSettingsConstants.TrackId); var currentTrackPosition = ApplicationSettingsHelper.ReadResetSettingsValue(ApplicationSettingsConstants.Position); if (currentTrackId != null) { // Find the index of the item by name var index = playbackList.Items.ToList().FindIndex(item => GetTrackId(item).ToString() == (string)currentTrackId); if (currentTrackPosition == null) { // Play from start if we dont have position Debug.WriteLine("StartPlayback: Switching to track " + index); playbackList.MoveTo((uint)index); // Begin playing BackgroundMediaPlayer.Current.Play(); } else { // Play from exact position otherwise TypedEventHandler <MediaPlaybackList, CurrentMediaPlaybackItemChangedEventArgs> handler = null; handler = (MediaPlaybackList list, CurrentMediaPlaybackItemChangedEventArgs args) => { if (args.NewItem == playbackList.Items[index]) { // Unsubscribe because this only had to run once for this item playbackList.CurrentItemChanged -= handler; // Set position var position = TimeSpan.Parse((string)currentTrackPosition); Debug.WriteLine("StartPlayback: Setting Position " + position); BackgroundMediaPlayer.Current.Position = position; // Begin playing BackgroundMediaPlayer.Current.Play(); } }; playbackList.CurrentItemChanged += handler; // Switch to the track which will trigger an item changed event Debug.WriteLine("StartPlayback: Switching to track " + index); playbackList.MoveTo((uint)index); } } else { // Begin playing BackgroundMediaPlayer.Current.Play(); } } else { // Begin playing BackgroundMediaPlayer.Current.Play(); } } catch (Exception ex) { Debug.WriteLine(ex.ToString()); } }