Commit 73ad7e6f authored by kempe's avatar kempe

Moved audioplayer code to c++

parent acedc1f0
......@@ -14,8 +14,11 @@ TARGET = harbour-received
CONFIG += sailfishapp
QT += multimedia
SOURCES += src/harbour-received.cpp \
src/settings.cpp
src/settings.cpp \
src/audioplayer.cpp
OTHER_FILES += qml/harbour-received.qml \
qml/cover/CoverPage.qml \
......@@ -46,7 +49,8 @@ CONFIG += sailfishapp_i18n
TRANSLATIONS += translations/harbour-received-sv.ts
HEADERS += \
src/settings.h
src/settings.h \
src/audioplayer.h
DISTFILES += \
qml/components/js/Rad.js \
......
import QtQuick 2.6
import Sailfish.Silica 1.0
import it.kempe.AudioPlayer 1.0
import "../js/Storage.js" as DB
import "../js/Favorites.js" as FavoritesUtils
import "../js/Utils.js" as Utils
......@@ -15,8 +16,9 @@ DockedAudioPlayerForm {
// Property assignment
isLoading: audio.isLoading
isPlaying: audio.isPlaying
isPlaying: AudioPlayer.isPlaying
showProgressBar: AudioPlayer.isLoading || AudioPlayer.isBuffering
bufferProgress: AudioPlayer.bufferProgress
// Triggers
......@@ -24,7 +26,7 @@ DockedAudioPlayerForm {
buttonStar.onClicked: player.isFavorite = FavoritesUtils.toogleFavorite(stationData)
buttonPrevious.onClicked: playPrev()
buttonPlay.onClicked: isPlaying ? stop() : resume()
buttonPlay.onClicked: AudioPlayer.togglePlayback();
buttonNext.onClicked: playNext()
sleepTimer.onSleepTriggered: stop();
......@@ -56,9 +58,7 @@ DockedAudioPlayerForm {
window.stationIcon = stationData.stationLogo;
stop();
audio.source = stationData.url;
audio.play();
AudioPlayer.loadUrl(stationData.url)
}
function playNext() {
......@@ -72,13 +72,4 @@ DockedAudioPlayerForm {
console.debug("Playing prev favorite", JSON.stringify(prevStation));
play(prevStation);
}
function stop() {
audio.stopAndReset();
}
function resume() {
audio.source = stationData.url;
audio.play();
}
}
......@@ -6,7 +6,6 @@ DockedPanel {
id: player
property alias opener: opener
property alias audio: audio
property alias buttonStar: controls.buttonStar
property alias buttonPrevious: controls.buttonPrevious
......@@ -17,9 +16,10 @@ DockedPanel {
property alias cancelSleepTimer: cancelSleepTimer
property alias setSleepTimer: setSleepTimer
property bool isLoading: false
property bool showProgressBar: false
property bool isPlaying: false
property bool isFavorite: false
property int bufferProgress: 0
property var stationData
......@@ -32,10 +32,6 @@ DockedPanel {
id: sleepTimer
}
AudioPlayer {
id: audio
}
Item {
anchors.fill: parent
......@@ -81,9 +77,9 @@ DockedPanel {
ProgressBar {
id: progressBar
value: audio.bufferProgress
value: bufferProgress
width: parent.width
visible: isLoading
visible: showProgressBar
}
PlayerControlsForm {
......@@ -91,7 +87,7 @@ DockedPanel {
anchors.horizontalCenter: parent.horizontalCenter
width: parent.width
visible: !player.isLoading
visible: !showProgressBar
isPlaying: player.isPlaying
isStared: player.isFavorite
}
......
import QtQuick 2.6
import Sailfish.Silica 1.0
import it.kempe.AudioPlayer 1.0
CoverBackground {
Image {
......@@ -15,14 +16,12 @@ CoverBackground {
id: coverAction
CoverAction {
iconSource: player.isPlaying ? "image://theme/icon-cover-pause" : "image://theme/icon-cover-play"
iconSource: AudioPlayer.isPlaying ? "image://theme/icon-cover-pause" : "image://theme/icon-cover-play"
onTriggered: {
if(!player.stationData)
player.playNext()
else if(!player.isPlaying)
player.resume()
else
player.stop()
AudioPlayer.togglePlayback()
}
}
......
#include "audioplayer.h"
AudioPlayer::AudioPlayer(QObject *parent) : QObject(parent)
{
m_current_url = "";
m_buffer_progress = 0;
m_state = QMediaPlayer::StoppedState;
m_status = QMediaPlayer::NoMedia;
m_player = new QMediaPlayer(this);
connect(m_player, &QMediaPlayer::stateChanged, this, &AudioPlayer::stateChanged);
connect(m_player, &QMediaPlayer::mediaStatusChanged, this, &AudioPlayer::statusChanged);
connect(m_player, &QMediaPlayer::bufferStatusChanged, this, &AudioPlayer::bufferStatusChanged);
}
AudioPlayer::~AudioPlayer()
{
}
void AudioPlayer::loadUrl(QString url)
{
m_player->stop();
m_current_url = QUrl(url);
togglePlayback();
}
void AudioPlayer::togglePlayback()
{
switch(m_player->state()) {
case QMediaPlayer::PausedState:
case QMediaPlayer::StoppedState:
m_player->setMedia(m_current_url);
m_player->play();
break;
case QMediaPlayer::PlayingState:
m_player->stop();
m_player->setMedia(QMediaContent());
break;
default:
break;
}
}
void AudioPlayer::stateChanged(QMediaPlayer::State state)
{
m_state = state;
emit playbackStateChanged();
}
void AudioPlayer::statusChanged(QMediaPlayer::MediaStatus status)
{
m_status = status;
emit playbackStatusChanged();
}
void AudioPlayer::bufferStatusChanged(const int progress)
{
m_buffer_progress = progress / 100.0;
emit bufferProgressChanged();
}
#ifndef AUDIOPLAYER_H
#define AUDIOPLAYER_H
#include <QObject>
#include <QQmlEngine>
#include <QJSEngine>
#include <QMediaPlayer>
class AudioPlayer : public QObject {
Q_OBJECT
Q_PROPERTY(bool isPlaying READ isPlaying NOTIFY playbackStateChanged)
Q_PROPERTY(bool isLoading READ isLoading NOTIFY playbackStatusChanged)
Q_PROPERTY(bool isBuffering READ isBuffering NOTIFY playbackStatusChanged)
Q_PROPERTY(double bufferProgress READ bufferProgress NOTIFY bufferProgressChanged)
public:
explicit AudioPlayer(QObject *parent = 0);
~AudioPlayer();
bool isPlaying() const { return m_state == QMediaPlayer::PlayingState; }
bool isLoading() const { return m_status == QMediaPlayer::LoadingMedia; }
bool isBuffering() const { return m_status == QMediaPlayer::BufferingMedia; }
double bufferProgress() const { return m_buffer_progress; }
signals:
void playbackStateChanged();
void playbackStatusChanged();
void bufferProgressChanged();
public slots:
void loadUrl(QString url);
void togglePlayback();
private slots:
void stateChanged(QMediaPlayer::State);
void statusChanged(QMediaPlayer::MediaStatus);
void bufferStatusChanged(const int);
private:
QMediaPlayer *m_player;
QUrl m_current_url;
QMediaPlayer::State m_state;
QMediaPlayer::MediaStatus m_status;
double m_buffer_progress;
};
static QObject *audioPlayerProvider(QQmlEngine *engine, QJSEngine *scriptEngine)
{
Q_UNUSED(engine)
Q_UNUSED(scriptEngine)
AudioPlayer *audioPlayer = new AudioPlayer();
return audioPlayer;
}
#endif // AUDIOPLAYER_H
......@@ -36,10 +36,12 @@
#include <QQuickView>
#include <QQmlContext>
#include <QGuiApplication>
#include <QtQml>
#include <sailfishapp.h>
#include "src/settings.h"
#include "src/audioplayer.h"
int main(int argc, char *argv[])
......@@ -48,8 +50,9 @@ int main(int argc, char *argv[])
QGuiApplication *app = SailfishApp::application(argc, argv);
QQuickView *view = SailfishApp::createView();
Settings settings;
qmlRegisterSingletonType<AudioPlayer>("it.kempe.AudioPlayer", 1, 0, "AudioPlayer", audioPlayerProvider);
Settings settings;
view->rootContext()->setContextProperty("settings", &settings);
view->setSource(SailfishApp::pathTo("qml/harbour-received.qml"));
view->showFullScreen();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment