playlist - LibVLCSharp 播放列表实现
问题描述
我正在尝试将 LibVLCSharp 与播放列表(或计划)一起使用,并在两个不同视频之间使用基本循环对其进行测试。我在 WPF 中使用它。UI 有一个按钮来开始播放第一个视频。如果我不循环并每次都单击按钮,则下一个视频将按预期播放。让它循环并在第二个视频上发生线程错误。我已经在 SO 上查看了其他一些帖子 -如何使用 Libvlcsharp和其中的各种链接实现循环播放,但我遗漏了一些东西。我希望有人有一个建议 - 或两个!谢谢。
XAML
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:wpf="clr-namespace:LibVLCSharp.WPF;assembly=LibVLCSharp.WPF"
xmlns:local="clr-namespace:testVLC"
xmlns:Properties="clr-namespace:testVLC.Properties" x:Class="testVLC.MainWindow"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width=".2*"/>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width=".2*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height=".25*"/>
<RowDefinition Height="2*"/>
<RowDefinition Height="1*"/>
<RowDefinition Height=".25*"/>
<RowDefinition Height=".25*"/>
</Grid.RowDefinitions>
<wpf:VideoView x:Name="VLC_player" Grid.Column="1" Grid.ColumnSpan="4" Grid.Row="0" Grid.RowSpan="3" Background="Black" Loaded="VLC_player_Loaded"/>
<Button x:Name="go_Btn" Content="Press to view video" Grid.Column="2" HorizontalAlignment="Right" Grid.Row="3" Padding="4,1" Margin="0,0,1,0" Click="Button_click"/>
<TextBox x:Name="countTBox" Grid.Column="2" HorizontalAlignment="Left" Grid.Row="3" Width="30" VerticalContentAlignment="Stretch"/>
</Grid>
</Window>
CS
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using LibVLCSharp.Shared;
using MediaPlayer = LibVLCSharp.Shared.MediaPlayer;
namespace testVLC
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
LibVLC _libVLC;
MediaPlayer media_player;
bool init_flag = false;
bool Web_flag = false;
int count = 0;
public MainWindow()
{
InitializeComponent();
}
private async void Media_play (string URI)
{
if (init_flag)
{
if(Web_flag)
{
_libVLC = new LibVLC("--verbose=2");
Media media = new Media(_libVLC, (URI), FromType.FromLocation);
await media.Parse(MediaParseOptions.ParseNetwork);
media_player = new MediaPlayer(media.SubItems.First());
VLC_player.MediaPlayer = media_player;
media_player.EndReached += Video_Ended;
media_player.Play();
//MessageBox.Show(Thread.CurrentThread.Name);
}
else
{
_libVLC = new LibVLC();
media_player = new MediaPlayer(_libVLC);
VLC_player.MediaPlayer = media_player;
media_player.EndReached += Video_Ended;
media_player.Play(new Media(_libVLC, URI));
}
}
}
private void Button_click(object sender, RoutedEventArgs e)
{
count++;
this.Dispatcher.Invoke((Action)(() =>
{
countTBox.Text = count.ToString();
}));
Main_List();
}
private async void Video_Ended(object sender, EventArgs e)
{
//ThreadPool.QueueUserWorkItem(_ => media_player.Stop());//doesn't work
await Task.Run(() => media_player.Stop());
Web_flag = false;
//Button_click(this, null);//Works fine when commented out and the "Press to view video" button is clicked each time. Not so much when looping.
}
private void Main_List()
{
if (count%2 == 0)//even
{
Media_play("C:\\Users\\echo_\\Downloads\\videoplayback (1).mp4");
}
else//odd
{
Web_flag = true;
Media_play("https://youtu.be/UK4t59mhIhs");
}
}
private void VLC_player_Loaded(object sender, RoutedEventArgs e)
{
init_flag = true;
Core.Initialize();
}
}
}
解决方案
不要尝试在 VideoEnded 事件中调用 Stop,我认为 libvlc 3 中该区域的一个已知问题会导致程序挂起。调用 .Play 来代替新媒体。
另外,如果可以避免的话,我不建议使用两个不同的 libvlc 实例。1 个 LibVlc 和 1 个媒体播放器,但同一个 MediaPlayer 上的多个 Play() 将是常态。如果您需要不同的选项,您可以将不同的选项作为媒体选项传递(尽管某些选项在媒体级别不可用,例如详细程度选项(在这种情况下,我会在那里注册一个日志回调并过滤))
推荐阅读
- javascript - 无法使用 Selenium 和 java/javascript 在字段中输入数据
- nativescript - 如何在使用 nativescript-Angular 键入和更改 Android 设备的光标指针颜色时删除输入文本中的下划线
- oracle - 我想调用这个过程并传递在游标中获取的变量
- node.js - 复制“Concourse CI”节点调用/调试 process.stdin Net.socket
- c# - DbContext.AttachRange() 在这种情况下如何工作
- python - 通过 Anaconda 安装 libm.so.6
- javascript - 如何通过nodejs(通过heroku)下载文件并将它们存储到s3?
- android - 当我使用自定义 TextView 对齐文本时如何修复 Cutoff 大文本?
- python - 为什么我的 Google Maps API 请求的填充速度如此缓慢?
- javascript - 在一加 7 Pro [React-Native] 中截屏时,Android 应用程序崩溃