예제 #1
0
        public override async Task OnEntry(Episode owner, IState <Episode, EpisodeEvent> fromState, IEventProcessor <Episode, EpisodeEvent> stateMachine)
        {
            owner.NotifyPropertyChanged(() => owner.State);
            Progress <IDownloader> progress = new Progress <IDownloader>((downloader) =>
            {
                ulong totalBytesToReceive = downloader.GetTotalBytes();
                double at = 0;
                if (totalBytesToReceive > 0)
                {
                    at = (double)downloader.GetBytesDownloaded() / totalBytesToReceive;
                }
                owner.DownloadProgress = at;
            });

            try
            {
                IDownloader downloader = owner.m_DownloadService.CreateDownloader(owner.Uri, await owner.GetStorageFolder(), owner.FolderAndFileName, progress);
                StorageFile localFile  = await downloader.Download();

                // set duration
                MusicProperties musicProperties = await localFile.Properties.GetMusicPropertiesAsync();

                owner.Duration = musicProperties.Duration;
                // set position
                owner.Position = TimeSpan.FromMilliseconds(0);

                TouchedFiles.Instance.Add(localFile.Path);
                TouchedFiles.Instance.Add(Path.GetDirectoryName(localFile.Path));
                Task t = stateMachine.PostEvent(EpisodeEvent.DownloadSuccess);
            }
            catch (Exception e)
            {
                Tracer.TraceWarning("EpisodeStateDownloading.OnEntry(): error downloading {0}. {1}", owner.Uri, e);
            }
            Task task = stateMachine.PostEvent(EpisodeEvent.DownloadFail);
        }