-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathmainwindow.cpp
177 lines (164 loc) · 4.54 KB
/
mainwindow.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
/*-------------------------------------------------
#
# Project created by QtCreator
# Author: 沙振宇
# CreateTime: 2017-05-23
# UpdateTime: 2019-12-19
# Info: Qt+VLC简单的使用显示视频Demo
# Url:https://shazhenyu.blog.csdn.net/article/details/72673677
# Github:https://github.com/ShaShiDiZhuanLan/Demo_VLC_Qt
#
#-------------------------------------------------*/
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDir>
#include <QFile>
#include <QGraphicsScene>
#include <QGraphicsProxyWidget>
#include <QGraphicsView>
#include "vlc/vlc.h"
#include <QDebug>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
m_fileDialog = new QFileDialog(this);
m_fileDialog->setWindowTitle(tr("Open Video"));
m_fileDialog->setDirectory(".");
m_fileDialog->setNameFilter(tr("Video Files(*.mp4 *.mp3 *flv *3gp *rmvb)"));
m_fileDialog->hide();
}
MainWindow::~MainWindow()
{
delete ui;
}
//加载
bool MainWindow::load(const QString &filepath)
{
if(filepath.isEmpty()){
qDebug() << "filepath isEmpty";
return false;
}
if(!QFile::exists(filepath)){
qDebug() << "QFile no exists";
return false;
}
bool bload = loadVlc(QDir::toNativeSeparators(filepath).toUtf8().constData());
if(!bload){
qDebug() << "bload no isEmpty";
return false;
}
return true;
}
//播放
void MainWindow::play()
{
if(libvlc_Ended == libvlc_media_get_state(m_media)){
stop();//vlc这个设定太奇怪了,当自然播放结束,重新播放还得主动调用一次stop();
}
libvlc_media_player_play(m_mediaPlayer);
}
//暂停
void MainWindow::pause()
{
if((bool)libvlc_media_player_is_playing(m_mediaPlayer)){
m_bPlaying = false;
libvlc_media_player_set_pause(m_mediaPlayer, 1);//暂停
}
}
//停止
void MainWindow::stop()
{
m_bPlaying = false;
libvlc_media_player_stop(m_mediaPlayer);
}
//加载
bool MainWindow::loadVlc(const QString &filepath)
{
m_vlcInstance = libvlc_new(0, NULL);
m_media = libvlc_media_new_path(m_vlcInstance, filepath.toUtf8().data());
if(!m_media){
freeVlc();
return m_media;
}
qDebug() << "loadVlc" << qPrintable(filepath);
m_mediaPlayer = libvlc_media_player_new_from_media(m_media);
if(!m_mediaPlayer){
freeVlc();
return false;
}
libvlc_media_parse(m_media);
libvlc_event_manager_t *em = libvlc_media_player_event_manager(m_mediaPlayer);
libvlc_event_attach(em, libvlc_MediaPlayerTimeChanged, vlcEvents, this);
libvlc_event_attach(em, libvlc_MediaPlayerEndReached, vlcEvents, this);
libvlc_event_attach(em, libvlc_MediaPlayerStopped, vlcEvents, this);
libvlc_event_attach(em, libvlc_MediaPlayerPlaying, vlcEvents, this);
libvlc_event_attach(em, libvlc_MediaPlayerPaused, vlcEvents, this);
libvlc_media_player_set_hwnd(m_mediaPlayer, (void *)ui->label->winId());
return true;
}
//VLC事件
void MainWindow::vlcEvents(const libvlc_event_t *ev, void *param)
{
Q_UNUSED(param);
switch (ev->type) {
case libvlc_MediaPlayerTimeChanged:
break;
case libvlc_MediaPlayerEndReached://不能在回调里调用stop();否则会卡死
break;
case libvlc_MediaPlayerStopped:
qDebug() << "libvlc_MediaPlayerStopped";
break;
case libvlc_MediaPlayerPlaying:
qDebug() << "libvlc_MediaPlayerPlay";
break;
case libvlc_MediaPlayerPaused:
qDebug() << "libvlc_MediaPlayerPaused";
break;
}
}
//释放
void MainWindow::freeVlc()
{
if(m_media){
libvlc_media_release(m_media);
m_media = Q_NULLPTR;
}
if(m_mediaPlayer){
libvlc_media_player_stop(m_mediaPlayer);
libvlc_media_player_release(m_mediaPlayer);
m_mediaPlayer = Q_NULLPTR;
}
if(m_vlcInstance){
libvlc_release(m_vlcInstance);
m_vlcInstance = Q_NULLPTR;
}
}
void MainWindow::on_pushButton_clicked()
{
if(ui->pushButton->text() == "play") {
play();
ui->pushButton->setText("pause");
} else if(ui->pushButton->text() == "pause") {
pause();
ui->pushButton->setText("play");
}
}
void MainWindow::on_pB_Stop_clicked()
{
stop();
ui->pushButton->setText("play");
}
void MainWindow::on_pB_Open_clicked()
{
if(m_vlcInstance) {
freeVlc();
}
m_fileDialog->show();
if(m_fileDialog->exec() == QDialog::Accepted) {
QString path = m_fileDialog->selectedFiles()[0];
ui->lB_Path->setText(path);
load(path);
}
}