-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplayer.cpp
More file actions
217 lines (177 loc) · 6.76 KB
/
Copy pathplayer.cpp
File metadata and controls
217 lines (177 loc) · 6.76 KB
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#include "player.h"
#include "mainwindow.h"
Player::Player()
{
this->player = new QMediaPlayer;
this->is_active = false;
this->media_interupts_enabled = false;
this->media_buffered = false;
this->media_loaded = false;
this->track_duration = 0;
}
void Player::Setup(MainWindow* main_window, int player_index)
{
this->main_window = main_window;
this->player_index = player_index;
QObject::connect(this->GetMediaPlayer(), SIGNAL(stateChanged(QMediaPlayer::State)), this, SLOT(OnStateChanged(QMediaPlayer::State)));
QObject::connect(this->GetMediaPlayer(), SIGNAL(durationChanged(qint64)), this, SLOT(OnDurationChange(qint64)));
QObject::connect(this->GetMediaPlayer(), SIGNAL(positionChanged(qint64)), this, SLOT(OnPositionChanged(qint64)));
QObject::connect(this->GetMediaPlayer(), SIGNAL(mediaStatusChanged(QMediaPlayer::MediaStatus)), this, SLOT(OnMediaStatusChange(QMediaPlayer::MediaStatus)));
this->PrintDebug("Setup connectors");
}
void Player::OnMediaStatusChange(QMediaPlayer::MediaStatus status)
{
this->PrintDebug("State: " + QString::number(status));
if (status == QMediaPlayer::LoadedMedia)
this->media_loaded = true;
else if (status == QMediaPlayer::BufferedMedia)
this->media_buffered = true;
}
QMediaPlayer* Player::GetMediaPlayer()
{
return this->player;
}
void Player::OnPositionChanged(qint64 new_position)
{
// Do not act on position change if not active
if (! this->is_active)
return;
qint64 duration = this->GetMediaPlayer()->duration();
if (duration >= 1000)
{
duration = duration / 1000;
char label_text[59];
long long dur_mins = std::floor(duration / 60);
long long dur_hrs = std::floor(dur_mins / 60);
long long new_pos_mins = 0;
long long new_pos_hrs = 0;
if (new_position >= 1000) {
new_position = new_position / 1000;
new_pos_mins = std::floor(new_position / 60);
new_pos_hrs = std::floor(new_pos_mins / 60);
}
snprintf(
label_text,
sizeof(label_text),
"%lld:%02lld:%02lld / %lld:%02lld:%02lld",
new_pos_hrs,
new_pos_mins % 60,
new_position % 60,
dur_hrs,
dur_mins % 60,
duration % 60);
this->main_window->GetPositionLabel()->setText(label_text);
} else {
this->main_window->GetPositionLabel()->setText("0:00:00 / 0:00:00");
}
}
void Player::OnDurationChange(qint64 new_duration) {
this->PrintDebug("OnDurationChange called: " + QString::number(new_duration));
this->track_duration = new_duration;
}
void Player::OnStateChanged(QMediaPlayer::State newState) {
this->PrintDebug("Media State: " + QString::number(newState));
// If interupts are disabled (when swapping players), if some comes to an end,
// start the player again.
if (this->media_interupts_enabled && newState == QMediaPlayer::StoppedState) {
this->PrintDebug("Interupts enabled, resarting current player.");
this->GetMediaPlayer()->play();
}
}
void Player::PrepareFlipTo(QUrl url)
{
this->PrintDebug("Starting PrepareFlipTo.");
this->media_loaded = false;
this->media_buffered = false;
this->track_duration = 0;
int old_volume = this->GetMediaPlayer()->volume();
bool was_active = this->is_active;
// Update file path of next player
this->PrintDebug("Loading file: " + url.url());
this->GetMediaPlayer()->setMedia(url);
// Check for any errors after loading media
if (this->GetMediaPlayer()->error())
this->main_window->DisplayError(this->GetMediaPlayer()->errorString());
this->PrintDebug("Waiting for media to load.");
while (this->media_loaded == false)
// Wait for 50ms
QCoreApplication::processEvents(QEventLoop::AllEvents, 50);
this->PrintDebug("Media loaded.");
// Play track
this->is_active = false;
this->GetMediaPlayer()->pause();
// Wait for media to buffer
this->PrintDebug("Waiting for media to buffer.");
while (this->media_buffered == false)
// Wait for 50ms
QCoreApplication::processEvents(QEventLoop::AllEvents, 50);
this->PrintDebug("Media buffered.");
// Wait for position to be set (and required flag set to false)
// @TODO: Do not play audio with minimal volume - this will be audible to the user.
// This is required as the duration will not be populated (nor will the durationChanged
// slot be called) if: media is paused instead of played, mediaplayer volume is set to 0
// or mediaplayer is set to muted.
// Therefore, this is the only way to be able to obtain the duration of the track.
this->GetMediaPlayer()->setVolume(1);
this->GetMediaPlayer()->play();
this->PrintDebug("Waiting for duration to be set.");
while (this->track_duration == 0)
// Wait for 50ms
QCoreApplication::processEvents(QEventLoop::AllEvents, 50);
this->GetMediaPlayer()->pause();
this->PrintDebug("Duration set.");
this->GetMediaPlayer()->setVolume(old_volume);
this->is_active = was_active;
this->PrintDebug("Finished PrepareFlipTo.");
}
void Player::PrintDebug(QString debug)
{
std::cout << "Player " << this->player_index << ": " << debug.toStdString() << std::endl;
}
void Player::FlipFrom(bool was_playing)
{
this->PrintDebug("Starting FipFrom.");
this->is_active = false;
this->media_interupts_enabled = false;
if (was_playing)
this->player->pause();
this->PrintDebug("Finished FipFrom.");
}
void Player::FlipTo(bool was_playing)
{
this->PrintDebug("Starting FlipTo.");
this->SetPosition();
this->is_active = true;
if (was_playing)
this->Play();
this->media_interupts_enabled = true;
this->PrintDebug("Finished FlipTo.");
}
void Player::SetPosition()
{
// Set position based on time since application startup, using modulus of track length.
// Ignore tts less than 0, maybe due to time change or race condition
qint64 tts = (QDateTime::currentMSecsSinceEpoch() - this->main_window->GetStartupTime());
if (tts >= 0)
{
qint64 dur = this->GetMediaPlayer()->duration();
this->PrintDebug("Track duration: " + QString::number(this->GetMediaPlayer()->duration()) + ".");
if (dur > 0) {
this->PrintDebug("Setting track to position: " + QString::number(tts % this->GetMediaPlayer()->duration()));
this->GetMediaPlayer()->setPosition(tts % this->GetMediaPlayer()->duration());
}
}
}
void Player::Play()
{
this->GetMediaPlayer()->play();
if (this->GetMediaPlayer()->state() != QMediaPlayer::PlayingState)
this->main_window->DisplayError("Not playing");
}
void Player::Pause()
{
this->GetMediaPlayer()->pause();
}
Player::~Player()
{
}