Skip to content

Commit

Permalink
可以做到稳定播放 支持基本的播放暂停选曲拖动操作。
Browse files Browse the repository at this point in the history
  • Loading branch information
Milkitic committed Sep 8, 2018
1 parent c2e32b5 commit 51d9b0b
Show file tree
Hide file tree
Showing 10 changed files with 313 additions and 194 deletions.
1 change: 0 additions & 1 deletion OsuPlayer/Interface/IPlayer.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Milkitic.OsuPlayer.Models;
using SharpDX.MediaFoundation;
using System;
using System.Collections.Generic;
using System.Linq;
Expand Down
12 changes: 9 additions & 3 deletions OsuPlayer/Milkitic.OsuPlayer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,18 @@
<StartupObject />
</PropertyGroup>
<ItemGroup>
<Reference Include="NAudio, Version=1.8.4.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\NAudio.1.8.4\lib\net35\NAudio.dll</HintPath>
</Reference>
<Reference Include="NAudio.Vorbis, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\NAudio.Vorbis.1.0.0.0\lib\net35\NAudio.Vorbis.dll</HintPath>
</Reference>
<Reference Include="NVorbis, Version=0.8.4.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\NVorbis.0.8.4.0\lib\NVorbis.dll</HintPath>
</Reference>
<Reference Include="SharpDX, Version=4.2.0.0, Culture=neutral, PublicKeyToken=b4dcf0f35e5521f1, processorArchitecture=MSIL">
<HintPath>..\packages\SharpDX.4.2.0\lib\net45\SharpDX.dll</HintPath>
</Reference>
<Reference Include="SharpDX.MediaFoundation, Version=4.2.0.0, Culture=neutral, PublicKeyToken=b4dcf0f35e5521f1, processorArchitecture=MSIL">
<HintPath>..\packages\SharpDX.MediaFoundation.4.2.0\lib\net45\SharpDX.MediaFoundation.dll</HintPath>
</Reference>
<Reference Include="SharpDX.XAudio2, Version=4.2.0.0, Culture=neutral, PublicKeyToken=b4dcf0f35e5521f1, processorArchitecture=MSIL">
<HintPath>..\packages\SharpDX.XAudio2.4.2.0\lib\net45\SharpDX.XAudio2.dll</HintPath>
</Reference>
Expand Down
2 changes: 1 addition & 1 deletion OsuPlayer/Models/PlayStatus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ namespace Milkitic.OsuPlayer.Models
{
public enum PlayStatusEnum
{
Ready, Playing, Paused, Stopped
NotInitialized, Ready, Playing, Paused, Stopped
}
}
4 changes: 3 additions & 1 deletion OsuPlayer/PlayerMain.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

161 changes: 98 additions & 63 deletions OsuPlayer/PlayerMain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ namespace Milkitic.OsuPlayer
public partial class PlayerMain : Form
{
private HitsoundPlayer _hitsoundPlayer;
private readonly CancellationTokenSource _cts = new CancellationTokenSource();
private CancellationTokenSource _cts = new CancellationTokenSource();
private Task _statusTask;
private PlayStatusEnum tmpStatus = PlayStatusEnum.Stopped;

private PlayStatusEnum _tmpStatus = PlayStatusEnum.Stopped;
private bool _scrollLock;
private object _playerLock = new object();

public PlayerMain()
{
Expand All @@ -24,69 +24,14 @@ public PlayerMain()

private void Form1_Load(object sender, EventArgs e)
{
_statusTask = new Task(() =>
{
while (true)
{
if (_cts.IsCancellationRequested)
return;
if (_hitsoundPlayer != null)
{
BeginInvoke(new Action(() =>
{
btnControlPlayPause.Enabled = true;
btnControlStop.Enabled = true;
}));
if (tmpStatus != _hitsoundPlayer.PlayStatus)
{
switch (_hitsoundPlayer.PlayStatus)
{
case PlayStatusEnum.Playing:
BeginInvoke(new Action(() => { btnControlPlayPause.Text = @"Pause"; }));
break;
case PlayStatusEnum.Stopped:
case PlayStatusEnum.Paused:
BeginInvoke(new Action(() =>
{
btnControlPlayPause.Text = @"Play";
tkProgress.Value = Math.Min(_hitsoundPlayer.PlayTime, tkProgress.Maximum);
}));
break;
}
tmpStatus = _hitsoundPlayer.PlayStatus;
}
if (tmpStatus == PlayStatusEnum.Playing && !_scrollLock)
{
BeginInvoke(new Action(() =>
{
tkProgress.Maximum = _hitsoundPlayer.Duration;
tkProgress.Value = Math.Min(_hitsoundPlayer.PlayTime, tkProgress.Maximum);
}));
}
}
else
{
BeginInvoke(new Action(() =>
{
btnControlPlayPause.Enabled = false;
btnControlStop.Enabled = false;
}));
}
Thread.Sleep(50);
}
}, _cts.Token);

_statusTask.Start();
RunSurfaceUpdate();
}

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
ClearHitsoundPlayer();
_cts.Cancel();
Task.WaitAll(_statusTask);
ClearHitsoundPlayer();
WavePlayer.Device?.Dispose();
WavePlayer.MasteringVoice?.Dispose();
}
Expand All @@ -95,9 +40,32 @@ private void BtnLoadSingle_Click(object sender, EventArgs e)
{
string path = LoadFile();
if (path == null) return;
if (!File.Exists(path))
{
MessageBox.Show(@"你选择了一个不存在的文件。");
return;
}

ClearHitsoundPlayer();
_hitsoundPlayer = new HitsoundPlayer(path);
_hitsoundPlayer.Play();
try
{
_hitsoundPlayer = new HitsoundPlayer(path);
_cts = new CancellationTokenSource();
//RunSurfaceUpdate();
_hitsoundPlayer.Play();
}
catch (NotSupportedException ex)
{
MessageBox.Show(this, @"铺面读取时发生问题:" + ex.Message, Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
catch (FormatException ex)
{
MessageBox.Show(this, @"铺面读取时发生问题:" + ex.Message, Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
catch (Exception ex)
{
MessageBox.Show(this, @"发生未处理的异常问题:" + ex.ToString(), Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}

private void BtnControlPlayPause_Click(object sender, EventArgs e)
Expand All @@ -122,7 +90,7 @@ private void BtnControlPlayPause_Click(object sender, EventArgs e)

private void BtnControlStop_Click(object sender, EventArgs e)
{
_hitsoundPlayer.Stop();
_hitsoundPlayer?.Stop();
}

private void TkProgress_MouseUp(object sender, MouseEventArgs e)
Expand Down Expand Up @@ -158,11 +126,78 @@ private static string LoadFile()
};
return openFileDialog.ShowDialog() != DialogResult.OK ? null : openFileDialog.FileName;
}

private void ClearHitsoundPlayer()
{
_hitsoundPlayer?.Stop();
_hitsoundPlayer?.Dispose();
_hitsoundPlayer = null;

}

private void RunSurfaceUpdate()
{
_statusTask = Task.Run(new Action(UpdateSurface), _cts.Token);
}

private void UpdateSurface()
{
while (true)
{
if (_cts.IsCancellationRequested) return;

if (_hitsoundPlayer != null)
{
BeginInvoke(new Action(() =>
{
btnControlPlayPause.Enabled = true;
btnControlStop.Enabled = true;
tkProgress.Enabled = true;
}));
if (_tmpStatus != _hitsoundPlayer.PlayStatus)
{
switch (_hitsoundPlayer.PlayStatus)
{
case PlayStatusEnum.Playing:
BeginInvoke(new Action(() => { btnControlPlayPause.Text = @"Pause"; }));
break;
case PlayStatusEnum.Stopped:
case PlayStatusEnum.Paused:
var ok = Math.Min(_hitsoundPlayer.PlayTime, tkProgress.Maximum);
BeginInvoke(new Action(() =>
{
btnControlPlayPause.Text = @"Play";
tkProgress.Value = ok < 0 ? 0 : ok;
}));
break;
}

_tmpStatus = _hitsoundPlayer.PlayStatus;
}

if (_tmpStatus == PlayStatusEnum.Playing && !_scrollLock)
{
var ok = Math.Min(_hitsoundPlayer.PlayTime, tkProgress.Maximum);
BeginInvoke(new Action(() =>
{
if (_hitsoundPlayer == null) return;
tkProgress.Maximum = _hitsoundPlayer.Duration;
tkProgress.Value = ok < 0 ? 0 : (ok > tkProgress.Maximum ? tkProgress.Maximum : ok);
}));
}
}
else
{
BeginInvoke(new Action(() =>
{
btnControlPlayPause.Enabled = false;
btnControlStop.Enabled = false;
tkProgress.Enabled = false;
}));
}

Thread.Sleep(50);
}
}
}
}
13 changes: 12 additions & 1 deletion OsuPlayer/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
using System;
using Milkitic.OsuPlayer.Models;
using Milkitic.OsuPlayer.Utils;
using NAudio.Wave;
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Milkitic.OsuPlayer
Expand All @@ -15,5 +21,10 @@ static void Main()
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new PlayerMain());
}

private static void OnPlaybackStopped(object sender, StoppedEventArgs e)
{

}
}
}
Loading

0 comments on commit 51d9b0b

Please sign in to comment.