Skip to content

Commit

Permalink
Add: 完善歌词页面
Browse files Browse the repository at this point in the history
  • Loading branch information
Miaoyww committed Mar 9, 2024
1 parent 68b389b commit 3aadc18
Show file tree
Hide file tree
Showing 22 changed files with 463 additions and 150 deletions.
75 changes: 7 additions & 68 deletions NonsPlayer.Core/Models/Lyric.cs
Original file line number Diff line number Diff line change
@@ -1,82 +1,21 @@
using System.Text.Json.Serialization;
using System.Text.RegularExpressions;
using LyricParser.Abstraction;

Check failure on line 3 in NonsPlayer.Core/Models/Lyric.cs

View workflow job for this annotation

GitHub Actions / build (Debug, x64)

The type or namespace name 'LyricParser' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 3 in NonsPlayer.Core/Models/Lyric.cs

View workflow job for this annotation

GitHub Actions / build (Debug, x64)

The type or namespace name 'LyricParser' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 3 in NonsPlayer.Core/Models/Lyric.cs

View workflow job for this annotation

GitHub Actions / build (Debug, arm64)

The type or namespace name 'LyricParser' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 3 in NonsPlayer.Core/Models/Lyric.cs

View workflow job for this annotation

GitHub Actions / build (Debug, arm64)

The type or namespace name 'LyricParser' could not be found (are you missing a using directive or an assembly reference?)
using Newtonsoft.Json.Linq;

namespace NonsPlayer.Core.Models;

public class LyricGroup
{
[JsonPropertyName("lyrics")] public List<Lyric> Lyrics;

public LyricGroup(JObject lrc)
[JsonPropertyName("lyrics")] public LrcLyricCollection Lyrics;

Check failure on line 10 in NonsPlayer.Core/Models/Lyric.cs

View workflow job for this annotation

GitHub Actions / build (Debug, x64)

The type or namespace name 'LrcLyricCollection' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 10 in NonsPlayer.Core/Models/Lyric.cs

View workflow job for this annotation

GitHub Actions / build (Debug, arm64)

The type or namespace name 'LrcLyricCollection' could not be found (are you missing a using directive or an assembly reference?)
[JsonPropertyName("trans_lyrics")] public LrcLyricCollection? TransLyrics;

Check failure on line 11 in NonsPlayer.Core/Models/Lyric.cs

View workflow job for this annotation

GitHub Actions / build (Debug, x64)

The type or namespace name 'LrcLyricCollection' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 11 in NonsPlayer.Core/Models/Lyric.cs

View workflow job for this annotation

GitHub Actions / build (Debug, arm64)

The type or namespace name 'LrcLyricCollection' could not be found (are you missing a using directive or an assembly reference?)

public LyricGroup(LrcLyricCollection originalLyric, LrcLyricCollection transLyric = null)

Check failure on line 13 in NonsPlayer.Core/Models/Lyric.cs

View workflow job for this annotation

GitHub Actions / build (Debug, x64)

The type or namespace name 'LrcLyricCollection' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 13 in NonsPlayer.Core/Models/Lyric.cs

View workflow job for this annotation

GitHub Actions / build (Debug, x64)

The type or namespace name 'LrcLyricCollection' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 13 in NonsPlayer.Core/Models/Lyric.cs

View workflow job for this annotation

GitHub Actions / build (Debug, arm64)

The type or namespace name 'LrcLyricCollection' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 13 in NonsPlayer.Core/Models/Lyric.cs

View workflow job for this annotation

GitHub Actions / build (Debug, arm64)

The type or namespace name 'LrcLyricCollection' could not be found (are you missing a using directive or an assembly reference?)
{
Lyrics = new List<Lyric>();
var oArray = ParseLrc(lrc["lrc"]["lyric"].ToString());
Tuple<TimeSpan, string>[]? tArray = null;
Dictionary<TimeSpan, int> timeSpanTable = new();
foreach (var item in lrc["lrc"]["lyric"].ToString().Split("\n"))
{
JObject newLyric;
try
{
if (item.Contains("{"))
newLyric = JObject.Parse(item);
else
continue;
}
catch (Exception e)
{
continue;
}

var time = TimeSpan.Zero;
var word = string.Join("", newLyric["c"].Select(x => x["tx"].ToString()));
Lyrics.Add(new Lyric(word, string.Empty, time));
}

if (lrc.Property("tlyric") != null)
{
tArray = ParseLrc(lrc["tlyric"]["lyric"].ToString());
for (var i = 0; i < tArray.Length; i++)
{
var item = tArray[i];
timeSpanTable[item.Item1] = i;
}
}

foreach (var item in oArray)
{
var tLyric = string.Empty;
if (tArray != null)
if (timeSpanTable.TryGetValue(item.Item1, out var index))
tLyric = tArray[index].Item2;

Lyrics.Add(new Lyric(item.Item2, tLyric, item.Item1));
}
Lyrics = originalLyric;
TransLyrics = transLyric;
}

[JsonPropertyName("lyric_count")] public int? Count => Lyrics.Count;

private Tuple<TimeSpan, string>[] ParseLrc(string content)
{
var match = Regex.Matches(content, "\\[([0-9.:]*)\\]+(.*)", RegexOptions.Compiled);
var result = new Tuple<TimeSpan, string>[match.Count];
for (var i = 0; i < match.Count; i++)
{
var item = match[i];
TimeSpan time;
if (item.Groups[1].Value == "99:00.00")
// 纯音乐或无歌词
time = TimeSpan.MaxValue;
else
time = TimeSpan.Parse("00:" + item.Groups[1].Value);

var word = item.Groups[2].Value;
result[i] = new Tuple<TimeSpan, string>(time, word);
}

return result;
}
}

public class Lyric
Expand Down
16 changes: 13 additions & 3 deletions NonsPlayer.Core/Models/Music.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using Newtonsoft.Json.Linq;
using LyricParser.Abstraction;

Check failure on line 1 in NonsPlayer.Core/Models/Music.cs

View workflow job for this annotation

GitHub Actions / build (Debug, x64)

The type or namespace name 'LyricParser' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 1 in NonsPlayer.Core/Models/Music.cs

View workflow job for this annotation

GitHub Actions / build (Debug, x64)

The type or namespace name 'LyricParser' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 1 in NonsPlayer.Core/Models/Music.cs

View workflow job for this annotation

GitHub Actions / build (Debug, arm64)

The type or namespace name 'LyricParser' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 1 in NonsPlayer.Core/Models/Music.cs

View workflow job for this annotation

GitHub Actions / build (Debug, arm64)

The type or namespace name 'LyricParser' could not be found (are you missing a using directive or an assembly reference?)
using Newtonsoft.Json.Linq;
using NonsPlayer.Core.Api;
using NonsPlayer.Core.Contracts.Models;
using NonsPlayer.Core.Enums;
using NonsPlayer.Core.Exceptions;
using NonsPlayer.Core.Nons;
using LyricParser.Implementation;

Check failure on line 8 in NonsPlayer.Core/Models/Music.cs

View workflow job for this annotation

GitHub Actions / build (Debug, x64)

The type or namespace name 'LyricParser' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 8 in NonsPlayer.Core/Models/Music.cs

View workflow job for this annotation

GitHub Actions / build (Debug, x64)

The type or namespace name 'LyricParser' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 8 in NonsPlayer.Core/Models/Music.cs

View workflow job for this annotation

GitHub Actions / build (Debug, arm64)

The type or namespace name 'LyricParser' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 8 in NonsPlayer.Core/Models/Music.cs

View workflow job for this annotation

GitHub Actions / build (Debug, arm64)

The type or namespace name 'LyricParser' could not be found (are you missing a using directive or an assembly reference?)

namespace NonsPlayer.Core.Models;

Expand All @@ -14,7 +16,7 @@ public class Music : INonsModel
public string FileType;
public bool IsEmpty;
public bool IsLiked;
public LyricGroup LyricGroup;
public LyricGroup? Lyrics;
public MusicQualityLevel[] QualityLevels;
public TimeSpan TotalTime;
public string Trans;
Expand Down Expand Up @@ -51,6 +53,14 @@ public async Task GetFileInfo()

public async Task GetLyric()
{
LyricGroup = new LyricGroup(await Apis.Lyric.GetLyric(Id.ToString(), NonsCore.Instance));
var response = await Apis.Lyric.GetLyric(Id.ToString(), NonsCore.Instance);
var originalLyric = LrcParser.ParseLrc(response["lrc"]["lyric"].ToString().AsSpan());
LrcLyricCollection? transLyric = null;
if (response["tlyric"]?["lyric"] != null)
{
transLyric = LrcParser.ParseLrc(response["tlyric"]["lyric"].ToString().AsSpan());
}

Lyrics = new LyricGroup(originalLyric, transLyric);
}
}
2 changes: 1 addition & 1 deletion NonsPlayer.Core/Nons/Player/Player.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public async Task NewPlay(Music music2play)
{
if (OutputDevice == null) OutputDevice = new WaveOutEvent();
await Task.WhenAll(music2play.GetLyric(), music2play.GetFileInfo());
MusicChangedHandle(music2play);
MusicChangedHandle?.Invoke(music2play);
CurrentMusic = music2play;
if (NPMediaFoundationReader == null) NPMediaFoundationReader = new MediaFoundationReader(music2play.Url);

Expand Down
2 changes: 1 addition & 1 deletion NonsPlayer.Core/NonsPlayer.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@
</ItemGroup>

<ItemGroup>
<Folder Include="Controller\" />
<ProjectReference Include="..\LyricParser\LyricParser\LyricParser.csproj" />
</ItemGroup>
</Project>
2 changes: 1 addition & 1 deletion NonsPlayer.Core/Services/ControlService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ public class ControlService
public ControlService()
{
_httpListener = new HttpListener();
_httpListener.Prefixes.Add($"http://127.0.0.1:8080/nons/api/{API_VERSION}/");
_httpListener.Prefixes.Add($"http://192.168.101.111:8080/nons/api/{API_VERSION}/");
_cancellationTokenSource = new CancellationTokenSource();
}

Expand Down
1 change: 1 addition & 0 deletions NonsPlayer/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ public App()
services.AddTransient<BestMusicCardViewModel>();
services.AddTransient<BestArtistCardViewModel>();
services.AddTransient<MusicListBarViewModel>();
services.AddTransient<LyricItemViewModel>();
// Configuration
services.Configure<LocalSettingsOptions>(
context.Configuration.GetSection(nameof(LocalSettingsOptions)));
Expand Down
24 changes: 24 additions & 0 deletions NonsPlayer/Components/Models/LyricModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using LyricParser.Abstraction;
using Microsoft.UI.Xaml;
using NonsPlayer.Core.Models;

namespace NonsPlayer.Components.Models;

public class LyricModel
{
public static LyricModel PureSong = new()
{ LyricLine = new LrcLyricsLine("纯音乐 请欣赏", string.Empty, TimeSpan.Zero) };

public static LyricModel NoLyric = new()
{ LyricLine = new LrcLyricsLine("无歌词 请欣赏", string.Empty, TimeSpan.Zero) };

public static LyricModel LoadingLyric = new()
{ LyricLine = new LrcLyricsLine("加载歌词中...", string.Empty, TimeSpan.Zero) };

public ILyricLine LyricLine;
public string Translation;

public bool HaveTranslation => !string.IsNullOrEmpty(Translation);
public Type LyricType => LyricLine.GetType();
public Visibility TransVisibility;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace NonsPlayer.Components.Models;

public class MusicItem
public class MusicModel
{
public string Index;
public Music Music;
Expand Down
25 changes: 25 additions & 0 deletions NonsPlayer/Components/ViewModels/LyricItemViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using LyricParser.Abstraction;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls.Primitives;
using NonsPlayer.Core.Helpers;
using NonsPlayer.Core.Nons.Player;
using NonsPlayer.Core.Services;
using NonsPlayer.Helpers;
using NonsPlayer.Services;
using NonsPlayer.ViewModels;

namespace NonsPlayer.Components.ViewModels;

public partial class LyricItemViewModel : ObservableObject
{
[ObservableProperty] private Visibility transVisibility = Visibility.Visible;
[ObservableProperty] private ILyricLine? pureLyric;
[ObservableProperty] private string? transLyric;
public LyricItemViewModel()
{
}


}
4 changes: 2 additions & 2 deletions NonsPlayer/Components/ViewModels/MusicListBarViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ namespace NonsPlayer.Components.ViewModels;

public class MusicListBarViewModel : ObservableObject
{
public ObservableCollection<MusicItem> MusicItems = new();
public ObservableCollection<MusicModel> MusicItems = new();

public void UpdateMusicItems(ObservableCollection<MusicItem> items)
public void UpdateMusicItems(ObservableCollection<MusicModel> items)
{
MusicItems = items;
}
Expand Down
24 changes: 24 additions & 0 deletions NonsPlayer/Components/Views/LyricItem.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<UserControl
x:Class="NonsPlayer.Components.Views.LyricItem"
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"
mc:Ignorable="d">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Name="TextBlockPureLyric"
Text="{x:Bind ViewModel.PureLyric.CurrentLyric,Mode=OneWay}"
HorizontalAlignment="Left" VerticalAlignment="Top" TextAlignment="Center"
FontFamily="HarmonyOS Sans SC" FontWeight="Bold" FontSize="20" />

<TextBlock Grid.Row="1" Name="TextBlockTranLyric" Text="{x:Bind ViewModel.TransLyric, Mode=OneWay}"
Visibility="{x:Bind ViewModel.TransVisibility, Mode=OneWay}"
HorizontalAlignment="Left" VerticalAlignment="Bottom" TextAlignment="Center"
FontFamily="HarmonyOS Sans SC" FontWeight="Bold" FontSize="18" />

</Grid>
</UserControl>
41 changes: 41 additions & 0 deletions NonsPlayer/Components/Views/LyricItem.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System.Diagnostics;
using System.Windows.Forms;
using CommunityToolkit.Mvvm.Input;
using LyricParser.Abstraction;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls.Primitives;
using Microsoft.UI.Xaml.Input;
using NonsPlayer.Components.ViewModels;
using NonsPlayer.Core.Nons.Player;
using NonsPlayer.Core.Services;
using NonsPlayer.Helpers;
using NonsPlayer.Services;
using NonsPlayer.ViewModels;
using UserControl = Microsoft.UI.Xaml.Controls.UserControl;

namespace NonsPlayer.Components.Views;

public sealed partial class LyricItem : UserControl
{
public LyricItem()
{
ViewModel = App.GetService<LyricItemViewModel>();
InitializeComponent();
}

public LyricItemViewModel ViewModel { get; }

public ILyricLine? PureLyric
{
set => ViewModel.PureLyric = value;
}

public string? TransLyric
{
set => ViewModel.TransLyric = value;
}
public Visibility TransVisibility
{
set => ViewModel.TransVisibility = value;
}
}
2 changes: 1 addition & 1 deletion NonsPlayer/Components/Views/MusicListBar.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
BorderThickness="0,1,0,0" BorderBrush="#F0F1F2"
ItemsSource="{x:Bind ViewModel.MusicItems, Mode=OneWay}">
<ListView.ItemTemplate>
<DataTemplate x:DataType="models:MusicItem">
<DataTemplate x:DataType="models:MusicModel">
<views:PlaylistMusicItemCard
Music="{x:Bind Music}"
Index="{x:Bind Index}" />
Expand Down
2 changes: 1 addition & 1 deletion NonsPlayer/Components/Views/MusicListBar.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public MusicListBar()
InitializeComponent();
}

public ObservableCollection<MusicItem> MusicItems
public ObservableCollection<MusicModel> MusicItems
{
set => ViewModel.UpdateMusicItems(value);
}
Expand Down
Loading

0 comments on commit 3aadc18

Please sign in to comment.