-
Notifications
You must be signed in to change notification settings - Fork 0
/
audio_player.cpp
87 lines (72 loc) · 1.75 KB
/
audio_player.cpp
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
#include "audio_player.h"
#include <QTimer>
AudioPlayer::AudioPlayer(QObject* parent) : QObject(parent)
{
m_player = new QMediaPlayer(this);
connect(m_player, &QMediaPlayer::stateChanged, this, &AudioPlayer::onStateChanged);
connect(m_player, &QMediaPlayer::mediaStatusChanged, this, &AudioPlayer::onMediaStatusChanged);
connect(m_player, &QMediaPlayer::positionChanged, this, &AudioPlayer::onPositionChanged);
connect(m_player, &QMediaPlayer::durationChanged, this, &AudioPlayer::onDurationChanged);
}
void AudioPlayer::setPosition(qint64 position)
{
m_player->setPosition(position);
}
qint64 AudioPlayer::position() const
{
return m_player->position();
}
qint64 AudioPlayer::duration() const
{
return m_player->duration();
}
QMediaPlayer::State AudioPlayer::state() const
{
return m_player->state();
}
void AudioPlayer::play(const QString &filePath)
{
m_player->setMedia(QUrl::fromLocalFile(filePath));
m_player->play();
}
void AudioPlayer::stop()
{
m_player->stop();
}
void AudioPlayer::togglePlayback()
{
if(m_player->state() == QMediaPlayer::PlayingState)\
{
m_player->pause();
}
else {
m_player->play();
}
}
void AudioPlayer::onStateChanged(QMediaPlayer::State state)
{
emit stateChanged(state);
}
void AudioPlayer::onMediaStatusChanged(QMediaPlayer::MediaStatus state)
{
if(state == QMediaPlayer::EndOfMedia)
{
emit trackFinished();
}
}
void AudioPlayer::onPositionChanged(qint64 position)
{
emit positionChanged(position);
}
void AudioPlayer::onDurationChanged(qint64 duration)
{
emit durationChanged(duration);
}
int AudioPlayer::volume() const
{
return m_player->volume();
}
void AudioPlayer::setVolume(int volume)
{
m_player->setVolume(volume);
}