202508151750

調整有無人聲問題
This commit is contained in:
allen.yan 2025-08-15 17:53:55 +08:00
parent 4c962b14ce
commit 7626173988
2 changed files with 51 additions and 30 deletions

View File

@ -9,16 +9,15 @@ namespace DualScreenDemo.Services
private Media? _media;
private bool _disposed;
public MediaService(nint handle)
public MediaService()
{
Core.Initialize();
_libVLC = new LibVLC(
"--aout=directsound",
"--network-caching=300",
"--file-caching=300",
"--audio-time-stretch",
"--video-filter=clone"
//$"--clone-views={handle}" // 這是 clone 的目標視窗
"--audio-time-stretch"
);
_mediaPlayer = new MediaPlayer(_libVLC);
@ -52,13 +51,10 @@ namespace DualScreenDemo.Services
_media = new Media(_libVLC, filePath, FromType.FromPath);
_media.AddOption(":audio-output=directsound");
_media.AddOption($":audio-track={audioTrackIndex}");
}
public void Play()
{
if (_media == null) return;
_mediaPlayer.Play(_media);
}
public void Play() => _mediaPlayer.Play();
public void Pause() => _mediaPlayer.Pause();
public void Stop() => _mediaPlayer.Stop();
#endregion

View File

@ -66,8 +66,8 @@ namespace DualScreenDemo
private const int GWL_EXSTYLE = -20;
private const int WS_EX_TOPMOST = 0x00000008;
private const uint SWP_NOZORDER = 0x0004;
//private MediaServicePrimary primary = new MediaServicePrimary();
private MediaService _media;
private MediaService _mediaService0= new MediaService();
private MediaService _mediaService1= new MediaService();
public static OverlayForm overlayForm;
public bool isMuted = false;
@ -121,16 +121,16 @@ namespace DualScreenDemo
}
else
{
_media= new MediaService(PrimaryForm.Instance.videoPanel.Handle);
_media.SetVideoOutput(this.Handle, secondMonitor.Bounds.Width, secondMonitor.Bounds.Height);
_mediaService0.SetVideoOutput(PrimaryForm.Instance.videoPanel.Handle,PrimaryForm.Instance.videoPanel.Width,PrimaryForm.Instance.videoPanel.Height);
_mediaService1.SetVideoOutput(this.Handle, secondMonitor.Bounds.Width, secondMonitor.Bounds.Height);
PlayNextSong();
}
}
private void VideoPlayerForm_FormClosing(object sender, FormClosingEventArgs e)
{
_media.Dispose();
_mediaService0.Dispose();
_mediaService1.Dispose();
// 清理COM
CoUninitialize();
}
@ -332,10 +332,11 @@ namespace DualScreenDemo
private Task InitializeAndPlayMedia(SongData song)
{
string pathToPlay = song.getFile();
// 渲染媒體文件
_media.LoadMedia(pathToPlay,song.isPublicSong ? 0 : 1);
_media.Mute(isMuted);
_media.Play();
_mediaService0.LoadMedia(pathToPlay, 0);
_mediaService0.Mute(isMuted);
_mediaService1.LoadMedia(pathToPlay, song.isPublicSong ? 0 : 1);
_mediaService1.Mute(isMuted);
// 音量處理
//SetVolume(isMuted ? 0 : previousVolume);
@ -370,7 +371,7 @@ namespace DualScreenDemo
{
try
{
if (_media.IsAtEnd())
if (_mediaService0.IsAtEnd() || _mediaService1.IsAtEnd())
{
BeginInvoke(new Action(async () =>
{
@ -381,7 +382,6 @@ namespace DualScreenDemo
catch (Exception ex)
{
Console.WriteLine($"監控媒體事件時發生錯誤: {ex.Message}");
await Task.Delay(1000);
}
await Task.Delay(1000);
@ -405,19 +405,22 @@ namespace DualScreenDemo
public void Play()
{
_media.Play();
_mediaService0.Play();
_mediaService1.Play();
isPaused = false;
OverlayForm.MainForm.HidePauseLabel();
}
public void Stop()
{
_media.Stop();
_mediaService0.Stop();
_mediaService1.Stop();
}
public void Pause()
{
_media.Pause();
_mediaService0.Pause();
_mediaService1.Pause();
isPaused = true;
OverlayForm.MainForm.ShowPauseLabel();
}
@ -456,23 +459,45 @@ namespace DualScreenDemo
}
public bool Mute(bool isMuted)
{
return _media.Mute(isMuted);
if(isMuted){
if(isVocalRemoved){
_mediaService0.Mute(true);
_mediaService1.Mute(false);
}else{
_mediaService0.Mute(false);
_mediaService1.Mute(true);
}
}else{
_mediaService0.Mute(false);
_mediaService1.Mute(false);
}
return isMuted;
}
public void SetVolume(int volume)
{
Console.WriteLine($"SetVolume: {volume}");
_media.SetVolume(volume);
_mediaService0.SetVolume(volume);
_mediaService1.SetVolume(volume);
}
public int GetVolume()
{
return _media.GetVolume();
return _mediaService1.GetVolume();
}
private bool isVocalRemoved = false;
private bool isVocalRemoved = true;
public void ToggleVocalRemoval()
{
isVocalRemoved=!isVocalRemoved;
int trackIndex = isVocalRemoved ? 1:0;
_media.SetAudioTrack(trackIndex);
if(isVocalRemoved){
_mediaService0.Mute(true);
_mediaService1.Mute(false);
}else{
_mediaService0.Mute(false);
_mediaService1.Mute(true);
}
//int trackIndex = isVocalRemoved ? 1:0;
//_mediaService.SetAudioTrack(trackIndex);
OverlayForm.MainForm.ShowTopRightLabelTime(isVocalRemoved ? "無人聲" : "有人聲");
}
}