Permalink
Browse files

MythMusic: allow the music player to be controlled by sending it Myth…

…Events.

Currently you can start, stop and pause the player, get/set the volume, play a
file, url or track id and get the metadata of the currently playing track.
Other commands will be added as they are needed.

Shortly I will add some code to the web browser widget that will allow the
music player to be controlled using JavaScript.

It would be trivial to update the network control to allow the player to be
controlled using that as well if anyone is interested.
  • Loading branch information...
1 parent adcaa6f commit 747c374e143474a148c38373c484af50fe6e2f23 Paul Harrison committed Dec 12, 2010
Showing with 84 additions and 2 deletions.
  1. +84 −2 mythplugins/mythmusic/mythmusic/musicplayer.cpp
View
86 mythplugins/mythmusic/mythmusic/musicplayer.cpp
@@ -468,8 +468,7 @@ void MusicPlayer::customEvent(QEvent *event)
stop(true);
}
}
-
- if (me->Message().left(12) == "PLAYBACK_END")
+ else if (me->Message().left(12) == "PLAYBACK_END")
{
if (m_wasPlaying)
{
@@ -486,6 +485,89 @@ void MusicPlayer::customEvent(QEvent *event)
m_wasPlaying = false;
}
}
+ else if (me->Message().left(13) == "MUSIC_COMMAND")
+ {
+ QStringList list = me->Message().split(' ');
+
+ if (list.size() >= 2)
+ {
+ if (list[1] == "PLAY")
+ play();
+ else if (list[1] == "STOP")
+ stop();
+ else if (list[1] == "PAUSE")
+ pause();
+ else if (list[1] == "SET_VOLUME")
+ {
+ if (list.size() >= 3)
+ {
+ int volume = list[2].toInt();
+ if (volume >= 0 && volume <= 100)
+ setVolume(volume);
+ }
+ }
+ else if (list[1] == "GET_VOLUME")
+ {
+ QString message = QString("MUSIC_CONTROL ANSWER %1").arg(getVolume());
+ MythEvent me(message);
+ gCoreContext->dispatch(me);
+ }
+ else if (list[1] == "PLAY_FILE")
+ {
+ int start = me->Message().indexOf("'");
+ int end = me->Message().lastIndexOf("'");
+
+ if (start != -1 && end != -1 && start != end)
+ {
+ QString filename = me->Message().mid(start + 1, end - start - 1);
+ Metadata mdata;
+ mdata.setFilename(filename);
+ playFile(mdata);
+ }
+ else
+ VERBOSE(VB_IMPORTANT, QString("MusicPlayer: got invalid MUSIC_COMMAND PLAY_FILE - %1").arg(me->Message()));
+ }
+ else if (list[1] == "PLAY_URL")
+ {
+ if (list.size() == 3)
+ {
+ QString filename = list[2];
+ Metadata mdata;
+ mdata.setFilename(filename);
+ playFile(mdata);
+ }
+ else
+ VERBOSE(VB_IMPORTANT, QString("MusicPlayer: got invalid MUSIC_COMMAND PLAY_URL - %1").arg(me->Message()));
+ }
+ else if (list[1] == "PLAY_TRACK")
+ {
+ if (list.size() == 3)
+ {
+ int trackID = list[2].toInt();
+ Metadata *mdata = gMusicData->all_music->getMetadata(trackID);
+ if (mdata)
+ playFile(*mdata);
+ }
+ else
+ VERBOSE(VB_IMPORTANT, QString("MusicPlayer: got invalid MUSIC_COMMAND PLAY_TRACK - %1").arg(me->Message()));
+ }
+ else if (list[1] == "GET_METADATA")
+ {
+ QString mdataStr;
+ Metadata *mdata = getCurrentMetadata();
+ if (mdata)
+ mdataStr = QString("%1 by %2 from %3").arg(mdata->Title()).arg(mdata->Artist()).arg(mdata->Album());
+ else
+ mdataStr = "Unknown Track";
+
+ QString message = QString("MUSIC_CONTROL ANSWER %1").arg(mdataStr);
+ MythEvent me(message);
+ gCoreContext->dispatch(me);
+ }
+ }
+ else
+ VERBOSE(VB_IMPORTANT, QString("MusicPlayer: got unknown/invalid MUSIC_COMMAND - %1").arg(me->Message()));
+ }
}
if (m_isAutoplay)

0 comments on commit 747c374

Please sign in to comment.