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,218 +0,0 @@
|
|||
#include "MusicModel.h"
|
||||
#include <QDir>
|
||||
#include <QDebug>
|
||||
#include <VLCQtCore/Media.h>
|
||||
|
||||
MusicModel::MusicModel(VlcInstance* instance, QObject *parent) : QObject(parent),
|
||||
mVlc(instance), mMedia(new VlcMediaList(instance))
|
||||
{
|
||||
/* nothing */
|
||||
}
|
||||
|
||||
MusicModel::~MusicModel()
|
||||
{
|
||||
// do not delete! will cause segmentation fault
|
||||
//if(mMedia)
|
||||
// mMedia->deleteLater();
|
||||
}
|
||||
|
||||
void MusicModel::init(NavigationItemModel *item)
|
||||
{
|
||||
if(mCurrentItem == item){
|
||||
return;
|
||||
}
|
||||
mCurrentItem = item;
|
||||
emit currentItemChanged();
|
||||
|
||||
reset();
|
||||
clearMediaList();
|
||||
|
||||
readMedia(mCurrentItem->getPath());
|
||||
|
||||
setMediaTitle(mMedia->at(0));
|
||||
}
|
||||
|
||||
void MusicModel::navigateBack()
|
||||
{
|
||||
emit navigateTo(mCurrentItem);
|
||||
}
|
||||
|
||||
void MusicModel::playPause()
|
||||
{
|
||||
mIsPlaying = !mIsPlaying;
|
||||
emit isPlayingChanged();
|
||||
if(mIsPlaying)
|
||||
emit play();
|
||||
else
|
||||
emit pause();
|
||||
}
|
||||
|
||||
void MusicModel::stopMusic()
|
||||
{
|
||||
if(mIsPlaying){
|
||||
mIsPlaying = false;
|
||||
emit isPlayingChanged();
|
||||
|
||||
reset();
|
||||
|
||||
emit stop();
|
||||
}
|
||||
}
|
||||
|
||||
void MusicModel::playNext()
|
||||
{
|
||||
emit next();
|
||||
if(!mIsPlaying){
|
||||
mIsPlaying = true;
|
||||
emit isPlayingChanged();
|
||||
}
|
||||
}
|
||||
|
||||
void MusicModel::playPrevious()
|
||||
{
|
||||
emit previous();
|
||||
if(!mIsPlaying){
|
||||
mIsPlaying = true;
|
||||
emit isPlayingChanged();
|
||||
}
|
||||
}
|
||||
|
||||
NavigationItemModel *MusicModel::getCurrentItem()
|
||||
{
|
||||
return mCurrentItem;
|
||||
}
|
||||
|
||||
VlcMediaList *MusicModel::getMedia()
|
||||
{
|
||||
return mMedia;
|
||||
}
|
||||
|
||||
bool MusicModel::isPlaying() const
|
||||
{
|
||||
return mIsPlaying;
|
||||
}
|
||||
|
||||
bool MusicModel::hasNext() const
|
||||
{
|
||||
return mHasNext;
|
||||
}
|
||||
|
||||
bool MusicModel::hasPrevious() const
|
||||
{
|
||||
return mHasPrevious;
|
||||
}
|
||||
|
||||
void MusicModel::setAudioVolume(int newVolume)
|
||||
{
|
||||
if(newVolume != mAudioVolume){
|
||||
if(newVolume > 100){
|
||||
mAudioVolume = 100;
|
||||
} else if(newVolume < 0){
|
||||
mAudioVolume = 0;
|
||||
} else {
|
||||
mAudioVolume = newVolume;
|
||||
}
|
||||
emit audioVolumeChanged(mAudioVolume);
|
||||
}
|
||||
}
|
||||
|
||||
double MusicModel::getProgress() const
|
||||
{
|
||||
return mCurrentMediaItemProgress;
|
||||
}
|
||||
|
||||
QString MusicModel::getMediaTitle() const
|
||||
{
|
||||
return mMediaTitle;
|
||||
}
|
||||
|
||||
QString MusicModel::getMediaLength()
|
||||
{
|
||||
return timeToString(mCurrentMediaItemLength);
|
||||
}
|
||||
|
||||
QString MusicModel::getTime()
|
||||
{
|
||||
return timeToString(mCurrentTime);
|
||||
}
|
||||
|
||||
void MusicModel::onNextMediaSet(VlcMedia *media)
|
||||
{
|
||||
setMediaTitle(media);
|
||||
|
||||
mHasNext = true;
|
||||
mHasPrevious = true;
|
||||
if(mMedia->at(0) == media){
|
||||
mHasPrevious = false;
|
||||
}
|
||||
if(mMedia->at(mMedia->count()-1) == media){
|
||||
mHasNext = false;
|
||||
}
|
||||
emit hasPreviousChanged();
|
||||
emit hasNextChanged();
|
||||
}
|
||||
|
||||
void MusicModel::onTimeChanged(int time)
|
||||
{
|
||||
mCurrentMediaItemProgress = (double) time / mCurrentMediaItemLength;
|
||||
mCurrentTime = time;
|
||||
emit progressChanged();
|
||||
}
|
||||
|
||||
void MusicModel::onLengthChanged(int length)
|
||||
{
|
||||
mCurrentMediaItemLength= length;
|
||||
emit mediaLengthChanged();
|
||||
}
|
||||
|
||||
void MusicModel::reset()
|
||||
{
|
||||
mHasNext = false;
|
||||
mHasPrevious = false;
|
||||
emit hasNextChanged();
|
||||
emit hasPreviousChanged();
|
||||
|
||||
mCurrentMediaItemProgress = 0.0;
|
||||
mCurrentTime = 0.0;
|
||||
emit progressChanged();
|
||||
|
||||
mCurrentMediaItemLength = 0.0;
|
||||
emit mediaLengthChanged();
|
||||
}
|
||||
|
||||
void MusicModel::clearMediaList()
|
||||
{
|
||||
while(mMedia->count() > 0){
|
||||
mMedia->removeMedia(0);
|
||||
}
|
||||
}
|
||||
|
||||
void MusicModel::readMedia(const QString& path)
|
||||
{
|
||||
auto dir = QDir(path);
|
||||
if(!dir.exists()) return;
|
||||
|
||||
auto fileNames = dir.entryList(QDir::Files);
|
||||
for(auto file:fileNames){
|
||||
if(file.endsWith(".flac") || file.endsWith(".mp3")){
|
||||
mMedia->addMedia(new VlcMedia(dir.filePath(file), true, mVlc));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
QString MusicModel::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;
|
||||
}
|
||||
|
||||
void MusicModel::setMediaTitle(VlcMedia *media)
|
||||
{
|
||||
auto list = media->currentLocation().split("/");
|
||||
auto title = list.at(list.count() -1 );
|
||||
mMediaTitle = title.left(title.lastIndexOf("."));
|
||||
emit mediaTitleChanged();
|
||||
}
|
||||
|
|
@ -1,100 +0,0 @@
|
|||
#ifndef MUSICMODEL_H
|
||||
#define MUSICMODEL_H
|
||||
|
||||
#include <QObject>
|
||||
|
||||
#include <models/NavigationItemModel.h>
|
||||
#include <VLCQtCore/MediaList.h>
|
||||
|
||||
class MusicModel : public QObject
|
||||
{
|
||||
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 play();
|
||||
void pause();
|
||||
void stop();
|
||||
void previous();
|
||||
void next();
|
||||
void hasPreviousChanged();
|
||||
void hasNextChanged();
|
||||
void isPlayingChanged();
|
||||
void progressChanged();
|
||||
void mediaLengthChanged();
|
||||
void mediaTitleChanged();
|
||||
void audioVolumeChanged(int newVolume);
|
||||
|
||||
public:
|
||||
MusicModel(VlcInstance* instance, QObject *parent = Q_NULLPTR);
|
||||
~MusicModel();
|
||||
|
||||
void init(NavigationItemModel* item);
|
||||
|
||||
Q_INVOKABLE void navigateBack();
|
||||
Q_INVOKABLE void playPause();
|
||||
Q_INVOKABLE void stopMusic();
|
||||
Q_INVOKABLE void playNext();
|
||||
Q_INVOKABLE void playPrevious();
|
||||
|
||||
NavigationItemModel *getCurrentItem();
|
||||
|
||||
VlcMediaList *getMedia();
|
||||
|
||||
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 onNextMediaSet(VlcMedia* media);
|
||||
void onTimeChanged(int time);
|
||||
void onLengthChanged(int length);
|
||||
|
||||
private:
|
||||
void reset();
|
||||
void clearMediaList();
|
||||
void readMedia(const QString& path);
|
||||
void setMediaTitle(VlcMedia* media);
|
||||
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 = Q_NULLPTR;
|
||||
VlcMediaList* mMedia = Q_NULLPTR;
|
||||
VlcInstance* mVlc = Q_NULLPTR;
|
||||
};
|
||||
|
||||
#endif // MUSICMODEL_H
|
||||
Loading…
Add table
Add a link
Reference in a new issue