remove vlc and replace by basic QMediaPlayer
This commit is contained in:
parent
a567428093
commit
fe7bbed7c1
10 changed files with 303 additions and 470 deletions
|
|
@ -1,72 +0,0 @@
|
|||
#include "MusicController.h"
|
||||
|
||||
#include <QDebug>
|
||||
#include <models/MusicModel.h>
|
||||
#include <VLCQtCore/MediaPlayer.h>
|
||||
#include "EnergySaver.h"
|
||||
|
||||
MusicController::MusicController(QObject *parent) : QObject(parent)
|
||||
{
|
||||
mVlc = new VlcInstance(VlcCommon::args(), this);
|
||||
mModel = new MusicModel(mVlc, this);
|
||||
mPlayer = new VlcMediaListPlayer(mVlc);
|
||||
mVlcAudio = new VlcAudio(mPlayer->mediaPlayer());
|
||||
|
||||
connect(mModel, &MusicModel::navigateTo, this, &MusicController::onNavigationRequest);
|
||||
|
||||
//connect(mModel, &MusicModel::play, mPlayer, &VlcMediaListPlayer::play);
|
||||
connect(mModel, &MusicModel::play, [this](){
|
||||
mVlcAudio->setVolume(mModel->getAudioVolume());
|
||||
mPlayer->play();
|
||||
});
|
||||
connect(mModel, &MusicModel::stop, mPlayer, &VlcMediaListPlayer::stop);
|
||||
connect(mModel, &MusicModel::previous, mPlayer, &VlcMediaListPlayer::previous);
|
||||
connect(mModel, &MusicModel::next, mPlayer, &VlcMediaListPlayer::next);
|
||||
connect(mModel, &MusicModel::pause, mPlayer->mediaPlayer(), &VlcMediaPlayer::pause);
|
||||
connect(mModel, &MusicModel::audioVolumeChanged, mVlcAudio, &VlcAudio::setVolume);
|
||||
|
||||
connect(mPlayer, SIGNAL(nextItemSet(VlcMedia*)), mModel, SLOT(onNextMediaSet(VlcMedia*)));
|
||||
connect(mPlayer->mediaPlayer(), &VlcMediaPlayer::lengthChanged, mModel, &MusicModel::onLengthChanged);
|
||||
connect(mPlayer->mediaPlayer(), &VlcMediaPlayer::timeChanged, mModel, &MusicModel::onTimeChanged);
|
||||
|
||||
// hand over player signals to energy saver in order to determine player activity.
|
||||
connect(mPlayer->mediaPlayer(), &VlcMediaPlayer::timeChanged, EnergySaver::instance(), &EnergySaver::restartTimer);
|
||||
}
|
||||
|
||||
MusicController::~MusicController()
|
||||
{
|
||||
mPlayer->deleteLater();
|
||||
mVlcAudio->deleteLater();
|
||||
}
|
||||
|
||||
void MusicController::initPlayer(NavigationItemModel *item)
|
||||
{
|
||||
if(item != mModel->getCurrentItem()){
|
||||
mPlayer->stop();
|
||||
mModel->init(item);
|
||||
}
|
||||
if(!mIsMediaListSet){
|
||||
mPlayer->setMediaList(mModel->getMedia());
|
||||
mIsMediaListSet = true;
|
||||
}
|
||||
}
|
||||
|
||||
void MusicController::setContext(QQmlContext *context)
|
||||
{
|
||||
mContext = context;
|
||||
setContextProperties();
|
||||
}
|
||||
|
||||
void MusicController::setContextProperties()
|
||||
{
|
||||
if(!mContext) return;
|
||||
mContext->setContextProperty("musicModel", mModel);
|
||||
}
|
||||
|
||||
void MusicController::onNavigationRequest(NavigationItemModel *item)
|
||||
{
|
||||
if(mModel->isPlaying()) {
|
||||
mModel->playPause();
|
||||
}
|
||||
emit navigateTo(item);
|
||||
}
|
||||
|
|
@ -1,52 +0,0 @@
|
|||
#ifndef MUSICCONTROLLER_H
|
||||
#define MUSICCONTROLLER_H
|
||||
|
||||
#include <QObject>
|
||||
#include <QQmlContext>
|
||||
#include <models/NavigationItemModel.h>
|
||||
|
||||
#include <VLCQtCore/Common.h>
|
||||
#include <VLCQtCore/Instance.h>
|
||||
#include <VLCQtCore/MediaListPlayer.h>
|
||||
#include <VLCQtCore/Audio.h>
|
||||
|
||||
class MusicModel;
|
||||
|
||||
class MusicController : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
signals:
|
||||
void navigateTo(NavigationItemModel* item);
|
||||
|
||||
public:
|
||||
MusicController(QObject *parent = Q_NULLPTR);
|
||||
~MusicController();
|
||||
|
||||
void initPlayer(NavigationItemModel* item);
|
||||
|
||||
void setContext(QQmlContext* context);
|
||||
|
||||
private:
|
||||
void setContextProperties();
|
||||
|
||||
QQmlContext* mContext = Q_NULLPTR;
|
||||
|
||||
MusicModel* mModel = Q_NULLPTR;
|
||||
|
||||
VlcInstance* mVlc = Q_NULLPTR;
|
||||
VlcMediaListPlayer* mPlayer = Q_NULLPTR;
|
||||
VlcAudio* mVlcAudio = Q_NULLPTR;
|
||||
|
||||
bool mIsMediaListSet = false;
|
||||
|
||||
private slots:
|
||||
/**
|
||||
* @brief Stop player if necessary and forward signal navigatTo
|
||||
* @param item target of navigation request
|
||||
* @see navigateTo(NavigationItemModel* item);
|
||||
*/
|
||||
void onNavigationRequest(NavigationItemModel* item);
|
||||
};
|
||||
|
||||
#endif // MUSICCONTROLLER_H
|
||||
194
LenaPi/controllers/MusicPlayer.cpp
Normal file
194
LenaPi/controllers/MusicPlayer.cpp
Normal file
|
|
@ -0,0 +1,194 @@
|
|||
#include "MusicPlayer.h"
|
||||
#include <QDir>
|
||||
#include <QMediaPlaylist>
|
||||
|
||||
|
||||
|
||||
MusicPlayer::MusicPlayer(QObject *parent) : QMediaPlayer(parent)
|
||||
{
|
||||
/* nothing */
|
||||
}
|
||||
|
||||
MusicPlayer::~MusicPlayer()
|
||||
{
|
||||
}
|
||||
|
||||
void MusicPlayer::init(NavigationItemModel *item)
|
||||
{
|
||||
if(mCurrentItem == item){
|
||||
return;
|
||||
}
|
||||
mCurrentItem = item;
|
||||
emit currentItemChanged();
|
||||
|
||||
reset();
|
||||
clearMediaList();
|
||||
|
||||
readMedia(mCurrentItem->getPath());
|
||||
}
|
||||
|
||||
void MusicPlayer::navigateBack()
|
||||
{
|
||||
emit navigateTo(mCurrentItem);
|
||||
}
|
||||
|
||||
void MusicPlayer::playPause()
|
||||
{
|
||||
mIsPlaying = !mIsPlaying;
|
||||
emit isPlayingChanged();
|
||||
if(mIsPlaying)
|
||||
play();
|
||||
else
|
||||
pause();
|
||||
}
|
||||
|
||||
void MusicPlayer::stopMusic()
|
||||
{
|
||||
if(mIsPlaying){
|
||||
mIsPlaying = false;
|
||||
emit isPlayingChanged();
|
||||
|
||||
reset();
|
||||
|
||||
emit stop();
|
||||
}
|
||||
}
|
||||
|
||||
void MusicPlayer::playNext()
|
||||
{
|
||||
//emit next();
|
||||
if(!mIsPlaying){
|
||||
mIsPlaying = true;
|
||||
emit isPlayingChanged();
|
||||
}
|
||||
}
|
||||
|
||||
void MusicPlayer::playPrevious()
|
||||
{
|
||||
//emit previous();
|
||||
if(!mIsPlaying){
|
||||
mIsPlaying = true;
|
||||
emit isPlayingChanged();
|
||||
}
|
||||
}
|
||||
|
||||
NavigationItemModel *MusicPlayer::getCurrentItem()
|
||||
{
|
||||
return mCurrentItem;
|
||||
}
|
||||
|
||||
bool MusicPlayer::isPlaying() const
|
||||
{
|
||||
return mIsPlaying;
|
||||
}
|
||||
|
||||
bool MusicPlayer::hasNext() const
|
||||
{
|
||||
return mHasNext;
|
||||
}
|
||||
|
||||
bool MusicPlayer::hasPrevious() const
|
||||
{
|
||||
return mHasPrevious;
|
||||
}
|
||||
|
||||
void MusicPlayer::setAudioVolume(int newVolume)
|
||||
{
|
||||
if(newVolume != mAudioVolume){
|
||||
if(newVolume > 100){
|
||||
mAudioVolume = 100;
|
||||
} else if(newVolume < 0){
|
||||
mAudioVolume = 0;
|
||||
} else {
|
||||
mAudioVolume = newVolume;
|
||||
}
|
||||
emit audioVolumeChanged(mAudioVolume);
|
||||
}
|
||||
}
|
||||
|
||||
double MusicPlayer::getProgress() const
|
||||
{
|
||||
return mCurrentMediaItemProgress;
|
||||
}
|
||||
|
||||
QString MusicPlayer::getMediaTitle() const
|
||||
{
|
||||
return mMediaTitle;
|
||||
}
|
||||
|
||||
QString MusicPlayer::getMediaLength()
|
||||
{
|
||||
return timeToString(mCurrentMediaItemLength);
|
||||
}
|
||||
|
||||
QString MusicPlayer::getTime()
|
||||
{
|
||||
return timeToString(mCurrentTime);
|
||||
}
|
||||
|
||||
void MusicPlayer::onTimeChanged(int time)
|
||||
{
|
||||
mCurrentMediaItemProgress = (double) time / mCurrentMediaItemLength;
|
||||
mCurrentTime = time;
|
||||
emit progressChanged();
|
||||
}
|
||||
|
||||
void MusicPlayer::onLengthChanged(int length)
|
||||
{
|
||||
mCurrentMediaItemLength= length;
|
||||
emit mediaLengthChanged();
|
||||
}
|
||||
|
||||
void MusicPlayer::reset()
|
||||
{
|
||||
mHasNext = false;
|
||||
mHasPrevious = false;
|
||||
emit hasNextChanged();
|
||||
emit hasPreviousChanged();
|
||||
|
||||
mCurrentMediaItemProgress = 0.0;
|
||||
mCurrentTime = 0.0;
|
||||
emit progressChanged();
|
||||
|
||||
mCurrentMediaItemLength = 0.0;
|
||||
emit mediaLengthChanged();
|
||||
}
|
||||
|
||||
void MusicPlayer::clearMediaList()
|
||||
{
|
||||
if(playlist()){
|
||||
playlist()->clear();
|
||||
}
|
||||
}
|
||||
|
||||
void MusicPlayer::readMedia(const QString& path)
|
||||
{
|
||||
auto dir = QDir(path);
|
||||
if(!dir.exists()) return;
|
||||
|
||||
// create playlist if necessary
|
||||
auto playList = playlist();
|
||||
if(!playList){
|
||||
playList = new QMediaPlaylist(this);
|
||||
setPlaylist(playList);
|
||||
}
|
||||
|
||||
// add audio files to playlist
|
||||
dir.setNameFilters({"*.mp3", "*.flac"});
|
||||
auto files = dir.entryInfoList(QDir::Files);
|
||||
for(auto file:files){
|
||||
auto fileName = file.absoluteFilePath();
|
||||
auto fileUrl = QUrl::fromLocalFile(fileName);
|
||||
qDebug() << fileName << fileUrl;
|
||||
playList->addMedia(QMediaContent(fileUrl));
|
||||
}
|
||||
}
|
||||
|
||||
QString MusicPlayer::timeToString(int time)
|
||||
{
|
||||
int sec = time/1000;
|
||||
int min = sec/60;
|
||||
sec = sec-min*60;
|
||||
QString secStr = (sec < 10) ? "0"+QString::number(sec) : QString::number(sec);
|
||||
return QString::number(min) + ":" + secStr;
|
||||
}
|
||||
89
LenaPi/controllers/MusicPlayer.h
Normal file
89
LenaPi/controllers/MusicPlayer.h
Normal file
|
|
@ -0,0 +1,89 @@
|
|||
#ifndef MUSICPLAYER_H
|
||||
#define MUSICPLAYER_H
|
||||
|
||||
#include <QObject>
|
||||
#include <QMediaPlayer>
|
||||
#include <models/NavigationItemModel.h>
|
||||
|
||||
class MusicPlayer : public QMediaPlayer
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
Q_PROPERTY(QObject* pCurrentItem READ getCurrentItem NOTIFY currentItemChanged)
|
||||
Q_PROPERTY(bool pHasNext READ hasNext NOTIFY hasNextChanged)
|
||||
Q_PROPERTY(bool pHasPrevious READ hasPrevious NOTIFY hasPreviousChanged)
|
||||
Q_PROPERTY(bool pIsPlaying READ isPlaying NOTIFY isPlayingChanged)
|
||||
Q_PROPERTY(double pProgress READ getProgress NOTIFY progressChanged)
|
||||
Q_PROPERTY(QString pMediaLength READ getMediaLength NOTIFY mediaLengthChanged)
|
||||
Q_PROPERTY(QString pTime READ getTime NOTIFY progressChanged)
|
||||
Q_PROPERTY(QString pMediaTitle READ getMediaTitle NOTIFY mediaTitleChanged)
|
||||
Q_PROPERTY(int pAudioVolume READ getAudioVolume WRITE setAudioVolume NOTIFY audioVolumeChanged)
|
||||
|
||||
signals:
|
||||
void navigateTo(NavigationItemModel *item);
|
||||
void currentItemChanged();
|
||||
void hasPreviousChanged();
|
||||
void hasNextChanged();
|
||||
void isPlayingChanged();
|
||||
void progressChanged();
|
||||
void mediaLengthChanged();
|
||||
void mediaTitleChanged();
|
||||
void audioVolumeChanged(int newVolume);
|
||||
|
||||
public:
|
||||
MusicPlayer(QObject *parent = Q_NULLPTR);
|
||||
~MusicPlayer();
|
||||
|
||||
void init(NavigationItemModel* item);
|
||||
|
||||
NavigationItemModel *getCurrentItem();
|
||||
|
||||
|
||||
bool isPlaying() const;
|
||||
bool hasNext() const;
|
||||
bool hasPrevious() const;
|
||||
|
||||
inline int getAudioVolume() const { return mAudioVolume; }
|
||||
/**
|
||||
* @brief Set audio volume. Information is transferred to VlcAudio
|
||||
* @param newVolume value between 0 and 100 (audio level in percent)
|
||||
* Ensures that volume is inbetween 0 and 100. If this range is exceeded,
|
||||
* the volume is set to the lowest and highest allowed value, respectively.
|
||||
*/
|
||||
void setAudioVolume(int newVolume);
|
||||
|
||||
double getProgress() const;
|
||||
|
||||
QString getMediaTitle() const;
|
||||
QString getMediaLength();
|
||||
QString getTime();
|
||||
|
||||
public slots:
|
||||
void navigateBack();
|
||||
void playPause();
|
||||
void stopMusic();
|
||||
void playNext();
|
||||
void playPrevious();
|
||||
|
||||
void onTimeChanged(int time);
|
||||
void onLengthChanged(int length);
|
||||
|
||||
private:
|
||||
void reset();
|
||||
void clearMediaList();
|
||||
void readMedia(const QString& path);
|
||||
QString timeToString(int time);
|
||||
|
||||
|
||||
bool mIsPlaying = false;
|
||||
bool mHasNext = false;
|
||||
bool mHasPrevious = false;
|
||||
int mCurrentMediaItemLength = 0;
|
||||
int mCurrentTime = 0;
|
||||
double mCurrentMediaItemProgress = 0;
|
||||
int mAudioVolume{50};
|
||||
QString mMediaTitle = QString("");
|
||||
NavigationItemModel* mCurrentItem = nullptr;
|
||||
};
|
||||
|
||||
#endif // MUSICPLAYER_H
|
||||
|
|
@ -6,15 +6,15 @@
|
|||
#include <models/NavigationItemModel.h>
|
||||
#include <models/NavigationListModel.h>
|
||||
#include <models/UiStateModel.h>
|
||||
#include <controllers/MusicController.h>
|
||||
#include <controllers/MusicPlayer.h>
|
||||
|
||||
NavigationController::NavigationController(QObject *parent) : QObject(parent),
|
||||
mRootItem(new NavigationItemModel(this)),
|
||||
mNavList(new NavigationListModel(this)),
|
||||
mUiState(new UiStateModel(this)),
|
||||
mMusicController(new MusicController(this))
|
||||
mMediaPlayer(new MusicPlayer(this))
|
||||
{
|
||||
connect(mMusicController, &MusicController::navigateTo, [this](NavigationItemModel* item) {
|
||||
connect(mMediaPlayer, &MusicPlayer::navigateTo, [this](NavigationItemModel* item) {
|
||||
mUiState->showNavigation();
|
||||
mNavList->navigateTo(item);
|
||||
});
|
||||
|
|
@ -40,7 +40,6 @@ void NavigationController::setContext(QQmlContext *context)
|
|||
{
|
||||
mContext = context;
|
||||
setContextProperties();
|
||||
mMusicController->setContext(mContext);
|
||||
}
|
||||
|
||||
void NavigationController::setContextProperties()
|
||||
|
|
@ -48,6 +47,7 @@ void NavigationController::setContextProperties()
|
|||
if(!mContext) return;
|
||||
mContext->setContextProperty("navigationList", mNavList);
|
||||
mContext->setContextProperty("uiStateModel", mUiState);
|
||||
mContext->setContextProperty("musicModel", mMediaPlayer);
|
||||
}
|
||||
|
||||
void NavigationController::add(const QString &path, NavigationItemModel *parentItem)
|
||||
|
|
@ -99,7 +99,7 @@ void NavigationController::onNavigationRequest()
|
|||
if(item->hasChildren())
|
||||
mNavList->setModelItems(item->getChildren());
|
||||
else {
|
||||
mMusicController->initPlayer(item);
|
||||
mMediaPlayer->init(item);
|
||||
mUiState->showMusicPlayer();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@
|
|||
class NavigationItemModel;
|
||||
class NavigationListModel;
|
||||
class UiStateModel;
|
||||
class MusicController;
|
||||
class MusicPlayer;
|
||||
|
||||
/**
|
||||
* @brief Main controller controlling ui state, navigation and music player.
|
||||
|
|
@ -59,7 +59,7 @@ private:
|
|||
NavigationListModel* mNavList;
|
||||
|
||||
UiStateModel* mUiState;
|
||||
MusicController* mMusicController;
|
||||
MusicPlayer* mMediaPlayer;
|
||||
|
||||
QString mRootPath = ".";
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue