M module-apps/application-music-player/ApplicationMusicPlayer.cpp => module-apps/application-music-player/ApplicationMusicPlayer.cpp +38 -1
@@ 27,6 27,11 @@ namespace app
handlePlayResponse(msg);
return sys::MessageNone{};
});
+
+ connect(typeid(AudioStopResponse), [&](sys::Message *msg) {
+ handleStopResponse(msg);
+ return sys::MessageNone{};
+ });
}
void ApplicationMusicPlayer::handlePlayResponse(sys::Message *msg)
@@ 35,6 40,16 @@ namespace app
currentFileToken = startResponse->token;
}
+ void ApplicationMusicPlayer::handleStopResponse(sys::Message *msg)
+ {
+ auto stopResponse = static_cast<AudioStopResponse *>(msg);
+
+ if (stopResponse->token == currentFileToken.value()) {
+ currentFileToken.reset();
+ isTrackPlaying = false;
+ }
+ }
+
sys::MessagePointer ApplicationMusicPlayer::DataReceivedHandler(sys::DataMessage *msgl,
[[maybe_unused]] sys::ResponseMessage *resp)
{
@@ 105,12 120,15 @@ namespace app
bool ApplicationMusicPlayer::play(const std::string &fileName)
{
AudioServiceAPI::PlaybackStart(this, audio::PlaybackType::Multimedia, fileName);
+ isTrackPlaying = true;
+
return true;
}
bool ApplicationMusicPlayer::pause()
{
if (currentFileToken) {
+ isTrackPlaying = false;
return AudioServiceAPI::Pause(this, currentFileToken.value());
}
return false;
@@ 119,7 137,7 @@ namespace app
bool ApplicationMusicPlayer::resume()
{
if (currentFileToken) {
-
+ isTrackPlaying = true;
return AudioServiceAPI::Resume(this, currentFileToken.value());
}
return false;
@@ 130,4 148,23 @@ namespace app
return AudioServiceAPI::GetFileTags(this, filePath);
}
+ bool ApplicationMusicPlayer::stop()
+ {
+ if (currentFileToken) {
+ isTrackPlaying = false;
+ return AudioServiceAPI::Stop(this, currentFileToken.value());
+ }
+ return false;
+ }
+
+ void ApplicationMusicPlayer::togglePlaying()
+ {
+ if (isTrackPlaying) {
+ pause();
+ }
+ else {
+ resume();
+ }
+ }
+
} /* namespace app */
M module-apps/application-music-player/ApplicationMusicPlayer.hpp => module-apps/application-music-player/ApplicationMusicPlayer.hpp +7 -0
@@ 5,6 5,7 @@
#include <Application.hpp>
#include <Audio/decoder/Decoder.hpp>
+#include <atomic>
namespace gui
{
@@ 28,6 29,8 @@ namespace app
{
std::optional<audio::Token> currentFileToken;
+ std::atomic_bool isTrackPlaying = false;
+
public:
explicit ApplicationMusicPlayer(std::string name = name_music_player,
std::string parent = {},
@@ 52,9 55,13 @@ namespace app
bool pause();
bool resume();
bool stop();
+ void startPlaying();
+ void togglePlaying();
+
std::optional<audio::Tags> getFileTags(const std::string &filePath);
void handlePlayResponse(sys::Message *msg);
+ void handleStopResponse(sys::Message *msg);
};
template <> struct ManifestTraits<ApplicationMusicPlayer>
M module-apps/application-music-player/windows/MusicPlayerEmptyWindow.cpp => module-apps/application-music-player/windows/MusicPlayerEmptyWindow.cpp +17 -5
@@ 31,6 31,7 @@ namespace gui
AppWindow::buildInterface();
bottomBar->setText(BottomBar::Side::LEFT, utils::translate("app_music_player_music_library"));
+ bottomBar->setText(BottomBar::Side::CENTER, utils::translate("app_music_player_play"));
bottomBar->setText(BottomBar::Side::RIGHT, utils::translate("app_music_player_quit"));
img = new gui::Image(this, noteImg::x, noteImg::y, "note");
@@ 65,15 66,26 @@ namespace gui
return true;
}
- if (!inputEvent.isShortPress()) {
- return false;
- }
-
- if (inputEvent.is(gui::KeyCode::KEY_LF)) {
+ if (inputEvent.is(gui::KeyCode::KEY_LF) && inputEvent.isShortPress()) {
application->switchWindow(gui::name::window::all_songs_window);
return true;
}
+ if (inputEvent.is(gui::KeyCode::KEY_ENTER) || inputEvent.is(gui::KeyCode::HEADSET_OK)) {
+ if (inputEvent.isLongPress()) {
+ auto app = dynamic_cast<app::ApplicationMusicPlayer *>(application);
+ assert(app);
+ app->stop();
+ return true;
+ }
+ else if (inputEvent.isShortPress()) {
+ auto app = dynamic_cast<app::ApplicationMusicPlayer *>(application);
+ assert(app);
+ app->togglePlaying();
+ return true;
+ }
+ }
+
return false;
}