示例#1
0
 private void Downloader_OnDownloading(long size, long downloadedSize, long speed)
 {
     Dispatcher.BeginInvoke(new Action(() =>
     {
         DlProgress.Value    = downloadedSize;
         LblSpeed.Content    = SharedUtils.CountSize(speed) + "/s";
         LblProgress.Content = $"{Math.Round(downloadedSize / (float)size * 100)} %";
     }));
 }
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is long l)
            {
                return(SharedUtils.CountSize(l));
            }

            return(value);
        }
示例#3
0
        private static async Task <CachedSound> GetOrCreateCacheSound(string path, bool isDefault)
        {
            string newPath = path;

            if (!File.Exists(newPath))
            {
                newPath = TryGetPath(newPath);
            }

            if (!File.Exists(newPath))
            {
                newPath = TryGetPath(Path.Combine(Path.GetDirectoryName(newPath), Path.GetFileNameWithoutExtension(newPath)));
            }

            if (!File.Exists(newPath))
            {
                if (!isDefault)
                {
                    CachedDictionary.TryAdd(path, null);
                }
                else
                {
                    DefaultDictionary.TryAdd(path, null);
                }
                return(null);
            }

            if (!isDefault && CachedDictionary.TryGetValue(path, out var value) ||
                isDefault && DefaultDictionary.TryGetValue(path, out value))
            {
                return(value);
            }

            CachedSound cachedSound;

            try
            {
                cachedSound = await CreateFromFile(newPath).ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                Logger.Error(ex, "Error while creating cached sound: {0}", path);
                if (!isDefault)
                {
                    CachedDictionary.TryAdd(path, null);
                }
                else
                {
                    DefaultDictionary.TryAdd(path, null);
                }
                return(null);
            }

            // Cache each file once before play.
            var sound = isDefault
                ? DefaultDictionary.GetOrAdd(path, cachedSound)
                : CachedDictionary.GetOrAdd(path, cachedSound);

            Logger.Debug("Total size of cache usage: {0}", SharedUtils.CountSize(
                             CachedDictionary.Values.Sum(k => k?.AudioData?.Length * sizeof(float) ?? 0) +
                             DefaultDictionary.Values.Sum(k => k?.AudioData?.Length * sizeof(float) ?? 0)));

            return(sound);
        }