-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMpvPlayer.Events.cs
278 lines (234 loc) · 9.53 KB
/
MpvPlayer.Events.cs
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
using ButtonBase = System.Windows.Controls.Primitives.ButtonBase;
using static CustomToolbox.Common.Sets.EnumSet;
using CustomToolbox.Common.Extensions;
using CustomToolbox.Common.Sets;
using MouseEventArgs = System.Windows.Forms.MouseEventArgs;
using Serilog.Events;
using System.Windows;
using Mpv.NET.API;
using Mpv.NET.Player;
namespace CustomToolbox;
/// <summary>
/// MpvPlayer 的相關事件
/// </summary>
public partial class WMain
{
public void PlayerHost_MouseDoubleClick(object? sender, MouseEventArgs? e)
{
if (WPPPlayer != null && WPPPlayer.IsShowing())
{
WPPPlayer.Close();
WPPPlayer = null;
// 雙擊放大到全螢幕。
/*
WMain_KeyDown(sender, new KeyEventArgs(
Keyboard.PrimaryDevice,
Keyboard.PrimaryDevice.ActiveSource,
0,
Key.R));
*/
}
else
{
WPPPlayer = new WPopupPlayer(this);
WPPPlayer.Show();
}
}
private void MediaLoaded(object? sender, EventArgs e)
{
try
{
Dispatcher.BeginInvoke(new Action(() =>
{
WriteLog(message: MsgSet.MsgMediaLoaded);
if (MPPlayer != null && CPPlayer.ClipData != null)
{
if (MPPlayer.IsMediaLoaded)
{
TimeSpan startTime = CPPlayer.ClipData.StartTime,
endTime = CPPlayer.ClipData.EndTime;
if (endTime == TimeSpan.FromSeconds(0))
{
endTime = MPPlayer.Duration.StripMilliseconds();
CPPlayer.ClipData.EndTime = endTime;
if (string.IsNullOrEmpty(CPPlayer.ClipData.Name) &&
!string.IsNullOrEmpty(MPPlayer.MediaTitle))
{
CPPlayer.ClipData.Name = MPPlayer.MediaTitle;
}
int maxVal = Convert.ToInt32(endTime.TotalSeconds);
PBDuration.Maximum = maxVal;
PBDuration.Minimum = 0;
SSeek.Maximum = maxVal;
SSeek.Minimum = 0;
}
MPPlayer.SeekAsync(startTime.TotalSeconds);
}
}
}));
}
catch (Exception ex)
{
WriteLog(
message: MsgSet.GetFmtStr(
MsgSet.MsgErrorOccured,
ex.GetExceptionMessage()),
logEventLevel: LogEventLevel.Error);
}
}
private void MediaFinished(object? sender, EventArgs e)
{
WriteLog(message: MsgSet.MsgMediaFinished);
BtnNext_Click(
nameof(MediaFinished),
new RoutedEventArgs(ButtonBase.ClickEvent));
}
private void MediaPaused(object? sender, EventArgs e)
{
WriteLog(message: MsgSet.MsgMediaPaused);
}
private void MediaResumed(object? sender, EventArgs e)
{
WriteLog(message: MsgSet.MsgMediaResumed);
}
private void MediaStartedBuffering(object? sender, EventArgs e)
{
WriteLog(message: MsgSet.MsgMediaStartedBuffering);
}
private void MediaEndedBuffering(object? sender, EventArgs e)
{
WriteLog(message: MsgSet.MsgMediaEndedBuffering);
}
private void MediaStartedSeeking(object? sender, EventArgs e)
{
WriteLog(message: MsgSet.MsgMediaStartedSeeking);
}
private void MediaEndedSeeking(object? sender, EventArgs e)
{
WriteLog(message: MsgSet.MsgMediaEndedSeeking);
}
private void MediaError(object? sender, EventArgs e)
{
WriteLog(
message: MsgSet.MsgMediaError,
logEventLevel: LogEventLevel.Error);
BtnNext_Click(
nameof(MediaError),
new RoutedEventArgs(ButtonBase.ClickEvent));
}
private void MediaUnloaded(object? sender, EventArgs e)
{
WriteLog(message: MsgSet.MsgMediaUnloaded);
}
private void PositionChanged(object? sender, MpvPlayerPositionChangedEventArgs e)
{
try
{
Dispatcher.BeginInvoke(new Action(() =>
{
if (CPPlayer.ClipData != null)
{
TimeSpan startTime = CPPlayer.ClipData.StartTime,
endTime = CPPlayer.ClipData.EndTime,
durationTime = CPPlayer.ClipData.DurationTime,
currentTime = e.NewPosition.Subtract(startTime);
int currentSeconds = Convert.ToInt32(e.NewPosition.TotalSeconds),
endSeconds = Convert.ToInt32(endTime.TotalSeconds),
// 僅用於時間標記編輯器模式。
durationSeconds = Convert.ToInt32(endTime.TotalSeconds);
// 判斷是否為時間標記編輯器模式。
if (CPPlayer.Mode == ClipPlayerMode.TimestampEditor)
{
// 先判斷 MPPlayer 是否已初始化。
if (MPPlayer != null)
{
// 用於避免 libmpv 取不到時間而發生例外。
if (currentSeconds >= durationSeconds)
{
// 先主動讓 mpv 停止播放短片,以免此事件被重複觸發。
MPPlayer.Stop();
BtnStop_Click(nameof(PositionChanged),
new RoutedEventArgs(ButtonBase.ClickEvent));
return;
}
currentTime = e.NewPosition;
durationTime = MPPlayer.Duration.StripMilliseconds();
int maxVal = Convert.ToInt32(durationTime.TotalSeconds);
PBDuration.Maximum = maxVal;
PBDuration.Minimum = 0;
SSeek.Maximum = maxVal;
SSeek.Minimum = 0;
}
}
else if (CPPlayer.Mode == ClipPlayerMode.ClipPlayer)
{
// 回歸原本的設計。
int maxVal = endSeconds;
PBDuration.Maximum = maxVal;
PBDuration.Minimum = 0;
SSeek.Maximum = maxVal;
SSeek.Minimum = 0;
}
if (currentSeconds >= PBDuration.Minimum &&
currentSeconds <= PBDuration.Maximum)
{
PBDuration.Value = currentSeconds;
}
// 當 SSeek 的 SeekStatus 為 SSeekStatus.Idle 時才允許更新。
if (CPPlayer.SeekStatus == SSeekStatus.Idle)
{
if (currentSeconds >= SSeek.Minimum &&
currentSeconds <= SSeek.Maximum)
{
SSeek.Value = currentSeconds;
}
}
if (currentTime > durationTime)
{
double newSeconds = Math.Round(
currentTime.TotalSeconds,
MidpointRounding.AwayFromZero);
// 當模式為多媒體播放器時,才會自動更新 ClipData 的結束時間。
if (CPPlayer.Mode == ClipPlayerMode.ClipPlayer)
{
CPPlayer.ClipData.EndTime = TimeSpan.FromSeconds(newSeconds);
}
LDuration.Content = $"{currentTime:hh\\:mm\\:ss} / {durationTime:hh\\:mm\\:ss}";
}
else
{
LDuration.Content = $"{currentTime:hh\\:mm\\:ss} / {durationTime:hh\\:mm\\:ss}";
}
// 當模式為多媒體播放器,且只有不是直播的短片,才會到結束時間時自動停止並切換下一個短片。
if (CPPlayer.Mode == ClipPlayerMode.ClipPlayer &&
!CPPlayer.ClipData.IsLivestream &&
currentSeconds >= endSeconds)
{
// 先主動讓 mpv 停止播放短片,以免此事件被重複觸發。
MPPlayer?.Stop();
BtnNext_Click(nameof(PositionChanged),
new RoutedEventArgs(ButtonBase.ClickEvent));
}
}
}));
}
catch (Exception ex)
{
WriteLog(
message: MsgSet.GetFmtStr(
MsgSet.MsgErrorOccured,
ex.GetExceptionMessage()),
logEventLevel: LogEventLevel.Error);
}
}
private void LogMessage(object? sender, MpvLogMessageEventArgs e)
{
MpvLogMessage mpvLogMessage = e.Message;
// 移除尾端的換行字元。
string rawText = mpvLogMessage.Text.TrimEnd('\r', '\n');
if (!string.IsNullOrEmpty(rawText))
{
WriteLog(message: $"[{mpvLogMessage.Prefix}] ({mpvLogMessage.LogLevel}) {rawText}");
}
}
}