203 lines
3.7 KiB
C++
203 lines
3.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();
|
|
|
|
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;
|
|
}
|
|
|
|
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();
|
|
}
|