aboutsummaryrefslogtreecommitdiff
path: root/player.cpp
blob: 0c189799d6f0cf7d69e20b1782ae0cef1ea3074a (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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*
    Alee Audio Player: Audio player in Qt
    Copyright (C) 2020 Alee Productions

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
*/
#include "player.h"
#include "ui_player.h"
#include "about.h"
#include "library.h"

void Player::loadFile()
{
    QMessageBox msgbox;

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

        char* fn = new char [mFile.toStdString().size()+1];
        strcpy( fn, mFile.toStdString().c_str() );
        TagLib::FileRef* fileref = new TagLib::FileRef(fn);

        if (fileref->tag() != nullptr) {
            TagLib::String title = fileref->tag()->title();
            TagLib::String artist = fileref->tag()->artist();
            const char* title_ = title.to8Bit(true).c_str();
            const char* artist_ = artist.to8Bit(true).c_str();
            ui->labelTitle->setText(title_);
            ui->labelAuthor->setText(artist_);
        }

        return;
    }
}



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

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

}

Player::~Player()
{
    qInfo() << tr("Closing Alee Audio Player...");
    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() << tr("Pausing music...");
         mPlayer->pause();
         ui->playButton->setText(tr("Play"));
     } else {
         qDebug() << tr("Playing music...");
         mPlayer->play();
         ui->playButton->setText(tr("Pause"));
     }

}

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

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

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

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


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);
}

void Player::on_actionBugReport_triggered()
{
    QDesktopServices::openUrl(QUrl("https://github.com/aleeproductions/Alee-Audio-Player/issues", QUrl::TolerantMode));
}