Exemplo n.º 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;
        }
Exemplo n.º 2
0
        private void Playlist_TrackChanged(BackgroundPlaylist sender, object args)
        {
            if (this._foregroundState != eForegroundState.Suspended)
            {
                var msg = new ValueSet();
                msg.Add(Command.Titte, Playlist.Name);
                BackgroundMediaPlayer.SendMessageToForeground(msg);     
            }

        }
Exemplo n.º 3
0
        public static async Task<BackgroundPlaylist> LoadBackgroundPlaylist(string path)
        {
            var file = await ApplicationData.Current.LocalCacheFolder.GetFileAsync(path);
            Stream stream = await file.OpenStreamForReadAsync();
            List<string> filepath = null;
            int currentindex= -1;

            using (XmlReader reader = XmlReader.Create(stream))
            {
                //reader.ReadStartElement("Playlist");
                reader.Read();
                while (reader.NodeType != XmlNodeType.EndElement || reader.Name != "Playlist")
                {
                    reader.Read();

                    if (reader.IsStartElement("CurrentIndex"))
                    {
                        reader.Read();
                        currentindex = Int32.Parse(reader.Value);
                        continue;
                    }
                    if (reader.IsStartElement("List"))
                    {
                        filepath = readFilelist(reader);
                    }
                }
            }
            stream.Dispose();
            BackgroundPlaylist bgPlaylist = new BackgroundPlaylist();
            bgPlaylist.ListPathsource = filepath;
            bgPlaylist.Currentindex = currentindex;
            return bgPlaylist;
        }