Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Allows a web page viewed in MythUIWebBrowser to control the music pla…

…yer.

This adds a MusicPlayer java script object that can control the music player.
The main use for this will be in MythNetVision to allow it to add audio only
feeds.

The object name is 'MusicPlayer' and currently has these functions.

Play()             - Starts the music player.
Pause()            - toggles the pause state of the music player.
Stop()             - stops the music player.
SetVolume(int)     - set the volume, takes a number between 0 and 100
GetVolume()        - returns the volume
GetMetadata()      - returns a string containing the current track details.
PlayFile(filename) - starts playing filename which should be a local file.
PlayURL(url)       - starts playing url which can be an http url.
PlayTrack(trackid) - starts playing the track from the database with id trackid.
  • Loading branch information...
commit d51fd096be54a809d053811d60da4a2655390f2a 1 parent 747c374
Paul Harrison authored
View
139 mythtv/libs/libmythui/mythuiwebbrowser.cpp
@@ -31,6 +31,137 @@
#include "mythuihelper.h"
#include "mythcorecontext.h"
+
+/**
+ * @class BrowserApi
+ * @brief Adds a JavaScript object
+ * \note allows the browser to control the music player
+ */
+BrowserApi::BrowserApi(QObject *parent) : QObject(parent)
+{
+ gCoreContext->addListener(this);
+}
+
+void BrowserApi::setWebView(QWebView *view)
+{
+ QWebPage *page = view->page();
+ m_frame = page->mainFrame();
+
+ attachObject();
+ connect(m_frame, SIGNAL(javaScriptWindowObjectCleared()), this, SLOT(attachObject()));
+}
+
+void BrowserApi::attachObject(void)
+{
+ m_frame->addToJavaScriptWindowObject(QString("MusicPlayer"), this);
+}
+
+int BrowserApi::Play(void)
+{
+ MythEvent me(QString("MUSIC_COMMAND PLAY"));
+ gCoreContext->dispatch(me);
+}
+
+int BrowserApi::Stop(void)
+{
+ MythEvent me(QString("MUSIC_COMMAND STOP"));
+ gCoreContext->dispatch(me);
+}
+
+int BrowserApi::Pause(void)
+{
+ MythEvent me(QString("MUSIC_COMMAND PAUSE"));
+ gCoreContext->dispatch(me);
+}
+
+int BrowserApi::SetVolume(int volumn)
+{
+ MythEvent me(QString("MUSIC_COMMAND SET_VOLUME %1").arg(volumn));
+ gCoreContext->dispatch(me);
+}
+
+int BrowserApi::GetVolume(void)
+{
+ m_gotAnswer = false;
+
+ MythEvent me(QString("MUSIC_COMMAND GET_VOLUME"));
+ gCoreContext->dispatch(me);
+
+ QTime timer;
+ timer.start();
+ while (timer.elapsed() < 2000 && !m_gotAnswer)
+ {
+ qApp->processEvents();
+ usleep(10000);
+ }
+
+ if (m_gotAnswer)
+ return m_answer.toInt();
+
+ return -1;
+}
+
+int BrowserApi::PlayFile(QString filename)
+{
+ MythEvent me(QString("MUSIC_COMMAND PLAY_FILE '%1'").arg(filename));
+ gCoreContext->dispatch(me);
+}
+
+int BrowserApi::PlayTrack(int trackID)
+{
+ MythEvent me(QString("MUSIC_COMMAND PLAY_TRACK %1").arg(trackID));
+ gCoreContext->dispatch(me);
+}
+
+int BrowserApi::PlayURL(QString url)
+{
+ MythEvent me(QString("MUSIC_COMMAND PLAY_URL %1").arg(url));
+ gCoreContext->dispatch(me);
+}
+
+QString BrowserApi::GetMetadata(void)
+{
+ m_gotAnswer = false;
+
+ MythEvent me(QString("MUSIC_COMMAND GET_METADATA"));
+ gCoreContext->dispatch(me);
+
+ QTime timer;
+ timer.start();
+ while (timer.elapsed() < 2000 && !m_gotAnswer)
+ {
+ qApp->processEvents();
+ usleep(10000);
+ }
+
+ if (m_gotAnswer)
+ return m_answer;
+
+ return QString("unknown");
+}
+
+void BrowserApi::customEvent(QEvent *e)
+{
+ if ((MythEvent::Type)(e->type()) == MythEvent::MythEventMessage)
+ {
+ MythEvent *me = (MythEvent *)e;
+ QString message = me->Message();
+
+ if (message.left(13) != "MUSIC_CONTROL")
+ return;
+
+ QStringList tokens = message.simplified().split(" ");
+ if ((tokens.size() >= 3) &&
+ (tokens[1] == "ANSWER"))
+ {
+ m_answer = tokens[2];
+ for (int i = 3; i < tokens.size(); i++)
+ m_answer += QString(" ") + tokens[i];
+ m_gotAnswer = true;
+ }
+ }
+}
+
/**
* @class MythWebView
* @brief Subclass of QWebView
@@ -45,6 +176,14 @@ MythWebView::MythWebView(QWidget *parent, MythUIWebBrowser *parentBrowser)
this, SLOT(handleUnsupportedContent(QNetworkReply *)));
page()->setForwardUnsupportedContent(true);
+
+ m_api = new BrowserApi(this);
+ m_api->setWebView(this);
+}
+
+MythWebView::~MythWebView(void)
+{
+ delete m_api;
}
/**
View
36 mythtv/libs/libmythui/mythuiwebbrowser.h
@@ -17,12 +17,47 @@
class MythUIWebBrowser;
+class BrowserApi : public QObject
+{
+ Q_OBJECT
+ public:
+ BrowserApi(QObject *parent);
+
+ void setWebView(QWebView *view);
+
+ public slots:
+ int Play(void);
+ int Stop(void);
+ int Pause(void);
+
+ int SetVolume(int volumn);
+ int GetVolume(void);
+
+ int PlayFile(QString filename);
+ int PlayTrack(int trackID);
+ int PlayURL(QString url);
+
+ QString GetMetadata(void);
+
+ private slots:
+ void attachObject();
+
+ private:
+ void customEvent(QEvent *e);
+
+ QWebFrame *m_frame;
+
+ bool m_gotAnswer;
+ QString m_answer;
+};
+
class MythWebView : public QWebView
{
Q_OBJECT
public:
MythWebView(QWidget *parent, MythUIWebBrowser *parentBrowser);
+ ~MythWebView(void);
virtual void keyPressEvent(QKeyEvent *event);
@@ -30,6 +65,7 @@ class MythWebView : public QWebView
void handleUnsupportedContent(QNetworkReply *reply);
private:
MythUIWebBrowser *m_parentBrowser;
+ BrowserApi *m_api;
};
/**
Please sign in to comment.
Something went wrong with that request. Please try again.