Add controls.rs for player commands
This commit is contained in:
69
src/player/controls.rs
Normal file
69
src/player/controls.rs
Normal file
@@ -0,0 +1,69 @@
|
|||||||
|
//! 播放控制命令
|
||||||
|
|
||||||
|
use super::VideoPlayer;
|
||||||
|
use anyhow::Result;
|
||||||
|
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub enum PlayerCommand {
|
||||||
|
Play,
|
||||||
|
Pause,
|
||||||
|
Stop,
|
||||||
|
SeekFrame(u64),
|
||||||
|
SeekTime(u64),
|
||||||
|
StepForward,
|
||||||
|
StepBackward,
|
||||||
|
SetSpeed(f32),
|
||||||
|
SetVolume(f32),
|
||||||
|
ToggleMute,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl VideoPlayer {
|
||||||
|
pub fn execute(&mut self, cmd: PlayerCommand) -> Result<String> {
|
||||||
|
match cmd {
|
||||||
|
PlayerCommand::Play => {
|
||||||
|
self.play()?;
|
||||||
|
Ok("Playing".to_string())
|
||||||
|
}
|
||||||
|
PlayerCommand::Pause => {
|
||||||
|
self.pause()?;
|
||||||
|
Ok("Paused".to_string())
|
||||||
|
}
|
||||||
|
PlayerCommand::Stop => {
|
||||||
|
self.stop()?;
|
||||||
|
Ok("Stopped".to_string())
|
||||||
|
}
|
||||||
|
PlayerCommand::SeekFrame(frame) => {
|
||||||
|
self.seek_frame(frame)?;
|
||||||
|
Ok(format!("Seeked to frame {}", frame))
|
||||||
|
}
|
||||||
|
PlayerCommand::SeekTime(ms) => {
|
||||||
|
self.seek_time(ms)?;
|
||||||
|
Ok(format!("Seeked to {} ms", ms))
|
||||||
|
}
|
||||||
|
PlayerCommand::StepForward => {
|
||||||
|
let next = self.get_current_frame() + 1;
|
||||||
|
self.seek_frame(next)?;
|
||||||
|
Ok(format!("Frame {}", next))
|
||||||
|
}
|
||||||
|
PlayerCommand::StepBackward => {
|
||||||
|
let prev = self.get_current_frame().saturating_sub(1);
|
||||||
|
self.seek_frame(prev)?;
|
||||||
|
Ok(format!("Frame {}", prev))
|
||||||
|
}
|
||||||
|
PlayerCommand::SetSpeed(speed) => {
|
||||||
|
if !(0.25..=4.0).contains(&speed) {
|
||||||
|
anyhow::bail!("Speed must be between 0.25 and 4.0");
|
||||||
|
}
|
||||||
|
Ok(format!("Speed set to {}x", speed))
|
||||||
|
}
|
||||||
|
PlayerCommand::SetVolume(volume) => {
|
||||||
|
let vol = volume.clamp(0.0, 1.0);
|
||||||
|
Ok(format!("Volume set to {:.0}%", vol * 100.0))
|
||||||
|
}
|
||||||
|
PlayerCommand::ToggleMute => {
|
||||||
|
let muted = !self.get_state().eq(&super::PlayState::Stopped);
|
||||||
|
Ok(format!("Mute: {}", muted))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user