使用Vlc.DotNet与LibVLCSharp播放视频时仅出声音却显示黑屏问题排查
WinForms双LibVLC视频播放器实现方案
我搭建了一个简单的WinForms解决方案,核心是集成两个基于LibVLC的视频播放器,同时搭配了一个按钮来触发二者同步播放视频,具体细节如下:
核心组件说明
- 采用Vlc.DotNet和LibVLCSharp两个LibVLC封装库实现播放器功能
- 开发过程均严格参照对应库的官方示例进行实现
- 项目附带两个日志文件:
Loglibvlcsharp.txt和LogVlcDotNet,用于排查播放相关问题
完整实现代码
public partial class QueueDisplayVideoView : Form { private string[] paths, files; private int videoIndex = 0; public LibVLC _libVLC; public MediaPlayer _mp; public LibVLCSharp.Shared.Media media; public QueueDisplayVideoView() { InitializeComponent(); Core.Initialize(); _libVLC = new LibVLC(enableDebugLogs:true); _libVLC.SetLogFile(@"C:\Temp\LogLib.txt"); _mp = new MediaPlayer(_libVLC); videoView.MediaPlayer = _mp; } protected override void OnLoad(EventArgs e) { _presenter.OnViewReady(); base.OnLoad(e); LoadVideoFiles(); SetPlayerAspectRatio(); //StartAutoPlay(); } private void StartAutoPlay() { vlcControl.Play(new Uri(paths[videoIndex])); _mp.Play(new LibVLCSharp.Shared.Media(_libVLC, new Uri(paths[videoIndex]))); } private void LoadVideoFiles() { var path = some path paths = Directory.GetFiles(path, "*", SearchOption.AllDirectories); files = new string[paths.Length]; for (int i = 0; i < paths.Length; i++) { files[i] = Path.GetFileName(paths[i]); } } private void SetPlayerAspectRatio() { var videoWidth = vlcControl.Width.ToString(); var videoHieght = vlcControl.Height.ToString(); vlcControl.Video.AspectRatio = videoWidth + ":" + videoHieght; } private void vlcControl_VlcLibDirectoryNeeded(object sender, Vlc.DotNet.Forms.VlcLibDirectoryNeededEventArgs e) { // var currentAssembly = System.Reflection.Assembly.GetEntryAssembly(); var currentDirectory = @"Q:\bin"; //new FileInfo(currentAssembly.Location).DirectoryName; // Default installation path of VideoLAN.LibVLC.Windows var fullPath = Path.Combine(currentDirectory, "libvlc", IntPtr.Size == 4 ? "win-x86" : "win-x64"); e.VlcLibDirectory = new DirectoryInfo(fullPath); } private void vlcControl_EndReached(object sender, Vlc.DotNet.Core.VlcMediaPlayerEndReachedEventArgs e) { GetNextVideoIndex(); ThreadPool.QueueUserWorkItem(_ => vlcControl.Play(new Uri(paths[videoIndex]))); } private void button1_Click(object sender, EventArgs e) { StartAutoPlay(); } private void GetNextVideoIndex() { if (videoIndex < files.Length - 1) { videoIndex = videoIndex + 1; } else { videoIndex = 0; } } }
注意:代码中
var path = some path需要替换为实际的视频文件目录路径
内容的提问来源于stack exchange,提问作者styx
相关产品推荐
相关产品推荐

