Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

载入视频时完全加载弹幕库 #805

Merged
merged 1 commit into from
Oct 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,13 @@ private async void OnDanmakuListAddedAsync(object? sender, IReadOnlyList<Danmaku

private void OnRequestClearDanmaku(object? sender, EventArgs e)
{
_cachedDanmakus.Clear();
if (ViewModel.IsShowDanmaku)
{
_cachedDanmakus.Clear();
_lastProgress = 0;
}

_danmakuController?.Clear();
_lastProgress = 0;
}

private void OnRequestAddSingleDanmaku(object? sender, string e)
Expand Down Expand Up @@ -192,8 +196,15 @@ private void Redraw(bool force = false)

DispatcherQueue?.TryEnqueue(() =>
{
_danmakuController?.Close();
_danmakuController = new DanmakuFrostMaster(RootGrid, this.Get<ILogger<DanmakuFrostMaster>>());
if (_danmakuController is not null)
{
_danmakuController?.Clear();
}
else
{
_danmakuController = new DanmakuFrostMaster(RootGrid, this.Get<ILogger<DanmakuFrostMaster>>());
}

if (_cachedDanmakus.Any())
{
_danmakuController.AddDanmakuList(_cachedDanmakus);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ public sealed partial class DanmakuViewModel

private string _aid;
private string _cid;
private int _segmentIndex;
private int _position;
private int _duration;

[ObservableProperty]
private bool _isShowDanmaku;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Microsoft.Extensions.Logging;
using Richasy.BiliKernel.Bili.Media;
using Richasy.BiliKernel.Models;
using Richasy.BiliKernel.Models.Danmaku;
using Richasy.WinUI.Share.ViewModels;

namespace BiliCopilot.UI.ViewModels.Core;
Expand Down Expand Up @@ -35,7 +36,6 @@ public void ResetData(string aid, string cid)
ClearAll();
_aid = aid;
_cid = cid;
_segmentIndex = -1;
ResetData();
}

Expand All @@ -54,13 +54,6 @@ public void ResetData()
public void UpdatePosition(int position)
{
_position = position;
var index = Convert.ToInt32(Math.Ceiling(position / 360d));
if (index == 0)
{
index = 1;
}

LoadDanmakusCommand.Execute(index);
ProgressChanged?.Invoke(this, position);
}

Expand All @@ -85,13 +78,8 @@ public void Resume()
/// <summary>
/// 重新绘制.
/// </summary>
public void Redraw(bool force = false)
public void Redraw()
{
if (force)
{
_segmentIndex = -1;
}

RequestRedrawDanmaku?.Invoke(this, EventArgs.Empty);
}

Expand All @@ -100,8 +88,7 @@ public void Redraw(bool force = false)
/// </summary>
public void ClearAll()
{
_segmentIndex = -1;
_position = 0;
_duration = 0;
_aid = string.Empty;
_cid = string.Empty;
ClearDanmaku();
Expand All @@ -127,32 +114,43 @@ public bool IsEmpty()
=> string.IsNullOrEmpty(_aid) || string.IsNullOrEmpty(_cid);

[RelayCommand]
private async Task LoadDanmakusAsync(int index = 0)
private async Task LoadDanmakusAsync(int duration)
{
if (IsLoading || _segmentIndex == index || !IsShowDanmaku || string.IsNullOrEmpty(_aid) || string.IsNullOrEmpty(_cid))
if (IsLoading || string.IsNullOrEmpty(_aid) || string.IsNullOrEmpty(_cid) || _duration == duration)
{
return;
}

try
IsLoading = true;
_duration = duration;
var count = Convert.ToInt32(Math.Ceiling(_duration / 360d));
if (count == 0)
{
IsLoading = true;
var danmakus = await _danmakuService.GetSegmentDanmakusAsync(_aid, _cid, index);
if (danmakus.Count > 0)
{
ListAdded?.Invoke(this, danmakus);
}

_segmentIndex = index;
count = 1;
}
catch (Exception ex)

var totalDanmakus = new List<DanmakuInformation>();
for (var i = 0; i < count; i++)
{
_logger.LogError(ex, $"加载 {_aid} | {_cid} 的弹幕失败,索引为 {index}");
try
{
var danmakus = await _danmakuService.GetSegmentDanmakusAsync(_aid, _cid, i + 1);
totalDanmakus.AddRange(danmakus);
}
catch (Exception ex)
{
_logger.LogError(ex, $"加载 {_aid} | {_cid} 的弹幕失败,索引为 {i}");
break;
}
}
finally

if (totalDanmakus.Count > 0)
{
IsLoading = false;
ListAdded?.Invoke(this, totalDanmakus);
}

Redraw();
IsLoading = false;
}

[RelayCommand]
Expand Down Expand Up @@ -208,10 +206,11 @@ partial void OnIsShowDanmakuChanged(bool value)
{
if (value)
{
Redraw(true);
Redraw();
}
else
{
ClearDanmaku();
ResetStyle();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ private void PlayerProgressChanged(int progress, int duration)
var aid = _episode.GetExtensionIfNotNull<long>(EpisodeExtensionDataId.Aid).ToString();
var cid = _episode.GetExtensionIfNotNull<long>(EpisodeExtensionDataId.Cid).ToString();
Danmaku.ResetData(aid, cid);
Danmaku.Redraw(true);
Danmaku.LoadDanmakusCommand.Execute(duration);
}

Danmaku?.UpdatePosition(progress);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,7 @@ private void PlayerProgressChanged(int progress, int duration)
if (progress < duration && progress > 1 && (Danmaku?.IsEmpty() ?? false))
{
Danmaku?.ResetData(_view?.Information.Identifier.Id, _part?.Identifier.Id);
Danmaku?.LoadDanmakusCommand.Execute(duration);
}

Danmaku?.UpdatePosition(progress);
Expand Down
2 changes: 2 additions & 0 deletions src/Libs/Danmaku.Core/DanmakuFrostMaster.cs
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,8 @@ public void Seek(uint targetMs)
break;
}
}

_render?.Start();
}
_lastTimeMs = targetMs;
_isSeeking = false;
Expand Down
Loading