-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
198 lines (179 loc) · 7.73 KB
/
Program.cs
File metadata and controls
198 lines (179 loc) · 7.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
namespace BiliVoxLive;
using System;
using System.IO;
using System.Windows;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using LibVLCSharp.Shared;
public class Program
{
[STAThread]
public static void Main()
{
try
{
var app = new Application();
var currentDirectory = AppDomain.CurrentDomain.BaseDirectory;
var libvlcDirectory = Path.Combine(currentDirectory, "libvlc", "win-x64");
// 检查目录是否存在
if (!Directory.Exists(libvlcDirectory))
{
MessageBox.Show($"VLC库目录不存在: {libvlcDirectory}",
"错误", MessageBoxButton.OK, MessageBoxImage.Error);
return;
}
// 检查插件目录
var pluginsDir = Path.Combine(libvlcDirectory, "plugins");
if (!Directory.Exists(pluginsDir))
{
MessageBox.Show($"VLC插件目录不存在: {pluginsDir}",
"错误", MessageBoxButton.OK, MessageBoxImage.Error);
return;
}
// 检查必要的文件
var requiredFiles = new[] { "libvlc.dll", "libvlccore.dll" };
foreach (var file in requiredFiles)
{
var filePath = Path.Combine(libvlcDirectory, file);
if (!File.Exists(filePath))
{
MessageBox.Show($"缺少必要的文件: {filePath}",
"错误", MessageBoxButton.OK, MessageBoxImage.Error);
return;
}
}
// 必须先设置环境变量
Environment.SetEnvironmentVariable("PATH",
$"{libvlcDirectory};{Environment.GetEnvironmentVariable("PATH")}");
// 然后初始化 Core
try
{
Core.Initialize(libvlcDirectory);
}
catch (Exception ex)
{
MessageBox.Show($"VLC Core初始化失败: {ex.Message}",
"错误", MessageBoxButton.OK, MessageBoxImage.Error);
return;
}
// 创建服务
var services = new ServiceCollection();
var serviceProvider = services.BuildServiceProvider(); // 先创建 ServiceProvider
// 创建 LibVLC 实例
LibVLC? libVLC = null;
try
{
libVLC = new LibVLC(
enableDebugLogs: false,
"--quiet", // 完全禁用VLC日志输出
"--drop-late-frames",
"--skip-frames",
"--network-caching=3000", // 增加到3秒
"--live-caching=3000", // 增加到3秒
"--file-caching=3000", // 增加到3秒
"--clock-synchro=0",
"--sout-mux-caching=3000", // 增加到3秒
"--audio-time-stretch",
"--aout=mmdevice",
"--no-stats",
"--no-osd",
"--no-snapshot-preview",
"--no-metadata-network-access",
"--verbose=-1", // 设置为最低日志级别
"--no-file-logging", // 禁用文件日志
"--no-sub-autodetect-file",
"--codec=any", // 使用任何可用解码器
"--avcodec-hw=any", // 允许任何硬件解码
"--avcodec-threads=0", // 自动设置解码线程数
"--http-reconnect", // 启用HTTP重连
"--http-continuous", // 启用HTTP连续播放
"--sout-keep" // 保持输出连接
);
// 只记录真正的错误
var ignoredMessages = new[]
{
"buffer", "looking", "creating", "volume",
"decoded", "found", "trying", "stream", "audio output",
"format", "params", "frame", "using", "TLS", "data",
"conversion", "removing", "resolving", "simple", "state",
"playback", "setting", "version", "HTTP"
};
libVLC.Log += (s, e) =>
{
// 只记录严重错误
if (e.Level == LogLevel.Error)
{
// 忽略包含特定关键词的消息
if (!ignoredMessages.Any(msg => e.Message.Contains(msg, StringComparison.OrdinalIgnoreCase)))
{
var logService = serviceProvider.GetService<ILogService>();
logService?.Error($"VLC严重错误: {e.Message}");
}
}
};
}
catch (Exception ex)
{
MessageBox.Show($"VLC实例创建失败: {ex.Message}",
"错误", MessageBoxButton.OK, MessageBoxImage.Error);
return;
}
// 配置服务
services.AddSingleton<LogService>();
services.AddSingleton<ILogService>(sp => sp.GetRequiredService<LogService>());
services.AddSingleton<CookieService>();
services.AddSingleton<ICookieService>(sp => sp.GetRequiredService<CookieService>());
services.AddSingleton(libVLC);
services.AddSingleton<BiliApiService>();
services.AddSingleton<LiveStreamService>();
services.AddSingleton<DanmakuService>();
services.AddTransient<MainWindow>();
// 重新构建 ServiceProvider,包含所有注册的服务
serviceProvider = services.BuildServiceProvider();
// 创建主窗口
var mainWindow = serviceProvider.GetRequiredService<MainWindow>();
app.MainWindow = mainWindow;
// 改进全局异常处理
app.DispatcherUnhandledException += (s, e) =>
{
var logService = serviceProvider.GetRequiredService<ILogService>();
logService.Error($"未处理的异常: {e.Exception.Message}", e.Exception);
MessageBox.Show(
$"发生错误: {e.Exception.Message}\n\n请检查日志了解详情。",
"错误",
MessageBoxButton.OK,
MessageBoxImage.Error
);
e.Handled = true;
};
// 应用程序退出处理
AppDomain.CurrentDomain.UnhandledException += (s, e) =>
{
var logService = serviceProvider.GetRequiredService<ILogService>();
logService.Error($"未处理的域异常: {e.ExceptionObject}", e.ExceptionObject as Exception);
};
app.Exit += async (s, e) =>
{
try
{
var liveService = serviceProvider.GetRequiredService<LiveStreamService>();
await liveService.StopAsync();
libVLC.Dispose();
}
catch (Exception ex)
{
MessageBox.Show($"清理资源时出错: {ex.Message}", "警告",
MessageBoxButton.OK, MessageBoxImage.Warning);
}
};
// 运行应用程序
mainWindow.Show();
app.Run();
}
catch (Exception ex)
{
MessageBox.Show($"启动失败: {ex.Message}\n\n{ex.StackTrace}",
"错误", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
}