public void SendMessageToForeground(PlayQueueConstantBGMessageId messageId, object value) { var message = new ValueSet(); message.Add(PlayQueueMessageHelper.BGMessageIdToMessageString(messageId), value); BackgroundMediaPlayer.SendMessageToForeground(message); }
/// <summary> /// Performs the work of a background task. The system calls this method when the associated /// background task has been triggered. /// </summary> /// <param name="taskInstance"> /// An interface to an instance of the background task. The system creates this instance when the /// task has been triggered to run. /// </param> public void Run(IBackgroundTaskInstance taskInstance) { Debug.WriteLine("Background Audio Task " + taskInstance.Task.Name + " starting"); this.systemMediaTransportControl = SystemMediaTransportControls.GetForCurrentView(); this.systemMediaTransportControl.IsEnabled = true; this.systemMediaTransportControl.IsPauseEnabled = true; this.systemMediaTransportControl.IsPlayEnabled = true; this.systemMediaTransportControl.IsNextEnabled = true; this.systemMediaTransportControl.IsPreviousEnabled = true; // Wire up system media control events this.systemMediaTransportControl.ButtonPressed += this.OnSystemMediaTransportControlButtonPressed; this.systemMediaTransportControl.PropertyChanged += this.OnSystemMediaTransportControlPropertyChanged; // Wire up background task events taskInstance.Canceled += this.OnTaskCanceled; taskInstance.Task.Completed += this.OnTaskcompleted; // Initialize message channel BackgroundMediaPlayer.MessageReceivedFromForeground += this.OnMessageReceivedFromForeground; // Notify foreground that we have started playing BackgroundMediaPlayer.SendMessageToForeground(new ValueSet() { { "BackgroundTaskStarted", "" } }); this.backgroundTaskStarted.Set(); this.backgroundTaskRunning = true; this.deferral = taskInstance.GetDeferral(); }
void SendMessage(string key, string value) { BackgroundMediaPlayer.SendMessageToForeground(new ValueSet { { key, value } }); }
private void SendIndex() { ValueSet message = new ValueSet(); message.Add(AppConstants.SongIndex, playlist.CurrentIndex.ToString()); BackgroundMediaPlayer.SendMessageToForeground(message); }
private void SendPosition() { ValueSet message = new ValueSet(); message.Add(AppConstants.Position, mediaPlayer.Position.ToString()); BackgroundMediaPlayer.SendMessageToForeground(message); }
private void FireOnSeek() { ValueSet value = new ValueSet(); value.Add(Constants.Seek, null); BackgroundMediaPlayer.SendMessageToForeground(value); }
private void SendBackgroundTaskIsStopping() { ValueSet message = new ValueSet(); message.Add(Constants.BackgroundTaskIsStopping, ""); BackgroundMediaPlayer.SendMessageToForeground(message); }
/// <summary> /// Fires when playlist changes to a new track /// </summary> /// <param name="sender"></param> /// <param name="args"></param> void playList_TrackChanged(MyPlaylist sender, object args) { string title; if (args is string) { title = args.ToString(); } else { title = Playlist.CurrentTrackName; } UpdateUVCOnNewTrack(title); ApplicationSettingsHelper.SaveSettingsValue(Constants.CurrentTrack, sender.CurrentTrackName); if (foregroundAppState == ForegroundAppStatus.Active) { //Message channel that can be used to send messages to foreground ValueSet message = new ValueSet(); message.Add(Constants.Trackchanged, title); BackgroundMediaPlayer.SendMessageToForeground(message); } }
/// <summary> /// 向前台发送媒体带参消息 /// </summary> /// <typeparam name="T">消息参数的类型</typeparam> /// <param name="type">媒体消息的类型</param> /// <param name="message">消息参数</param> public static void SendMediaMessageToForeground <T>(MediaMessageTypes type, T message) { var payload = new ValueSet(); payload.Add(MessageService.MessageType, type.ToString()); payload.Add(MessageService.MessageBody, serializeService.Serialize(message)); BackgroundMediaPlayer.SendMessageToForeground(payload); }
public static void SendMessageToForeground <T>(T message) { var payload = new ValueSet(); payload.Add(MessageService.MessageType, typeof(T).FullName); payload.Add(MessageService.MessageBody, JsonHelper.ToJson(message)); BackgroundMediaPlayer.SendMessageToForeground(payload); }
private void QueueManagerOnQueueUpdated(object sender, EventArgs eventArgs) { var message = new ValueSet { { PlayerConstants.QueueUpdated, "" } }; BackgroundMediaPlayer.SendMessageToForeground(message); }
/// <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..."); //SystemMediaTransportControls,是按下音量键后顶部弹出的控制音乐播放的控件。 //设置 SystemMediaTransportControls 的各个按键属性以及事件的订阅 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; // 关联取消 后台任务处理完成 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()); } //添加在媒体播放器状态发生更改时处理函数 BackgroundMediaPlayer.Current.CurrentStateChanged += Current_CurrentStateChanged; //Add handlers for playlist trackchanged //添加 后台播放列表变更时处理函数 Playlist.TrackChanged += playList_TrackChanged; Playlist.TrackComplete += playList_TrackComplete; //初始化消息通道 前台发往后台的信息 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); object value11 = ApplicationSettingsHelper.ReadResetSettingsValue(Constants.BackgroundTaskState); deferral = taskInstance.GetDeferral(); }
public static void SendMessageToForeground <T>(T message) { var payload = new ValueSet { { MessageBody, message.SerializeToJsonWithTypeInfo() } }; BackgroundMediaPlayer.SendMessageToForeground(payload); }
public static void SendMessageToClient <T>(T message) { ValueSet valueset = new ValueSet(); valueset.Add(MessageId, BackgroundMediaPlayerUserMessageKey); valueset.Add(MessageType, typeof(T).Name); valueset.Add(MessageContent, JsonHelper.ToJson(message)); BackgroundMediaPlayer.SendMessageToForeground(valueset); }
public void CompleteUpdate() { //SendIndex(); ValueSet message = new ValueSet(); message.Add(AppConstants.MediaOpened, BackgroundMediaPlayer.Current.NaturalDuration); BackgroundMediaPlayer.SendMessageToForeground(message); SendPosition(); }
public static void SendMessageToForeground <T>(object sender, T message, [CallerMemberName] string memberName = "") { var payload = new ValueSet(); payload.Add(MessageService.MessageType, typeof(T).FullName); payload.Add(MessageService.MessageBody, JsonConvert.SerializeObject(message)); Logger.LogInfo(sender, $"Sending {typeof(T).FullName} message to Foreground", memberName); BackgroundMediaPlayer.SendMessageToForeground(payload); }
/// <summary> /// Вызывается при изменении текущего трека в менеджере. /// </summary> private void Manager_TrackChanged(object sender, ManagerTrackChangedEventArgs e) { UpdateControlsOnNewTrack(e.Track); var valueSet = new ValueSet(); valueSet.Add(PLAYER_TRACK_ID, e.TrackID); BackgroundMediaPlayer.SendMessageToForeground(valueSet); UpdateTileOnNewTrack(e.Track); }
private void MediaPlayerOnMediaFailed(MediaPlayer sender, MediaPlayerFailedEventArgs args) { Debug.WriteLine("Failed to open the file with Background Media Player"); if (CurrentTrackItem != null) { BackgroundMediaPlayer.SendMessageToForeground(new ValueSet() { new KeyValuePair <string, object>(BackgroundAudioConstants.MFFailed, ((BackgroundTrackItem)CurrentTrackItem).Id) }); } }
private void QueueManager_ErrorHandler(object sender, UnauthorizedAccessException e) { if (ForegroundAppState == ForegroundAppStatus.Active) { //Message channel that can be used to send messages to foreground var message = new ValueSet { { PlayerConstants.Error, e.Message } }; BackgroundMediaPlayer.SendMessageToForeground(message); } }
private void playList_TrackChanged(Playlist sender, object args) { UpdateUVCOnNewTrack(); ApplicationSettingsHelper.SaveSettingsValue(SharedStrings.BACKGROUND_AUDIO_CURRENT_TRACK, sender.CurrentTrackName); if (foregroundAppState == ForegroundAppStatus.Active) { ValueSet message = new ValueSet(); message.Add(SharedStrings.BACKGROUND_AUDIO_TRACK_CHANGED, sender.CurrentTrackName); BackgroundMediaPlayer.SendMessageToForeground(message); } }
private BackgroundTaskDeferral deferral; // Used to keep task alive /// <summary> /// The Run method is the entry point of a background task. /// </summary> /// <param name="taskInstance"></param> public void Run(IBackgroundTaskInstance taskInstance) { taskInstance.Canceled += TaskInstance_Canceled; deferral = taskInstance.GetDeferral(); systemmediatransportcontrol = SystemMediaTransportControls.GetForCurrentView(); systemmediatransportcontrol.ButtonPressed += Systemmediatransportcontrol_ButtonPressed; systemmediatransportcontrol.IsEnabled = true; systemmediatransportcontrol.IsPauseEnabled = true; systemmediatransportcontrol.IsPlayEnabled = true; BackgroundMediaPlayer.MessageReceivedFromForeground += BackgroundMediaPlayer_MessageReceivedFromForeground; BackgroundMediaPlayer.SendMessageToForeground(new ValueSet()); }
/// <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(); }
private async void OnMessageReceivedFromForeground(object sender, MediaPlayerDataReceivedEventArgs e) { var data = e.Data; var episode = e.Data.ToObject <Episode>(); if (episode != null) { BackgroundMediaPlayer.SendMessageToForeground(episode.ToValueSet()); await PlayEpisodeAsync(episode); UpdateSystemControls(episode); } }
/// <summary> /// Fires when playlist changes to a new track /// </summary> /// <param name="sender"></param> /// <param name="args"></param> void playList_TrackChanged(object sender, object args) { UpdateUVCOnNewTrack(); UpdateLastFmOnNewTrack(); ApplicationSettingsHelper.SaveSettingsValue(BackgroundAudioConstants.CurrentTrack, Playlist.CurrentTrackIndex); if (foregroundAppState == ForegroundAppStatus.Active) { //Message channel that can be used to send messages to foreground ValueSet message = new ValueSet(); message.Add(BackgroundAudioConstants.Trackchanged, Playlist.CurrentTrack); BackgroundMediaPlayer.SendMessageToForeground(message); } }
public static void SendMessageToForeground <T>(T message) { try { var payload = new ValueSet(); payload.Add(MessageService.MessageType, typeof(T).FullName); payload.Add(MessageService.MessageBody, JsonHelper.ToJson(message)); BackgroundMediaPlayer.SendMessageToForeground(payload); } catch (Exception ex) { Debug.WriteLine($"$#~>{ex.Message}"); if (Debugger.IsAttached) { Debugger.Break(); } /*else throw;*/ } }
/// <summary> /// The Run method is the entry point of a background task. /// </summary> /// <param name="taskInstance"></param> public void Run(IBackgroundTaskInstance taskInstance) { _appSettingsHelper = new AppSettingsHelper(); Debug.WriteLine("Background Audio Task " + taskInstance.Task.Name + " starting..."); // InitializeAsync 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 = BackgroundMediaPlayer.Current.SystemMediaTransportControls; _systemmediatransportcontrol.ButtonPressed += systemmediatransportcontrol_ButtonPressed; // _systemmediatransportcontrol.PropertyChanged += systemmediatransportcontrol_PropertyChanged; _systemmediatransportcontrol.IsEnabled = true; _systemmediatransportcontrol.IsPauseEnabled = true; _systemmediatransportcontrol.IsPlayEnabled = true; _systemmediatransportcontrol.IsNextEnabled = true; _systemmediatransportcontrol.IsPreviousEnabled = true; //InitializeAsync message channel BackgroundMediaPlayer.MessageReceivedFromForeground += BackgroundMediaPlayer_MessageReceivedFromForeground; BackgroundMediaPlayer.Current.CurrentStateChanged += Current_CurrentStateChanged; //Send information to foreground that background task has been started if app is active if (ForegroundAppState == ForegroundAppStatus.Active) { var message = new ValueSet { { PlayerConstants.BackgroundTaskStarted, "" } }; BackgroundMediaPlayer.SendMessageToForeground(message); } _appSettingsHelper.Write(PlayerConstants.BackgroundTaskState, PlayerConstants.BackgroundTaskRunning); // This must be retrieved prior to subscribing to events below which use it _deferral = taskInstance.GetDeferral(); // Mark the background task as started to unblock SMTC Play operation (see related WaitOne on this signal) TaskStarted.Set(); taskInstance.Task.Completed += Taskcompleted; taskInstance.Canceled += OnCanceled; //Add handlers for MediaPlayer //Add handlers for playlist trackchanged QueueManager.TrackChanged += playList_TrackChanged; QueueManager.ErrorHandler += QueueManager_ErrorHandler; _backgroundtaskrunning = true; }
/// <summary> /// The Run method is the entry point of a background task. /// </summary> /// <param name="taskInstance"></param> public void Run(IBackgroundTaskInstance taskInstance) { _appSettingsHelper = new AppSettingsHelper(); Debug.WriteLine("Background Audio Task " + taskInstance.Task.Name + " starting..."); // InitializeAsync 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 += OnCanceled; taskInstance.Task.Completed += Taskcompleted; //Add handlers for MediaPlayer BackgroundMediaPlayer.Current.CurrentStateChanged += Current_CurrentStateChanged; //Add handlers for playlist trackchanged QueueManager.TrackChanged += playList_TrackChanged; QueueManager.MatchingTrack += QueueManagerOnMatchingTrack; QueueManager.QueueUpdated += QueueManagerOnQueueUpdated; QueueManager.FlushHistory(); //InitializeAsync message channel BackgroundMediaPlayer.MessageReceivedFromForeground += BackgroundMediaPlayer_MessageReceivedFromForeground; //Send information to foreground that background task has been started if app is active if (ForegroundAppState != ForegroundAppStatus.Suspended) { var message = new ValueSet { { PlayerConstants.BackgroundTaskStarted, "" } }; BackgroundMediaPlayer.SendMessageToForeground(message); } _backgroundTaskStarted.Set(); _backgroundtaskrunning = true; _appSettingsHelper.Write(PlayerConstants.BackgroundTaskState, PlayerConstants.BackgroundTaskRunning); _deferral = taskInstance.GetDeferral(); }
public void Run(IBackgroundTaskInstance taskInstance) { Debug.WriteLine("Background Audio Task " + taskInstance.Task.Name + " starting..."); deferral = taskInstance.GetDeferral(); // AttachMessageHandlers(); server = new MusicPlayerServer(); server.InitialServer(); ValueSet valueSet = new ValueSet(); valueSet["MessageId"] = MessageService.BackgroundMediaPlayerActivatedMessageKey; BackgroundMediaPlayer.SendMessageToForeground(valueSet); taskInstance.Task.Completed += TaskCompleted; taskInstance.Canceled += new BackgroundTaskCanceledEventHandler(OnCanceled); }
/// <summary> /// Method which sends given data to foreground. /// Lanuches only if app is in background /// </summary> /// <param name="messageData">Data object. It can be a string /// representing notification message for foreground or a /// MediaPlaybackItem representing an item which is currently /// played</param> private void SendMessageToForeground(MessageType messageType, object messageData) { if (!isPlayerInBackground) { try { //Add values to message ValueSet valuesToSend = new ValueSet(); valuesToSend.Add(messageType.ToString(), messageData); BackgroundMediaPlayer.SendMessageToForeground(valuesToSend); } catch (Exception ex) { Debug.WriteLine(DateTime.Now.ToString() + ": {0}", ex.Message); } } }
/// <summary> /// Fires when playlist changes to a new track /// </summary> /// <param name="sender"></param> /// <param name="args"></param> private void playList_TrackChanged(QueueManager sender, object args) { UpdateUvcOnNewTrack(); AppSettingsHelper.Write(PlayerConstants.CurrentTrack, sender.CurrentTrack.Id); if (_foregroundAppState != ForegroundAppStatus.Active) { return; } //Message channel that can be used to send messages to foreground var message = new ValueSet { { PlayerConstants.Trackchanged, sender.CurrentTrack.Id } }; BackgroundMediaPlayer.SendMessageToForeground(message); }