兼容win7版应用
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

182 lines
4.8 KiB

using AIK.Models.VideoModels;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using CommunityToolkit.Mvvm.Messaging;
using CommunityToolkit.Mvvm.Messaging.Messages;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
namespace AIK.ViewModels.CommonViewModel
{
public partial class VideoPlayerViewModel : ObservableObject, IDisposable
{
[ObservableProperty]
private string _videoTitle = "视频演示";
[ObservableProperty]
private string _videoPath = string.Empty;
[ObservableProperty]
private bool _isPlaying = false;
[ObservableProperty]
private double _progress = 0;
[ObservableProperty]
private string _currentTime = "00:00";
[ObservableProperty]
private string _totalTime = "00:00";
[ObservableProperty]
private double _volume = 0.7;
[ObservableProperty]
private bool _isMuted = false;
public Action CloseAction { get; set; }
public Action<double> ProgressChanged { get; set; }
private bool _disposed = false;
[RelayCommand]
private void PlayPause()
{
IsPlaying = !IsPlaying;
// 实际播放控制逻辑在View中通过MediaElement实现
if (IsPlaying)
{
Play();
}
else
{
Pause();
}
}
[RelayCommand]
private void ToggleMute()
{
IsMuted = !IsMuted;
// 音量控制逻辑在View中实现
}
[RelayCommand]
private void ToggleFullScreen()
{
// 全屏逻辑可以在Window中实现
WeakReferenceMessenger.Default.Send(new VideoControlMessage
{
Action = VideoControlMessage.ControlAction.ToggleFullScreen
});
}
[RelayCommand]
private void Close()
{
this.IsMuted = true;
this.IsPlaying = false;
CloseAction?.Invoke();
}
partial void OnProgressChanged(double value)
{
// 进度变化时更新播放位置(在View中处理)
ProgressChanged?.Invoke(value);
}
partial void OnVolumeChanged(double value)
{
// 音量变化时发送消息给View更新MediaElement
WeakReferenceMessenger.Default.Send(new VolumeChangedMessage
{
Volume = value,
IsMuted = IsMuted
});
// 如果音量变为0且不是静音状态,自动静音
if (value <= 0 && !IsMuted)
{
IsMuted = true;
}
}
public void SetVideoUrl(string videoUrl)
{
VideoPath = videoUrl;
}
#region 外部控制方法
public void Play()
{
IsPlaying = true;
WeakReferenceMessenger.Default.Send(new VideoControlMessage
{
Action = VideoControlMessage.ControlAction.Play
});
}
public void Pause()
{
IsPlaying = false;
WeakReferenceMessenger.Default.Send(new VideoControlMessage
{
Action = VideoControlMessage.ControlAction.Pause
});
}
public void Stop()
{
IsPlaying = false;
WeakReferenceMessenger.Default.Send(new VideoControlMessage
{
Action = VideoControlMessage.ControlAction.Stop
});
}
public void Seek(TimeSpan position)
{
WeakReferenceMessenger.Default.Send(new VideoControlMessage
{
Action = VideoControlMessage.ControlAction.Seek,
Position = position
});
}
public void SetVolume(int volumePercent)
{
// 设置音量百分比(0-100)
Volume = Math.Max(0, Math.Min(100, volumePercent)) / 100.0;
}
#endregion
#region IDisposable 实现
public void Dispose()
{
//Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (!_disposed)
{
if (disposing)
{
//// 清理托管资源
//CloseAction = null;
//ProgressChanged = null;
//// 取消所有消息订阅
//WeakReferenceMessenger.Default.UnregisterAll(this);
}
_disposed = true;
}
}
#endregion
}
}