From 4dd3070fa50882b7ec7a26f7d0994c064c6b29d6 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Fri, 16 Oct 2015 21:27:21 +0300 Subject: [PATCH] Implement seeking and play/pause controls in the remote --- remote/qml/pages/PlaybackPage.qml | 23 ++++++++++++++++++- remote/source/streamcontrolitem.cpp | 30 +++++++++++++++++++++++++ remote/source/streamcontrolitem.h | 18 ++++++++++++++- remote/source/xinemacontrol.cpp | 35 ++++++++++++++++++++++++++++- remote/source/xinemacontrol.h | 16 +++++++++++++ 5 files changed, 119 insertions(+), 3 deletions(-) diff --git a/remote/qml/pages/PlaybackPage.qml b/remote/qml/pages/PlaybackPage.qml index 4a487a4..345960a 100644 --- a/remote/qml/pages/PlaybackPage.qml +++ b/remote/qml/pages/PlaybackPage.qml @@ -49,6 +49,27 @@ Page return str+secs; } + + onDownChanged: + { + if(!down) + streamControl.position = value; + } + } + + IconButton + { + id: playPauseButton + anchors.horizontalCenter: parent.horizontalCenter + property string action: (streamControl.playbackState==StreamControl.Playing ? "pause" : "play") + icon.source: "image://theme/icon-l-"+action + onPressed: + { + if(action=="play") + streamControl.playbackState = StreamControl.Playing; + else if(action=="pause") + streamControl.playbackState = StreamControl.Paused; + } } } @@ -59,7 +80,7 @@ Page onPositionChanged: { if(!slider.down) - slider.value = streamControl.position + slider.value = position; } } } diff --git a/remote/source/streamcontrolitem.cpp b/remote/source/streamcontrolitem.cpp index a225d52..b7624df 100644 --- a/remote/source/streamcontrolitem.cpp +++ b/remote/source/streamcontrolitem.cpp @@ -15,6 +15,7 @@ void StreamControlItem::set_control(XinemaControlItem *c) if(control) { XinemaControl &xc = control->get_control(); + connect(&xc, &XinemaControl::playback_state_changed, this, &StreamControlItem::playback_state_changed); connect(&xc, &XinemaControl::title_changed, this, &StreamControlItem::title_changed); connect(&xc, &XinemaControl::duration_changed, this, &StreamControlItem::duration_changed); connect(&xc, &XinemaControl::position_changed, this, &StreamControlItem::position_changed); @@ -24,12 +25,35 @@ void StreamControlItem::set_control(XinemaControlItem *c) if(control) { + emit playback_state_changed(); emit title_changed(); emit duration_changed(); emit position_changed(); } } +StreamControlItem::PlaybackState StreamControlItem::get_playback_state() const +{ + if(!control) + return Stopped; + + return static_cast(control->get_control().get_playback_state()); +} + +void StreamControlItem::set_playback_state(PlaybackState state) +{ + if(!control) + return; + + XinemaControl &xc = control->get_control(); + if(state==Stopped) + xc.stop(); + else if(state==Paused) + xc.pause(); + else if(state==Playing) + xc.play(); +} + QString StreamControlItem::get_title() const { if(!control) @@ -46,6 +70,12 @@ float StreamControlItem::get_duration() const return control->get_control().get_duration(); } +void StreamControlItem::set_position(float pos) +{ + if(control) + control->get_control().seek(pos); +} + float StreamControlItem::get_position() const { if(!control) diff --git a/remote/source/streamcontrolitem.h b/remote/source/streamcontrolitem.h index 5b9b2bc..cb08836 100644 --- a/remote/source/streamcontrolitem.h +++ b/remote/source/streamcontrolitem.h @@ -2,6 +2,7 @@ #define STREAMCONTROLITEM_H_ #include +#include "xinemacontrol.h" class XinemaControlItem; @@ -10,9 +11,20 @@ class StreamControlItem: public QQuickItem Q_OBJECT Q_PROPERTY(XinemaControlItem *control READ get_control WRITE set_control NOTIFY control_changed) + Q_PROPERTY(PlaybackState playbackState READ get_playback_state WRITE set_playback_state NOTIFY playback_state_changed) Q_PROPERTY(QString title READ get_title NOTIFY title_changed) Q_PROPERTY(float duration READ get_duration NOTIFY duration_changed) - Q_PROPERTY(float position READ get_position NOTIFY position_changed) + Q_PROPERTY(float position READ get_position WRITE set_position NOTIFY position_changed) + + Q_ENUMS(PlaybackState) + +public: + enum PlaybackState + { + Stopped = XinemaControl::STOPPED, + Paused = XinemaControl::PAUSED, + Playing = XinemaControl::PLAYING + }; private: XinemaControlItem *control; @@ -23,12 +35,16 @@ public: void set_control(XinemaControlItem *); XinemaControlItem *get_control() const { return control; } + PlaybackState get_playback_state() const; + void set_playback_state(PlaybackState); QString get_title() const; float get_duration() const; + void set_position(float); float get_position() const; signals: void control_changed(); + void playback_state_changed(); void title_changed(); void duration_changed(); void position_changed(); diff --git a/remote/source/xinemacontrol.cpp b/remote/source/xinemacontrol.cpp index 63399a6..db349e7 100644 --- a/remote/source/xinemacontrol.cpp +++ b/remote/source/xinemacontrol.cpp @@ -1,6 +1,9 @@ #include "xinemacontrol.h" -XinemaControl::XinemaControl() +XinemaControl::XinemaControl(): + playback_state(STOPPED), + duration(0.0f), + position(0.0f) { QObject::connect(&socket, &QAbstractSocket::connected, this, &XinemaControl::connected); QObject::connect(&socket, &QAbstractSocket::disconnected, this, &XinemaControl::disconnected); @@ -33,6 +36,26 @@ void XinemaControl::play_file(const QString &fn) send_request("play_file "+fn); } +void XinemaControl::play() +{ + send_request("play"); +} + +void XinemaControl::seek(float time) +{ + send_request(QString("seek %1").arg(time)); +} + +void XinemaControl::pause() +{ + send_request("pause"); +} + +void XinemaControl::stop() +{ + send_request("stop"); +} + void XinemaControl::send_request(const QString &req) { socket.write(req.toUtf8()); @@ -77,6 +100,16 @@ void XinemaControl::process_reply(const QString &reply) emit subdirectory_added(args); else if(keyword=="file") emit file_added(args); + else if(keyword=="state") + { + if(args=="STOPPED") + playback_state = STOPPED; + else if(args=="PAUSED") + playback_state = PAUSED; + else if(args=="PLAYING") + playback_state = PLAYING; + emit playback_state_changed(playback_state); + } else if(keyword=="title") { title = args; diff --git a/remote/source/xinemacontrol.h b/remote/source/xinemacontrol.h index dc7ad8f..5693554 100644 --- a/remote/source/xinemacontrol.h +++ b/remote/source/xinemacontrol.h @@ -8,9 +8,18 @@ class XinemaControl: public QObject { Q_OBJECT +public: + enum PlaybackState + { + STOPPED, + PAUSED, + PLAYING + }; + private: QTcpSocket socket; QByteArray buffer; + PlaybackState playback_state; QString title; float duration; float position; @@ -24,16 +33,23 @@ public: void list_directory(const QString &); void play_file(const QString &); + PlaybackState get_playback_state() const { return playback_state; } const QString &get_title() const { return title; } float get_duration() const { return duration; } float get_position() const { return position; } + void play(); + void seek(float); + void pause(); + void stop(); + signals: void connected(); void disconnected(); void directory_started(const QString &); void file_added(const QString &); void subdirectory_added(const QString &); + void playback_state_changed(PlaybackState); void title_changed(const QString &); void duration_changed(float); void position_changed(float); -- 2.45.2