]> git.tdb.fi Git - xinema.git/commitdiff
Implement seeking and play/pause controls in the remote
authorMikko Rasa <tdb@tdb.fi>
Fri, 16 Oct 2015 18:27:21 +0000 (21:27 +0300)
committerMikko Rasa <tdb@tdb.fi>
Fri, 16 Oct 2015 18:27:21 +0000 (21:27 +0300)
remote/qml/pages/PlaybackPage.qml
remote/source/streamcontrolitem.cpp
remote/source/streamcontrolitem.h
remote/source/xinemacontrol.cpp
remote/source/xinemacontrol.h

index 4a487a4f8cd9b80add977b1ad9b3e52931ef7fce..345960a6c80de994d15c06dc017a865a895804fe 100644 (file)
@@ -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;
                }
        }
 }
index a225d52494f2366c63494c7015de75706a48846b..b7624df813d6c5f45cbe24cf99171fdeb84320f5 100644 (file)
@@ -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<PlaybackState>(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)
index 5b9b2bc0bac7111ce66ef1802468e7edee4e16e4..cb08836c69ca0a21327832057069202b5405020d 100644 (file)
@@ -2,6 +2,7 @@
 #define STREAMCONTROLITEM_H_
 
 #include <QQuickItem>
+#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();
index 63399a6e4a0bcde7bfd9861b2193c9db9570a44d..db349e73685a7d0472a9a9b7dd9cd0dba860389b 100644 (file)
@@ -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;
index dc7ad8fcd20cbdce0d439013bbe5f359e6da3aa2..5693554836b88f7e0639810ea57f08f272affe7d 100644 (file)
@@ -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);