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
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;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue