player.cpp 1.15 KB
Newer Older
Camilo Higuita's avatar
Camilo Higuita committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
#include "player.h"

Player::Player(QObject *parent) : QObject(parent)
{
    this->player = new QMediaPlayer(this);
    this->player->setVolume(100);

    this->updater = new QTimer(this);
    connect(this->updater, &QTimer::timeout, this, &Player::update);
}

void Player::source(const QString &url)
{
    this->sourceurl = url;
    this->player->setMedia(QUrl::fromLocalFile(this->sourceurl));

    qDebug()<<this->player->metaData("Title").toString();
}

void Player::play()
{
    if(sourceurl.isEmpty()) return;

    if(!updater->isActive())
        this->updater->start(100);

    this->player->play();
}

void Player::pause()
{
    this->player->pause();
}

void Player::stop()
{
    this->player->stop();
    this->updater->stop();
}

void Player::seek(const int &pos)
{
    this->player->setPosition(pos);
}

int Player::duration()
{
    if(this->sourceurl.isEmpty()) return 0;

    return static_cast<int>(this->player->duration());
}

void Player::update()
{
    emit this->pos(static_cast<int>(static_cast<double>(this->player->position())/this->player->duration()*1000));
    if(this->player->state() == QMediaPlayer::StoppedState)
        emit this->finished();
}