示例#1
0
        /// <summary>
        /// Hệ thống gọi đến hàm này khi association backgroundtask được bật
        /// </summary>
        /// <param name="taskInstance"> hệ thống tự tạo và truyền vào đây</param>
        public async void Run(IBackgroundTaskInstance taskInstance)
        {
            //System.Diagnostics.Debug.WriteLine("background run");
            taskInstance.Canceled += new BackgroundTaskCanceledEventHandler(BackgroundTaskCanceled);
            taskInstance.Task.Completed += new BackgroundTaskCompletedEventHandler(BackgroundTaskCompleted);
            _backgroundstarted.Set();//
            _deferral = taskInstance.GetDeferral();

            //Playlist = new BackgroundPlaylist();
            _smtc = initSMTC();


            this._foregroundState = this.initForegroundState();

            BackgroundMediaPlayer.Current.CurrentStateChanged += BackgroundMediaPlayer_CurrentStateChanged;
            //Playlist = await BackgroundPlaylist.LoadBackgroundPlaylist("playlist.xml");
            Playlist = new BackgroundPlaylist();
            Playlist.ListPathsource = await BackgroundPlaylist.LoadCurrentPlaylist(Constant.CurrentPlaylist);
            
            if (_foregroundState != eForegroundState.Suspended)
            {
                ValueSet message = new ValueSet();
                message.Add(Constant.BackgroundTaskStarted, "");
                BackgroundMediaPlayer.SendMessageToForeground(message);
            }  
            Playlist.TrackChanged += Playlist_TrackChanged;
            BackgroundMediaPlayer.MessageReceivedFromForeground += BackgroundMediaPlayer_MessageReceivedFromForeground;
            BackgroundMediaPlayer.Current.MediaEnded +=Current_MediaEnded;
            ApplicationSettingHelper.SaveSettingsValue(Constant.BackgroundTaskState, Constant.BackgroundTaskRunning);
            isbackgroundtaskrunning = true;
            _loopState = eLoopState.None;
        }
示例#2
0
        /// <summary>
        /// Hệ thống gọi đến hàm này khi association backgroundtask được bật
        /// </summary>
        /// <param name="taskInstance"> hệ thống tự tạo và truyền vào đây</param>
        public void Run(IBackgroundTaskInstance taskInstance)
        {
            System.Diagnostics.Debug.WriteLine("bg RUN");
            _smtc = initSMTC();
            taskInstance.Canceled += BackgroundTaskCanceled;
            taskInstance.Task.Completed += BackgroundTaskCompleted;

            this._foregroundState = this.initForegroundState();

            // Playlist.TrackedChanged += Playlist_TrackChanged;
            BackgroundMediaPlayer.MessageReceivedFromForeground += BackgroundMediaPlayer_MessageReceivedFromForeground;
            BackgroundMediaPlayer.Current.CurrentStateChanged +=BackgroundMediaPlayer_CurrentStateChanged;
            _backgroundstarted.Set();
            ApplicationSettingHelper.SaveSettingsValue(Constant.BackgroundTaskStarted, Constant.BackgroundTaskRunning);
            isbackgroundtaskrunning = true;
            _deferral = taskInstance.GetDeferral();
        }
示例#3
0
 private void BackgroundMediaPlayer_MessageReceivedFromForeground(object sender, MediaPlayerDataReceivedEventArgs e)
 {
     System.Diagnostics.Debug.WriteLine("message received");
     foreach (string key in e.Data.Keys)
     {
         switch (key)
         {
             case Constant.AppResumed:
                 this._foregroundState = eForegroundState.Active;
                 break;
             case Constant.AppSuspended:
                 this._foregroundState = eForegroundState.Suspended;
                 break;
             case Command.InitList:
                 System.Diagnostics.Debug.WriteLine("message received Init list");
                 //Playlist.ListPathsource.Add(e.Data[Command.InitList])
                 //var listtrack = (string[])e.Data[Command.InitList];
                 //Playlist.ListPathsource = listtrack;
                 break;
             case Command.SetCurrentIndex:
                 var currentindex = Convert.ToInt32(e.Data[Command.SetCurrentIndex]);
                 Playlist.Currentindex = currentindex;
                 break;
             case Command.PlayWithIndex:
                 //Playlist.Play();
                 Playlist.Currentindex = Int32.Parse(e.Data[Command.PlayWithIndex].ToString());
                 System.Diagnostics.Debug.WriteLine(Playlist.Currentindex);
                 BackgroundMediaPlayer.Current.SetUriSource(new Uri(Playlist.CurrentItem));
                 updatenewstmc();
                 break;
             case Command.Pause:
                 BackgroundMediaPlayer.Current.Pause();
                 break;
             case Command.Play:
                 BackgroundMediaPlayer.Current.Play();
                 break;
             case Command.Shuffle:
                 Playlist.Shuffle();
                 break;
             case Command.Next:
                 Playlist.Next();
                 BackgroundMediaPlayer.Current.SetUriSource(new Uri(Playlist.CurrentItem));
                 _smtc.DisplayUpdater.MusicProperties.Title = Playlist.Name;
                 _smtc.DisplayUpdater.Update();
                 break;
             case Command.Previous:
                 Playlist.Previous();
                 BackgroundMediaPlayer.Current.SetUriSource(new Uri(Playlist.CurrentItem));
                 _smtc.DisplayUpdater.MusicProperties.Title = Playlist.Name;
                 _smtc.DisplayUpdater.Update();
                 break;
             case Command.LoopState:
                 this._loopState = (eLoopState)Enum.Parse(typeof(eLoopState), e.Data[Command.LoopState].ToString());
                 break;
         }
     }
 }