コード例 #1
0
        public async Task ImplicitLoadMetadata()
        {
            var metadata = new MusicMetadata(TimeSpan.Zero, 0);
            var musicFile = new MusicFile(async fileNme => 
            {
                await Task.Yield();
                return metadata;
            }, "testfile.mp3");

            Assert.AreEqual("testfile.mp3", musicFile.FileName);
            Assert.IsFalse(musicFile.SharedMusicFiles.Any());
            Assert.IsFalse(musicFile.IsMetadataLoaded);

            Assert.IsNull(musicFile.Metadata);
            await Task.Delay(5);
            Assert.AreEqual(metadata, musicFile.Metadata);
            Assert.AreEqual(metadata, musicFile.GetMetadataAsync().Result);
            Assert.IsTrue(musicFile.IsMetadataLoaded);
        }
コード例 #2
0
        private async void TranscodeAsync(MusicFile musicFile)
        {
            var destinationFileName = GetDestinationFileName(musicFile.FileName);
            var transcodeItem = new TranscodeItem(musicFile, destinationFileName);
            transcodingManager.AddTranscodeItem(transcodeItem);
            Logger.Verbose("Start Transcode: {0} > {1}", musicFile.FileName, destinationFileName);

            var cts = new CancellationTokenSource();
            cancellationTokenSources.Add(transcodeItem, cts);
            UpdateCancelCommands();
            var metadata = await musicFile.GetMetadataAsync();
            uint bitrate = GetConvertBitrate(metadata.Bitrate);

            try
            {
                await TranscodeAsyncCore(transcodeItem, bitrate, cts.Token);
            }
            catch (OperationCanceledException)
            {
                transcodingManager.RemoveTranscodeItem(transcodeItem);
            }
            catch (Exception ex)
            {
                Logger.Error("TranscodeAsync exception: {0}", ex);
                transcodeItem.Error = ex;
            }
            finally
            {
                cancellationTokenSources.Remove(transcodeItem);
                if (allTranscodingsCanceledCompletion != null && !cancellationTokenSources.Any())
                {
                    allTranscodingsCanceledCompletion.SetResult(null);
                }
                UpdateCancelCommands();
                Logger.Verbose("End Transcode: {0} > {1}", musicFile.FileName, destinationFileName);
            }
        }
コード例 #3
0
        public void TotalDurationWithMetadataLoading()
        {
            var manager = new PlaylistManager();
            Assert.AreEqual(TimeSpan.Zero, manager.TotalDuration);
            var firstFile = new MockMusicFile(new MusicMetadata(TimeSpan.FromSeconds(10), 0), "");
            manager.AddAndReplaceItems(new[] { new PlaylistItem(firstFile) });
            
            var secondMetadata = new MusicMetadata(TimeSpan.FromSeconds(20), 0);
            var secondFile = new MusicFile(async x =>
            {
                await Task.Delay(10);
                return secondMetadata;
            }, "");

            manager.AddItems(new[] { new PlaylistItem(secondFile) });
            Assert.IsTrue(manager.IsTotalDurationEstimated);
            Assert.AreEqual(TimeSpan.FromSeconds(20), manager.TotalDuration);
            AssertHelper.PropertyChangedEvent(manager, x => x.TotalDuration, () => secondFile.GetMetadataAsync().GetResult());
            Assert.IsFalse(manager.IsTotalDurationEstimated);
            Assert.AreEqual(TimeSpan.FromSeconds(30), manager.TotalDuration);

            var thirdFile = new MockMusicFile(new MusicMetadata(TimeSpan.FromSeconds(30), 0), "");
            AssertHelper.PropertyChangedEvent(manager, x => x.TotalDuration, () => manager.AddItems(new[] { new PlaylistItem(thirdFile) }));
            Assert.AreEqual(TimeSpan.FromSeconds(60), manager.TotalDuration);
        }