-
Notifications
You must be signed in to change notification settings - Fork 0
/
AVPlayer.cpp
337 lines (283 loc) · 6.91 KB
/
AVPlayer.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
#include "stdafx.h"
#include "AVPlayer.h"
#include <cmath>
#include <vector>
#if _MSC_VER <= 1500 //vs2008
//#include <vlc/stdint.h>
#endif
#include <vlc/vlc.h>
CAVPlayer::CAVPlayer(void) :
m_pVLC_Inst(NULL),
m_pVLC_Player(NULL),
m_hWnd(NULL),
m_pfnPlaying(NULL),
m_pfnPosChanged(NULL),
m_pfnEndReached(NULL),
m_pVolume(50)
{
}
CAVPlayer::~CAVPlayer(void)
{
Release();
}
void CAVPlayer::Init()
{
if (! m_pVLC_Inst)
{
m_pVLC_Inst = libvlc_new(0, NULL);
}
}
void CAVPlayer::Release()
{
Stop();
if (m_pVLC_Inst)
{
libvlc_release (m_pVLC_Inst);
m_pVLC_Inst = NULL;
}
}
bool CAVPlayer::Play(const std::string &strPath)
{
if (! m_pVLC_Inst)
{
Init();
}
if(strPath.empty() || ! m_pVLC_Inst)
{
return false;
}
// 验证地址是网络地址,还是本地地址
bool bURL = false;
std::vector<std::string> vctURL;
vctURL.push_back("http");
vctURL.push_back("https");
vctURL.push_back("ftp");
vctURL.push_back("rstp");
for (unsigned i = 0; i < vctURL.size(); i++)
{
// 实际使用请判断大小写
if (! strPath.compare(0, vctURL[i].size(), vctURL[i]))
{
bURL = true;
break;
}
}
Stop();
bool bRet = false;
libvlc_media_t *m;
if (bURL)
{
// 示例网络地址:http://zhangmenshiting.baidu.com/data2/music/1203175/51597064800128.mp3?xcode=cb97ecc9d44ba29122881c69e505a43849f04e8309ecc859
m = libvlc_media_new_location(m_pVLC_Inst, strPath.c_str());
}
else
{
m = libvlc_media_new_path(m_pVLC_Inst, strPath.c_str());
}
if (m)
{
if (m_pVLC_Player = libvlc_media_player_new_from_media(m))
{
libvlc_media_player_set_hwnd(m_pVLC_Player, m_hWnd);
libvlc_media_player_play(m_pVLC_Player);
libvlc_audio_set_volume(m_pVLC_Player, int(m_pVolume * 1.3));
// 事件管理
libvlc_event_manager_t *vlc_evt_man = libvlc_media_player_event_manager(m_pVLC_Player);
libvlc_event_attach(vlc_evt_man, libvlc_MediaPlayerPlaying, ::OnVLC_Event, this);
libvlc_event_attach(vlc_evt_man, libvlc_MediaPlayerPositionChanged, ::OnVLC_Event, this);
libvlc_event_attach(vlc_evt_man, libvlc_MediaPlayerEndReached, ::OnVLC_Event, this);
bRet = true;
}
libvlc_media_release(m);
}
return bRet;
}
void CAVPlayer::Stop()
{
if (m_pVLC_Player)
{
libvlc_media_player_stop (m_pVLC_Player); /* Stop playing */
libvlc_media_player_release (m_pVLC_Player); /* Free the media_player */
m_pVLC_Player = NULL;
}
}
void CAVPlayer::Play()
{
if (m_pVLC_Player)
{
libvlc_media_player_play(m_pVLC_Player);
}
}
void CAVPlayer::Pause()
{
if (m_pVLC_Player)
{
libvlc_media_player_pause(m_pVLC_Player);
}
}
void CAVPlayer::Volume(int iVol)
{
if (iVol < 0)
{
return;
}
m_pVolume = iVol;
if (m_pVLC_Player)
{
libvlc_audio_set_volume(m_pVLC_Player,int(iVol * 1.3)); // 如果放到100,感觉比迅雷的100少了30,所以这里用1.3倍音量
}
}
void CAVPlayer::VolumeIncrease()
{
if (m_pVLC_Player)
{
int iVol = libvlc_audio_get_volume(m_pVLC_Player);
Volume((int)ceil(iVol * 1.1));
}
}
void CAVPlayer::VolumeReduce()
{
if (m_pVLC_Player)
{
int iVol = libvlc_audio_get_volume(m_pVLC_Player);
Volume((int)floor(iVol * 0.9));
}
}
int CAVPlayer::GetPos()
{
if (m_pVLC_Player)
{
return (int)(1000 * libvlc_media_player_get_position(m_pVLC_Player));
}
return 0;
}
void CAVPlayer::SeekTo(int iPos)
{
if (iPos < 0 || iPos > 1000)
{
return;
}
if (m_pVLC_Player)
{
libvlc_media_player_set_position(m_pVLC_Player, iPos/(float)1000.0);
}
}
void CAVPlayer::SeekForward()
{
//int iPos = GetPos();
//SeekTo((int)ceil(iPos * 1.1));
// 一次快退10秒
if (m_pVLC_Player)
{
libvlc_time_t i_time = libvlc_media_player_get_time(m_pVLC_Player) + 10000;
if (i_time > GetTotalTime())
{
i_time = GetTotalTime();
}
libvlc_media_player_set_time(m_pVLC_Player, i_time);
}
}
void CAVPlayer::SeekBackward()
{
//int iPos = GetPos();
//SeekTo((int)floor(iPos * 0.9));
if (m_pVLC_Player)
{
libvlc_time_t i_time = libvlc_media_player_get_time(m_pVLC_Player) - 10000;
if (i_time < 0)
{
i_time = 0;
}
libvlc_media_player_set_time(m_pVLC_Player, i_time);
}
}
void CAVPlayer::SetHWND( HWND hwnd )
{
if (::IsWindow(hwnd))
{
m_hWnd = hwnd;
}
}
HWND CAVPlayer::GetHWND()
{
return m_hWnd;
}
bool CAVPlayer::IsOpen()
{
return NULL != m_pVLC_Player;
}
bool CAVPlayer::IsPlaying()
{
if (m_pVLC_Player)
{
return (1 == libvlc_media_player_is_playing(m_pVLC_Player));
}
return false;
}
__int64 CAVPlayer::GetTotalTime()
{
if (m_pVLC_Player)
{
return libvlc_media_player_get_length(m_pVLC_Player);
}
return 0;
}
__int64 CAVPlayer::GetTime()
{
if (m_pVLC_Player)
{
return libvlc_media_player_get_time(m_pVLC_Player);
}
return 0;
}
int CAVPlayer::GetVolume()
{
if (m_pVLC_Player)
{
return libvlc_audio_get_volume(m_pVLC_Player);
}
return 0;
}
void CAVPlayer::SetCallbackPlaying( pfnCallback pfn )
{
m_pfnPlaying = pfn;
}
void CAVPlayer::SetCallbackPosChanged( pfnCallback pfn )
{
m_pfnPosChanged = pfn;
}
void CAVPlayer::SetCallbackEndReached( pfnCallback pfn )
{
m_pfnEndReached = pfn;
}
void OnVLC_Event( const libvlc_event_t *event, void *data )
{
CAVPlayer *pAVPlayer = (CAVPlayer *) data;
pfnCallback pfn = NULL;
if (! pAVPlayer)
{
return;
}
switch(event->type)
{
case libvlc_MediaPlayerPlaying:
pfn = pAVPlayer->m_pfnPlaying;
break;
case libvlc_MediaPlayerPositionChanged:
pfn = pAVPlayer->m_pfnPosChanged;
break;
case libvlc_MediaPlayerEndReached:
pfn = pAVPlayer->m_pfnEndReached;
break;
default:
break;
}
if (pfn)
{
pfn(data); // 此回调函数还可以传入其他参数,除了data外,还有event的各种信息(如event->u.media_player_position_changed.new_position)等等,请自行扩展。
}
}
int CAVPlayer::Save_snapshot(const char* filepath)
{
return libvlc_video_take_snapshot(m_pVLC_Player, 0, filepath, libvlc_video_get_width(m_pVLC_Player), libvlc_video_get_height(m_pVLC_Player));
}