183 lines
3.5 KiB
C++
183 lines
3.5 KiB
C++
#include "MusicPlayer.h"
|
|
#include <QDir>
|
|
#include <QMediaPlaylist>
|
|
|
|
|
|
|
|
MusicPlayer::MusicPlayer(QObject *parent) : QMediaPlayer(parent)
|
|
{
|
|
// relay base class signal as NOTIFY doesn't seem to be able to handle it directly
|
|
connect(this, &QMediaPlayer::volumeChanged, this, &MusicPlayer::audioVolumeChanged);
|
|
// init audio
|
|
setAudioVolume(50);
|
|
}
|
|
|
|
void MusicPlayer::init(NavigationItemModel *item)
|
|
{
|
|
if(mCurrentItem == item){
|
|
return;
|
|
}
|
|
mCurrentItem = item;
|
|
emit coverImageSourceChanged();
|
|
|
|
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();
|
|
}
|
|
}
|
|
|
|
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 != volume()){
|
|
qDebug() << volume() << newVolume;
|
|
setVolume(newVolume);
|
|
// signal audioVolumeChanged will be emitted automatically
|
|
}
|
|
}
|
|
|
|
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;
|
|
}
|