aboutsummaryrefslogtreecommitdiff
path: root/player.cpp
blob: 69fdd8a177424befbf46b53c48b807eeebe776ff (plain) (blame)
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include <QMediaPlayer>
#include <QDebug>
#include <QFileDialog>
#include <QMessageBox>
#include "player.h"
#include "ui_player.h"
#include "about.h"

void Player::mFileDialog()
{
    QString mFile;
    QMessageBox msgbox;
    mFile = QFileDialog::getOpenFileName(this, "Open any audio file", QDir::homePath(), tr("Audio Files (*.mp3 *.wav *.ogg *.flac)"));
    if (mFile == NULL) {
        qDebug() << "File cannot be found";
        msgbox.setWindowTitle("Uh oh! An error has occured!");
        msgbox.setText("File is invalid. Maybe try loading a valid audio file.");
        msgbox.setIcon(QMessageBox::Critical);
        msgbox.exec();
        return;
    } else {
        mPlayer->setMedia(QUrl::fromLocalFile(mFile));
        qDebug() << "Opening" << mFile;
        msgbox.setWindowTitle("Success!");
        msgbox.setText("This audio file has been loaded.");
        msgbox.setIcon(QMessageBox::Information);
        msgbox.exec();
        ui->volumeSlider->setValue(100);
        ui->playButton->setText(tr("Play"));
        return;
    }
}



Player::Player(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::Player)
{
    ui->setupUi(this);

    connect(mPlayer, &QMediaPlayer::positionChanged, this, &Player::on_positionChanged);
    connect(mPlayer, &QMediaPlayer::durationChanged, this, &Player::on_durationChanged);

}

Player::~Player()
{
    qInfo() << "Closing AleePlayer...";
    mPlayer->deleteLater();
    delete ui;
}

void Player::on_actionQuit_triggered()
{
    close();
}

void Player::on_playButton_pressed()
{
    ui->playbackSlider->setEnabled(true);
    ui->volumeSlider->setEnabled(true);

    if (mPlayer->state() == mPlayer->PlayingState) {
         qDebug() << "Pausing music...";
         mPlayer->pause();
         ui->playButton->setText(tr("Play"));
     } else {
         qDebug() << "Playing music...";
         mPlayer->play();
         ui->playButton->setText(tr("Pause"));
     }

}

void Player::on_stopButton_pressed()
{
    qInfo() << "Stopping music...";
    mPlayer->stop();
    ui->volumeSlider->setEnabled(false);
    ui->volumeSlider->setValue(100);
    ui->playbackSlider->setEnabled(false);
    ui->playbackSlider->setValue(0);
}

void Player::on_actionAbout_triggered()
{
    qDebug() << "Opening dialog";
    About about;
    about.exec();
}

void Player::on_mediaButton_pressed()
{
    mFileDialog();
}

void Player::on_actionOpen_triggered()
{
    mFileDialog();
}


void Player::on_volumeSlider_sliderMoved(int position)
{
    mPlayer->setVolume(position);
}

void Player::on_playbackSlider_sliderMoved(int position)
{
    mPlayer->setPosition(position);
}

void Player::on_positionChanged(qint64 position)
{
    ui->playbackSlider->setValue(position);
}

void Player::on_durationChanged(qint64 position)
{
    ui->playbackSlider->setMaximum(position);
}