147 lines
2.7 KiB
C++
147 lines
2.7 KiB
C++
#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))
|
|
{
|
|
}
|
|
|
|
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();
|
|
|
|
mHasNext = false;
|
|
mHasPrevious = false;
|
|
emit hasNextChanged();
|
|
emit hasPrevious();
|
|
|
|
while(mMedia->count() > 0){
|
|
mMedia->removeMedia(0);
|
|
}
|
|
|
|
auto dir = QDir(mCurrentItem->getPath());
|
|
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));
|
|
}
|
|
}
|
|
}
|
|
|
|
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;
|
|
mHasNext = false;
|
|
mHasPrevious = false;
|
|
mCurrentMediaItemProgress = 0.0;
|
|
emit progressChanged();
|
|
emit hasNextChanged();
|
|
emit hasPreviousChanged();
|
|
emit isPlayingChanged();
|
|
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()
|
|
{
|
|
return mIsPlaying;
|
|
}
|
|
|
|
bool MusicModel::hasNext()
|
|
{
|
|
return mHasNext;
|
|
}
|
|
|
|
bool MusicModel::hasPrevious()
|
|
{
|
|
return mHasPrevious;
|
|
}
|
|
|
|
double MusicModel::getProgress()
|
|
{
|
|
return mCurrentMediaItemProgress;
|
|
}
|
|
|
|
void MusicModel::onNextMediaSet(VlcMedia *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;
|
|
emit progressChanged();
|
|
}
|
|
|
|
void MusicModel::onLengthChanged(int length)
|
|
{
|
|
mCurrentMediaItemLength= length;
|
|
}
|