mirror of
https://github.com/LorisYounger/VPet.git
synced 2024-08-30 18:42:36 +00:00
修复播放音频无声音的问题
This commit is contained in:
parent
481cbca821
commit
8ea0820857
@ -3,6 +3,7 @@ using LinePutScript.Localization.WPF;
|
|||||||
using Panuon.WPF.UI;
|
using Panuon.WPF.UI;
|
||||||
using System;
|
using System;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using System.Media;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using System.Windows;
|
using System.Windows;
|
||||||
@ -49,6 +50,8 @@ namespace VPet_Simulator.Core
|
|||||||
/// 是否开始运行
|
/// 是否开始运行
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public bool IsWorking { get; private set; } = false;
|
public bool IsWorking { get; private set; } = false;
|
||||||
|
public SoundPlayer soundPlayer = new SoundPlayer();
|
||||||
|
public bool windowMediaPlayerAvailable = true;
|
||||||
public Main(GameCore core, bool loadtouchevent = true)
|
public Main(GameCore core, bool loadtouchevent = true)
|
||||||
{
|
{
|
||||||
//Console.WriteLine(DateTime.Now.ToString("T:fff"));
|
//Console.WriteLine(DateTime.Now.ToString("T:fff"));
|
||||||
@ -163,21 +166,30 @@ namespace VPet_Simulator.Core
|
|||||||
public void PlayVoice(Uri VoicePath)//, TimeSpan timediff = TimeSpan.Zero) TODO
|
public void PlayVoice(Uri VoicePath)//, TimeSpan timediff = TimeSpan.Zero) TODO
|
||||||
{
|
{
|
||||||
PlayingVoice = true;
|
PlayingVoice = true;
|
||||||
Dispatcher.Invoke(() =>
|
if (windowMediaPlayerAvailable)
|
||||||
{
|
{
|
||||||
VoicePlayer.Clock = new MediaTimeline(VoicePath).CreateClock();
|
Dispatcher.Invoke(() =>
|
||||||
VoicePlayer.Clock.Completed += Clock_Completed;
|
{
|
||||||
VoicePlayer.Play();
|
VoicePlayer.Clock = new MediaTimeline(VoicePath).CreateClock();
|
||||||
//Task.Run(() =>
|
VoicePlayer.Clock.Completed += Clock_Completed;
|
||||||
//{
|
VoicePlayer.MediaFailed += MediaPlayer_MediaFailed;
|
||||||
// Thread.Sleep(1000);
|
VoicePlayer.Play();
|
||||||
// Dispatcher.Invoke(() =>
|
//Task.Run(() =>
|
||||||
// {
|
//{
|
||||||
// if (VoicePlayer?.Clock?.NaturalDuration.HasTimeSpan == true)
|
// Thread.Sleep(1000);
|
||||||
// PlayEndTime += VoicePlayer.Clock.NaturalDuration.TimeSpan - TimeSpan.FromSeconds(0.8);
|
// Dispatcher.Invoke(() =>
|
||||||
// });
|
// {
|
||||||
//});
|
// if (VoicePlayer?.Clock?.NaturalDuration.HasTimeSpan == true)
|
||||||
});
|
// PlayEndTime += VoicePlayer.Clock.NaturalDuration.TimeSpan - TimeSpan.FromSeconds(0.8);
|
||||||
|
// });
|
||||||
|
//});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
soundPlayer.SoundLocation = VoicePath.LocalPath;
|
||||||
|
soundPlayer.LoadAsync();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 声音音量
|
/// 声音音量
|
||||||
@ -191,6 +203,12 @@ namespace VPet_Simulator.Core
|
|||||||
/// 当前是否正在播放
|
/// 当前是否正在播放
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public bool PlayingVoice = false;
|
public bool PlayingVoice = false;
|
||||||
|
private void MediaPlayer_MediaFailed(object sender, ExceptionRoutedEventArgs e)
|
||||||
|
{
|
||||||
|
windowMediaPlayerAvailable = false;
|
||||||
|
PlayingVoice = false;
|
||||||
|
MessageBoxX.Show("音频播放失败,已尝试自动切换到备用播放器。如果问题持续,请检查是否已安装WindowsMediaPlayer。\nAudio playback failed, attempted automatic switch to backup player. If the issue persists, please check if Windows Media Player is installed.", "音频错误 audio error", Panuon.WPF.UI.MessageBoxIcon.Warning);
|
||||||
|
}
|
||||||
private void Clock_Completed(object sender, EventArgs e)
|
private void Clock_Completed(object sender, EventArgs e)
|
||||||
{
|
{
|
||||||
PlayingVoice = false;
|
PlayingVoice = false;
|
||||||
|
@ -57,14 +57,25 @@ namespace VPet_Simulator.Core
|
|||||||
{
|
{
|
||||||
if (m.PlayingVoice)
|
if (m.PlayingVoice)
|
||||||
{
|
{
|
||||||
TimeSpan ts = Dispatcher.Invoke(() => m.VoicePlayer?.Clock?.NaturalDuration.HasTimeSpan == true ? (m.VoicePlayer.Clock.NaturalDuration.TimeSpan - m.VoicePlayer.Clock.CurrentTime.Value) : TimeSpan.Zero);
|
if (m.windowMediaPlayerAvailable)
|
||||||
if (ts.TotalSeconds > 2)
|
|
||||||
{
|
{
|
||||||
return;
|
TimeSpan ts = Dispatcher.Invoke(() => m.VoicePlayer?.Clock?.NaturalDuration.HasTimeSpan == true ? (m.VoicePlayer.Clock.NaturalDuration.TimeSpan - m.VoicePlayer.Clock.CurrentTime.Value) : TimeSpan.Zero);
|
||||||
|
if (ts.TotalSeconds > 2)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Console.WriteLine(1);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
Console.WriteLine(1);
|
if (m.soundPlayer.IsLoadCompleted)
|
||||||
|
{
|
||||||
|
m.PlayingVoice = false;
|
||||||
|
m.soundPlayer.PlaySync();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Task.Run(() =>
|
Task.Run(() =>
|
||||||
|
Loading…
Reference in New Issue
Block a user