我正在制作一个项目,可以提取YouTube视频的音频并将其保存到您的计算机上。 为此,我使用了一个名为YouTubeExtractor的GitHub库。
我正在使用backgroundworker,以便在下载文件时使UI可用。这是我到目前为止的代码。
public partial class MainWindow : Window
{
private readonly BackgroundWorker worker = new BackgroundWorker();
public MainWindow()
{
InitializeComponent();
worker.DoWork += worker_DoWork;
worker.WorkerReportsProgress = true;
worker.WorkerSupportsCancellation = true;
}
private void downloadButton_Click(object sender, RoutedEventArgs e)
{
worker.RunWorkerAsync();
}
string link;
double percentage;
private void worker_DoWork(object sender, DoWorkEventArgs e)
{
this.Dispatcher.Invoke((Action)(() =>
{
link = videoURL.Text;
}));
/*
* Get the available video formats.
* We'll work with them in the video and audio download examples.
*/
IEnumerable<VideoInfo> videoInfos = DownloadUrlResolver.GetDownloadUrls(link);
/*
* We want the first extractable video with the highest audio quality.
*/
VideoInfo video = videoInfos
.Where(info => info.CanExtractAudio)
.OrderByDescending(info => info.AudioBitrate)
.First();
/*
* If the video has a decrypted signature, decipher it
*/
if (video.RequiresDecryption)
{
DownloadUrlResolver.DecryptDownloadUrl(video);
}
/*
* Create the audio downloader.
* The first argument is the video where the audio should be extracted from.
* The second argument is the path to save the audio file.
*/
var audioDownloader = new AudioDownloader(video, System.IO.Path.Combine("C:/Downloads", video.Title + video.AudioExtension));
// Register the progress events. We treat the download progress as 85% of the progress and the extraction progress only as 15% of the progress,
// because the download will take much longer than the audio extraction.
audioDownloader.DownloadProgressChanged += (send, args) => Console.WriteLine(args.ProgressPercentage * 0.85);
audioDownloader.AudioExtractionProgressChanged += (send, args) => Console.WriteLine(85 + args.ProgressPercentage * 0.15);
/*
* Execute the audio downloader.
* For GUI applications note, that this method runs synchronously.
*/
audioDownloader.Execute();
}
}
}
我遇到的问题是我想显示这个
audioDownloader.DownloadProgressChanged += (send, args) => Console.WriteLine(args.ProgressPercentage * 0.85);
audioDownloader.AudioExtractionProgressChanged += (send, args) => Console.WriteLine(85 + args.ProgressPercentage * 0.15);
在UI元素中,例如标签或进度条而不是Console.WriteLine
每当我label1.Text = (85 + args.ProgressPercentage * 0.15);
它会抛出一个像
“调用线程无法访问此对象,因为另一个线程拥有它。”
我知道你可以用代表来解决这个问题,我需要明确说明如何解决这个问题。
谢谢。
答案 0 :(得分:1)
以下是使用任务和async
/ await
关键字
另外使用Dispatcher.BeginInvoke
更新您的用户界面。
代码:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using YoutubeExtractor;
namespace WpfApplication1
{
public partial class MainWindow
{
public MainWindow() {
InitializeComponent();
}
private async void Button_Click(object sender, RoutedEventArgs e) {
string videoUrl = @"https://www.youtube.com/watch?v=5aXsrYI3S6g";
await DownloadVideoAsync(videoUrl);
}
private Task DownloadVideoAsync(string url) {
return Task.Run(() => {
IEnumerable<VideoInfo> videoInfos = DownloadUrlResolver.GetDownloadUrls(url);
VideoInfo videoInfo = videoInfos.FirstOrDefault();
if (videoInfo != null) {
if (videoInfo.RequiresDecryption) {
DownloadUrlResolver.DecryptDownloadUrl(videoInfo);
}
string savePath =
Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.Desktop),
Path.ChangeExtension("myVideo", videoInfo.VideoExtension));
var downloader = new VideoDownloader(videoInfo, savePath);
downloader.DownloadProgressChanged += downloader_DownloadProgressChanged;
downloader.Execute();
}
});
}
private void downloader_DownloadProgressChanged(object sender, ProgressEventArgs e) {
Dispatcher.BeginInvoke((Action) (() => {
double progressPercentage = e.ProgressPercentage;
ProgressBar1.Value = progressPercentage;
TextBox1.Text = string.Format("{0:F} %", progressPercentage);
}));
}
}
}
XAML:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow"
Width="525"
Height="350">
<Grid>
<StackPanel>
<Button Click="Button_Click" Content="Download" />
<ProgressBar x:Name="ProgressBar1"
Height="20"
Maximum="100" />
<TextBox x:Name="TextBox1" />
</StackPanel>
</Grid>
</Window>