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 System;
|
||||
using System.Linq;
|
||||
using System.Media;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows;
|
||||
@ -49,6 +50,8 @@ namespace VPet_Simulator.Core
|
||||
/// 是否开始运行
|
||||
/// </summary>
|
||||
public bool IsWorking { get; private set; } = false;
|
||||
public SoundPlayer soundPlayer = new SoundPlayer();
|
||||
public bool windowMediaPlayerAvailable = true;
|
||||
public Main(GameCore core, bool loadtouchevent = true)
|
||||
{
|
||||
//Console.WriteLine(DateTime.Now.ToString("T:fff"));
|
||||
@ -163,10 +166,13 @@ namespace VPet_Simulator.Core
|
||||
public void PlayVoice(Uri VoicePath)//, TimeSpan timediff = TimeSpan.Zero) TODO
|
||||
{
|
||||
PlayingVoice = true;
|
||||
if (windowMediaPlayerAvailable)
|
||||
{
|
||||
Dispatcher.Invoke(() =>
|
||||
{
|
||||
VoicePlayer.Clock = new MediaTimeline(VoicePath).CreateClock();
|
||||
VoicePlayer.Clock.Completed += Clock_Completed;
|
||||
VoicePlayer.MediaFailed += MediaPlayer_MediaFailed;
|
||||
VoicePlayer.Play();
|
||||
//Task.Run(() =>
|
||||
//{
|
||||
@ -179,6 +185,12 @@ namespace VPet_Simulator.Core
|
||||
//});
|
||||
});
|
||||
}
|
||||
else
|
||||
{
|
||||
soundPlayer.SoundLocation = VoicePath.LocalPath;
|
||||
soundPlayer.LoadAsync();
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 声音音量
|
||||
/// </summary>
|
||||
@ -191,6 +203,12 @@ namespace VPet_Simulator.Core
|
||||
/// 当前是否正在播放
|
||||
/// </summary>
|
||||
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)
|
||||
{
|
||||
PlayingVoice = false;
|
||||
|
@ -56,6 +56,8 @@ namespace VPet_Simulator.Core
|
||||
else
|
||||
{
|
||||
if (m.PlayingVoice)
|
||||
{
|
||||
if (m.windowMediaPlayerAvailable)
|
||||
{
|
||||
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)
|
||||
@ -67,6 +69,15 @@ namespace VPet_Simulator.Core
|
||||
Console.WriteLine(1);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (m.soundPlayer.IsLoadCompleted)
|
||||
{
|
||||
m.PlayingVoice = false;
|
||||
m.soundPlayer.PlaySync();
|
||||
}
|
||||
}
|
||||
}
|
||||
Task.Run(() =>
|
||||
{
|
||||
Thread.Sleep(timeleft * 50);
|
||||
|
Loading…
Reference in New Issue
Block a user