-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplayercontroller.cpp
More file actions
145 lines (119 loc) · 3.43 KB
/
playercontroller.cpp
File metadata and controls
145 lines (119 loc) · 3.43 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
#include "playercontroller.h"
#include <QDebug>
PlayerController::PlayerController(QObject *parent)
: QObject(parent)
{
// 进度更新定时器,建议 200ms 一次
m_timer.setInterval(200);
connect(&m_timer, &QTimer::timeout, this, &PlayerController::onPlaybackTimer);
connect(&m_player, &AudioPlayer::playFinished, this, &PlayerController::onPlayFinished);
}
void PlayerController::setPlaylist(const QStringList &fileList) {
m_playlist = fileList;
m_currentIndex = -1;
}
void PlayerController::play(int index) {
if (index < 0 || index >= m_playlist.size()) {
qWarning() << "播放索引越界:" << index;
return;
}
m_currentIndex = index;
stop(); // 先停止当前播放
QString filePath = m_playlist[m_currentIndex];
if (!m_decoder.openFile(filePath)) {
qWarning() << "无法打开文件:" << filePath;
return;
}
m_duration = m_decoder.getDurationMs();
emit durationChanged(m_duration);
if (!m_player.start(m_decoder.getSampleRate(), m_decoder.getChannels())) {
qWarning() << "AudioPlayer 启动失败";
return;
}
m_position = 0;
m_isPlaying = true;
emit playbackStateChanged(true);
m_timer.start();
startDecodingAndPlaying();
emit currentSongChanged(
QFileInfo(filePath).fileName(),
m_decoder.getSampleRate(),
m_decoder.getChannels(),
m_decoder.getBitrate() / 1000
);
}
void PlayerController::stop() {
m_timer.stop();
m_player.stop();
m_decoder.cleanup();
m_position = 0;
m_isPlaying = false;
emit playbackStateChanged(false);
}
bool PlayerController::isPlaying()
{
return m_isPlaying;
}
void PlayerController::setVolume(int percent)
{
m_player.setVolume(percent);
}
void PlayerController::pause() {
if (m_isPlaying) {
m_player.pause();
m_timer.stop();
m_isPlaying = false;
emit playbackStateChanged(false);
}
}
void PlayerController::resume() {
if (!m_isPlaying) {
m_player.resume();
m_timer.start();
m_isPlaying = true;
emit playbackStateChanged(true);
}
}
void PlayerController::seek(qint64 ms) {
if (!m_decoder.seek(ms)) {
qWarning() << "seek 失败";
return;
}
m_position = ms;
// 清空播放缓冲,重新解码
m_player.clearPcmData();
startDecodingAndPlaying();
emit positionChanged(m_position);
}
QString PlayerController::currentFile() const {
if (m_currentIndex >= 0 && m_currentIndex < m_playlist.size())
return m_playlist[m_currentIndex];
return QString();
}
void PlayerController::onPlaybackTimer() {
m_position += 200; // 估算播放进度,单位ms
if (m_position >= m_duration) {
// 歌曲播放结束
stop();
emit positionChanged(m_duration);
return;
}
emit positionChanged(m_position);
}
void PlayerController::startDecodingAndPlaying() {
constexpr size_t bufferSize = 4096 * 8;
std::vector<uint8_t> pcmBuffer(bufferSize);
int outSampleRate = 0;
int outChannels = 0;
while (m_isPlaying) {
int decodedBytes = m_decoder.decodeFrame(pcmBuffer.data(), bufferSize, outSampleRate, outChannels);
if (decodedBytes <= 0) {
break;
}
m_player.addPcmData(pcmBuffer.data(), decodedBytes);
}
}
void PlayerController::onPlayFinished() {
qDebug() << "play finish ---->";
emit playCompleted();
}