From aee45d05095e4ceea5322ad332fea55ae897932a Mon Sep 17 00:00:00 2001 From: Mavridis Philippe Date: Wed, 24 May 2023 12:19:31 +0300 Subject: [PATCH] mpvpart: added volume controls Signed-off-by: Mavridis Philippe --- .../player-parts/libmpv-part/libmpv_part.cpp | 31 +++++++++++++++++-- .../player-parts/libmpv-part/libmpv_part.h | 6 ++++ .../player-parts/libmpv-part/libmpv_part.rc | 8 +++-- 3 files changed, 40 insertions(+), 5 deletions(-) diff --git a/kaffeine/src/player-parts/libmpv-part/libmpv_part.cpp b/kaffeine/src/player-parts/libmpv-part/libmpv_part.cpp index a42b3c7..7f7406f 100644 --- a/kaffeine/src/player-parts/libmpv-part/libmpv_part.cpp +++ b/kaffeine/src/player-parts/libmpv-part/libmpv_part.cpp @@ -121,12 +121,14 @@ bool MpvPart::initMpv() { void MpvPart::initActions() { + /** Playback controls ***/ new TDEAction(i18n("Play"), "media-playback-start", 0, this, SLOT(slotPlay()), actionCollection(), "player_play"); new TDEAction(i18n("Pause"), "media-playback-pause", Key_Space, this, SLOT(slotTogglePause()), actionCollection(), "player_pause"); new TDEAction(i18n("Stop"), "media-playback-stop", Key_Backspace, this, SLOT(slotStop()), actionCollection(), "player_stop"); new TDEAction(i18n("&Previous"), "media-skip-backward", Key_PageUp, this, SLOT(slotPrevious()), actionCollection(), "player_previous"); new TDEAction(i18n("&Next"), "media-skip-forward", Key_PageDown, this, SLOT(slotNext()), actionCollection(), "player_next"); + /*** Position toolbar ***/ // Important: we have a max of 1000 instead of 100 for better precision; multiply/divide your percentages by 10 m_position = new TQSlider(0, 1000, 10, 0, TQt::Horizontal, 0); TQToolTip::add(m_position, i18n("Position")); @@ -146,6 +148,13 @@ void MpvPart::initActions() m_playtime->setFocusPolicy(TQ_NoFocus); new KWidgetAction(m_playtime, i18n("Playtime"), 0, 0, 0, actionCollection(), "player_playtime"); + /*** Volume toolbar ***/ + new TDEAction(i18n("&Mute"), "player_mute", Key_U, this, SLOT(slotMute()), actionCollection(), "player_mute"); + + m_volume = new TQSlider(0, 100, 10, 100, TQt::Horizontal, 0); + connect(m_volume, SIGNAL(sliderMoved(int)), this, SLOT(slotSetVolume(int))); + KWidgetAction *volAction = new KWidgetAction(m_volume, i18n("Volume"), 0, 0, 0, actionCollection(), "player_volume"); + resetTime(); } @@ -328,6 +337,16 @@ bool MpvPart::isPaused() { return (bool)result; } +bool MpvPart::isMute() { + if (!m_mpv) return false; + + int result; + if (mpv_get_property(m_mpv, "ao-mute", MPV_FORMAT_FLAG, &result) < 0) { + return false; + } + return (bool)result; +} + void MpvPart::resetTime() { m_playtime->setText("0:00:00"); m_position->setValue(0); @@ -349,15 +368,20 @@ void MpvPart::slotSetSeekingPos(int pos) { } uint MpvPart::volume() const { - return 0; + int64_t value = 0; + mpv_get_property(m_mpv, "ao-volume", MPV_FORMAT_INT64, &value); + return value; } uint MpvPart::position() const { return m_percent; } -void MpvPart::slotSetVolume(uint) { +void MpvPart::slotSetVolume(uint volume) { + if (!m_mpv) return; + int64_t value = (int64_t)volume; + mpv_set_property(m_mpv, "ao-volume", MPV_FORMAT_INT64, &value); } void MpvPart::slotSetPosition(uint position) { @@ -395,7 +419,8 @@ void MpvPart::slotStop() { } void MpvPart::slotMute() { - + int value = isMute() ? 0 : 1; + mpv_set_property(m_mpv, "ao-mute", MPV_FORMAT_FLAG, &value); } #include "libmpv_part.moc" \ No newline at end of file diff --git a/kaffeine/src/player-parts/libmpv-part/libmpv_part.h b/kaffeine/src/player-parts/libmpv-part/libmpv_part.h index cb771f5..e81429f 100644 --- a/kaffeine/src/player-parts/libmpv-part/libmpv_part.h +++ b/kaffeine/src/player-parts/libmpv-part/libmpv_part.h @@ -62,6 +62,7 @@ class MpvPart : public KaffeinePart uint position() const; /* percent */ bool isPlaying(); bool isPaused(); + bool isMute(); bool closeURL(); static TDEAboutData* createAboutData(); @@ -92,6 +93,10 @@ class MpvPart : public KaffeinePart void slotStopSeeking(); void slotSetSeekingPos(int pos); + // Overloaded method with "int" argument + // Used in connection with m_volume::sliderMoved(int) + void slotSetVolume(int vol) { slotSetVolume(static_cast(vol)); } + private: bool initMpv(); void initActions(); @@ -102,6 +107,7 @@ class MpvPart : public KaffeinePart TQWidget *m_player; MpvEventThread *m_eventThread; TQSlider *m_position; + TQSlider *m_volume; TQPushButton *m_playtime; MRL m_mrl; diff --git a/kaffeine/src/player-parts/libmpv-part/libmpv_part.rc b/kaffeine/src/player-parts/libmpv-part/libmpv_part.rc index 21e765c..9373445 100644 --- a/kaffeine/src/player-parts/libmpv-part/libmpv_part.rc +++ b/kaffeine/src/player-parts/libmpv-part/libmpv_part.rc @@ -10,7 +10,7 @@ -Controls Toolbar +Playback Controls Toolbar @@ -22,5 +22,9 @@ - +Volume Controls Toolbar + + + + \ No newline at end of file