-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCVlcKits.cpp
292 lines (241 loc) · 7.47 KB
/
CVlcKits.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
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
#include "CVlcKits.h"
#include <iostream>
#include <QDir>
using namespace std;
/*
* VLC 播放器的回调函数
*/
void vlc_callback(const struct libvlc_event_t* p_event, void* p_data)
{
CVlcKits* pThis = static_cast<CVlcKits*>(p_data);
if (pThis)
{
switch (p_event->type)
{
case libvlc_MediaPlayerPositionChanged:
{
//播放器的位置改变时
float pos = libvlc_media_player_get_position(pThis->media_player());
pThis->setTimeSliderPos(pos * 100);
qint64 curSecs = libvlc_media_player_get_time(pThis->media_player()) / 1000;
int curH = curSecs / 3600;
int curMinute = (curSecs - curH * 3600) / 60;
int curSec = curSecs - curH * 3600 - curMinute * 60;
char buf1[256] = { 0 };
sprintf(buf1, "%02d:%02d:%02d", curH, curMinute, curSec);
QString str1(buf1);
qint64 totalSecs = pThis->getDuration();
int _hour = totalSecs / 3600;
int _minute = (totalSecs - _hour * 3600) / 60;
int _sec = totalSecs - _hour * 3600 - _minute * 60;
char buf2[256] = { 0 };
sprintf(buf2, "%02d:%02d:%02d", _hour, _minute, _sec);
QString str2(buf2);
QString text = str1 + "/" + str2;
pThis->setTimeText(text, libvlc_media_player_get_time(pThis->media_player()), totalSecs);
}
break;
case libvlc_MediaPlayerAudioVolume:
{
//音量改变
int volume = libvlc_audio_get_volume(pThis->media_player());
pThis->setVolumeSliderPos(volume);
}
break;
case libvlc_MediaPlayerMediaChanged:
{
//当媒体改变时,更新当前媒体的索引和持续时间
cout << "libvlc_MediaPlayerMediaChanged" << endl;
pThis->addCurrentIndex();
int _index = pThis->getCurrentIndex();
int vSize = pThis->getVecDuration().size();
if (_index > vSize)
{
// 开启了循环播放
// 4 5 6 7 8 9
if (_index % vSize == 0)
{
_index = vSize;
}
else
{
_index = _index % vSize;
}
}
libvlc_time_t _duration = pThis->getVecDuration()[_index - 1];
pThis->setCurrentDuration(_duration * 1000);
}
break;
}
}
}
CVlcKits::CVlcKits()
{
}
CVlcKits::~CVlcKits()
{
if (m_pMediaPlayer)
{
libvlc_media_player_release(m_pMediaPlayer);
m_pMediaPlayer = nullptr;
}
// 最后释放
if (m_pInstance)
{
libvlc_release(m_pInstance);
m_pInstance = nullptr;
}
}
/// <summary>
/// 初始化libvlc
/// </summary>
/// <returns>
/// 0 success
/// -1, libvlc_new failed
/// -2, libvlc_media_player_new failed
/// </returns>
int CVlcKits::initVLC()
{
// vlc初始化
m_pInstance = libvlc_new(0, nullptr);
if (m_pInstance)
{
m_pMediaPlayer = libvlc_media_player_new(m_pInstance);
if (m_pMediaPlayer)
{
m_pEvent_manager = libvlc_media_player_event_manager(m_pMediaPlayer);
libvlc_event_attach(m_pEvent_manager, libvlc_MediaPlayerPositionChanged, vlc_callback, this);
libvlc_event_attach(m_pEvent_manager, libvlc_MediaPlayerAudioVolume, vlc_callback, this);
libvlc_event_attach(m_pEvent_manager, libvlc_MediaPlayerMediaChanged, vlc_callback, this);
}
else
{
libvlc_release(m_pInstance);
return -2; // libvlc_media_player_new failed
}
}
else
{
return -1; // libvlc_new failed
}
return 0;
}
libvlc_media_player_t* CVlcKits::media_player()
{
return m_pMediaPlayer;
}
void CVlcKits::setTimeSliderPos(int value)
{
//qDebug() << "vlc send sig TimeSliderPos:" << value;
emit sig_TimeSliderPos(value);
}
void CVlcKits::setVolumeSliderPos(int value)
{
//qDebug() << "vlc send sig VolumeSliderPos:" << value;
emit sig_VolumeSliderPos(value);
}
void CVlcKits::setTimeText(const QString& str, const qint64& cur, const qint64& total)
{
//qDebug() << "vlc send sig time change:" << str;
emit sig_UpdateTimeText(str, cur, total);
}
void CVlcKits::setCurrentDuration(libvlc_time_t value)
{
//总时长
qDebug() << "vlc setCurrentDuration:" << value;
m_CurrentDuration = value;
}
libvlc_time_t CVlcKits::getDuration()
{
return m_CurrentDuration;
}
int CVlcKits::play(QStringList fileList, void* hwnd)
{
m_pMediaPlayerList = libvlc_media_list_player_new(m_pInstance);
m_medialist = libvlc_media_list_new(m_pInstance);
int size = fileList.size();
for (int i = 0; i < size; i++)
{
QString fileName = fileList[i];
fileName = QDir::toNativeSeparators(fileName);
libvlc_media_t* _pMedia = libvlc_media_new_path(m_pInstance, fileName.toStdString().c_str());
if (!_pMedia)
{
return -1;
}
libvlc_media_list_add_media(m_medialist, _pMedia);
cout << "before parse" << endl;
libvlc_media_parse(_pMedia);
cout << "after parse" << endl;
libvlc_time_t _duration = libvlc_media_get_duration(_pMedia);
if (_duration < -1)
{
return -2;
}
cout << "before push" << endl;
m_vecDurations.push_back(_duration / 1000);
cout << "after push" << endl;
libvlc_media_release(_pMedia);
}
// 设置播放循环模式
libvlc_media_list_player_set_playback_mode(m_pMediaPlayerList, libvlc_playback_mode_loop);
libvlc_media_list_player_set_media_list(m_pMediaPlayerList, m_medialist);
libvlc_media_list_player_set_media_player(m_pMediaPlayerList, m_pMediaPlayer);
libvlc_media_player_set_hwnd(m_pMediaPlayer, hwnd);
libvlc_media_list_player_play(m_pMediaPlayerList);
return 0;
}
/*
libvlc_media_list_player_play
libvlc_media_list_player_pause
libvlc_media_list_player_stop
*/
void CVlcKits::play()
{
if (libvlc_media_player_get_state(m_pMediaPlayer)
== libvlc_state_t::libvlc_Paused)
{
//libvlc_media_player_play(m_pMediaPlayer);
libvlc_media_list_player_play(m_pMediaPlayerList);
}
else if (libvlc_media_player_get_state(m_pMediaPlayer)
== libvlc_state_t::libvlc_Playing)
{
//libvlc_media_player_pause(m_pMediaPlayer);
libvlc_media_list_player_pause(m_pMediaPlayerList);
}
}
void CVlcKits::pause()
{
if (libvlc_media_player_get_state(m_pMediaPlayer)
== libvlc_state_t::libvlc_Playing)
{
libvlc_media_player_pause(m_pMediaPlayer);
}
}
void CVlcKits::stop()
{
if (libvlc_media_player_get_state(m_pMediaPlayer)
== libvlc_state_t::libvlc_Playing
|| libvlc_media_player_get_state(m_pMediaPlayer)
== libvlc_state_t::libvlc_Paused)
{
libvlc_media_player_stop(m_pMediaPlayer);
}
}
vector<libvlc_time_t> CVlcKits::getVecDuration()
{
return m_vecDurations;
}
int CVlcKits::getCurrentIndex()
{
return m_CurrentInex;;
}
void CVlcKits::addCurrentIndex()
{
m_CurrentInex++;
}
void CVlcKits::setVideoPostion(int value)
{
libvlc_media_player_set_position(m_pMediaPlayer, value / 100.0);
}