马上工作需要做视频软件了,所以准备学习QT来开发,为什么选择QT呢,因为他优点多多(不说了自己网上g一下
)
随着高清的不断普及,所有视频软件都向高清这个方向而前进,其中有一款开源跨平台的播放器vlc的特性和功能都是非
常的好,所以想准备学习vlc的源码来看看,在看他源码的时候,发现他还有一个可供开发人员调用的libvlc 媒体库可用(GPL),而vlc本身使用的GUI就是QT。
好了不多说了
先装好QT SDK,然后把环境变量设置一下(不会去G!)
然后把vlc库加入到mingw里面
先复制vlc-include.rar解压出来的头文件到Qt\2009.04\mingw\include里面
在把vlc-lib.rar解压出来的文件放到C:\vlc-lib
我们在 Qt Creator 里面建立一个空的QT项目
新建一个Player类 ,会多出player.cpp,player.h 修改成如下
player.cpp
/*
* Flie Name player.cpp
*/
#include "player.h"
#include <QVBoxLayout>
#include <QPushButton>
#include <QSlider>
#include <QTimer>
#include <QFrame>
Player::Player()
: QWidget()
{
//preparation of the vlc command
const char * const vlc_args[] = {
"-I", "dummy", /* Don't use any interface */
"--ignore-config", /* Don't use VLC's config */
"--extraintf=logger", //log anything
"--verbose=2", //be much more verbose then normal for debugging purpose
"--plugin-path=C:\\vlc-0.9.9-win32\\plugins\\" };
_videoWidget=new QFrame(this);
_volumeSlider=new QSlider(Qt::Horizontal,this);
_volumeSlider->setMaximum(100); //the volume is between 0 and 100
_volumeSlider->setToolTip("Audio slider");
// Note: if you use streaming, there is no ability to use the position slider
_positionSlider=new QSlider(Qt::Horizontal,this);
_positionSlider->setMaximum(POSITION_RESOLUTION);
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(_videoWidget);
layout->addWidget(_positionSlider);
layout->addWidget(_volumeSlider);
setLayout(layout);
_isPlaying=false;
poller=new QTimer(this);
//Initialize an instance of vlc
//a structure for the exception is neede for this initalization
libvlc_exception_init(&_vlcexcep);
//create a new libvlc instance
_vlcinstance=libvlc_new(sizeof(vlc_args) / sizeof(vlc_args[0]), vlc_args,&_vlcexcep); //tricky calculation of the char space used
raise (&_vlcexcep);
// Create a media player playing environement
_mp = libvlc_media_player_new (_vlcinstance, &_vlcexcep);
raise (&_vlcexcep);
//connect the two sliders to the corresponding slots (uses Qt's signal / slots technology)
connect(poller, SIGNAL(timeout()), this, SLOT(updateInterface()));
connect(_positionSlider, SIGNAL(sliderMoved(int)), this, SLOT(changePosition(int)));
connect(_volumeSlider, SIGNAL(sliderMoved(int)), this, SLOT(changeVolume(int)));
poller->start(100); //start timer to trigger every 100 ms the updateInterface slot
}
//desctructor
Player::~Player()
{
/* Stop playing */
libvlc_media_player_stop (_mp, &_vlcexcep);
/* Free the media_player */
libvlc_media_player_release (_mp);
libvlc_release (_vlcinstance);
raise (&_vlcexcep);
}
void Player::playFile(QString file)
{
//the file has to be in one of the following formats /perhaps a little bit outdated)
/*
[file://]filename Plain media file
http://ip:port/file HTTP URL
ftp://ip:port/file FTP URL
mms://ip:port/file MMS URL
screen:// Screen capture
[dvd://][device][@raw_device] DVD device
[vcd://][device] VCD device
[cdda://][device] Audio CD device
udp:[[<source address>]@[<bind address>][:<bind port>]]
*/
/* Create a new LibVLC media descriptor */
_m = libvlc_media_new (_vlcinstance, file.toAscii(), &_vlcexcep);
raise(&_vlcexcep);
libvlc_media_player_set_media (_mp, _m, &_vlcexcep);
raise(&_vlcexcep);
// /!\ Please note /!\
//
// passing the widget to the lib shows vlc at which position it should show up
// vlc automatically resizes the video to the ´given size of the widget
// and it even resizes it, if the size changes at the playing
/* Get our media instance to use our window */
#if defined(Q_OS_WIN)
libvlc_media_player_set_drawable(_mp, reinterpret_cast<unsigned int>(_videoWidget->winId()), &_vlcexcep );
//libvlc_media_player_set_hwnd(_mp, _videoWidget->winId(), &_vlcexcep ); // for vlc 1.0
#elif defined(Q_OS_MAC)
libvlc_media_player_set_drawable(_mp, _videoWidget->winId(), &_vlcexcep );
//libvlc_media_player_set_agl (_mp, _videoWidget->winId(), &_vlcexcep); // for vlc 1.0
#else //Linux
libvlc_media_player_set_drawable(_mp, _videoWidget->winId(), &_vlcexcep );
//libvlc_media_player_set_xwindow(_mp, _videoWidget->winId(), &_vlcexcep ); // for vlc 1.0
#endif
raise(&_vlcexcep);
/* Play */
libvlc_media_player_play (_mp, &_vlcexcep );
raise(&_vlcexcep);
_isPlaying=true;
}
void Player::changeVolume(int newVolume)
{
libvlc_exception_clear(&_vlcexcep);
libvlc_audio_set_volume (_vlcinstance,newVolume , &_vlcexcep);
raise(&_vlcexcep);
}
void Player::changePosition(int newPosition)
{
libvlc_exception_clear(&_vlcexcep);
// It's possible that the vlc doesn't play anything
// so check before
libvlc_media_t *curMedia = libvlc_media_player_get_media (_mp, &_vlcexcep);
libvlc_exception_clear(&_vlcexcep);
if (curMedia == NULL)
return;
float pos=(float)(newPosition)/(float)POSITION_RESOLUTION;
libvlc_media_player_set_position (_mp, pos, &_vlcexcep);
raise(&_vlcexcep);
}
void Player::updateInterface()
{
if(!_isPlaying)
return;
// It's possible that the vlc doesn't play anything
// so check before
libvlc_media_t *curMedia = libvlc_media_player_get_media (_mp, &_vlcexcep);
libvlc_exception_clear(&_vlcexcep);
if (curMedia == NULL)
return;
float pos=libvlc_media_player_get_position (_mp, &_vlcexcep);
int siderPos=(int)(pos*(float)(POSITION_RESOLUTION));
_positionSlider->setValue(siderPos);
int volume=libvlc_audio_get_volume (_vlcinstance,&_vlcexcep);
_volumeSlider->setValue(volume);
}
void Player::raise(libvlc_exception_t * ex)
{
if (libvlc_exception_raised (ex))
{
fprintf (stderr, "error: %s\n", libvlc_exception_get_message(ex));
exit (-1);
}
}
player.h
/* libVLC and Qt sample code
* Copyright © 2009 Alexander Maringer <maringer@maringer-it.de>
*/
#ifndef VLC_ON_QT_H
#define VLC_ON_QT_H
#include <vlc/vlc.h>
#include <QWidget>
class QVBoxLayout;
class QPushButton;
class QTimer;
class QFrame;
class QSlider;
#define POSITION_RESOLUTION 10000
class Player : public QWidget
{
Q_OBJECT
QSlider *_positionSlider;
QSlider *_volumeSlider;
QFrame *_videoWidget;
QTimer *poller;
bool _isPlaying;
libvlc_exception_t _vlcexcep;
libvlc_instance_t *_vlcinstance;
libvlc_media_player_t *_mp;
libvlc_media_t *_m;
public:
Player();
~Player();
void raise(libvlc_exception_t * ex);
public slots:
void playFile(QString file);
void updateInterface();
void changeVolume(int newVolume);
void changePosition(int newPosition);
};
#endif
在新建立一个main.cpp 修改成如下
#include <QtGui/QApplication>
#include "player.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Player p;
p.resize(640,480);
p.playFile("#填写你要播的文件路径#");
p.show();
return a.exec();
}
然后我们修改 .pro文件 成如下
TARGET = test1
TEMPLATE = app
LIBS += -L'C:\vlc-lib'
LIBS += -lvlc
SOURCES += player.cpp \
main.cpp
HEADERS += player.h
然后运行qmake
然后就可以编译了
编译成功以后想要运行,还必须把C:\vlc-lib里面的libvlc.dll,和libvlccore.dll拷贝到生成exe的目录里面才能运行
现在在qt creator里面就可以运行了(单独运行还需要其它dll)
haha
分享到:
相关推荐
课程设计C++开发基于Qt ffmpeg实现的视频播放器源码+详细注释+文档说明 Qt5.6.2(vs2013) + ffmpeg2.5.2 + SDL2 1.增加64位库,可自行判断编译器位数,并加载对应的库 2.代码结构调整(将底层播放器和Qt界面分开成2个...
这是基于libmpv设计的视频播放器最终版本的完整源码,包含源码文件、库文件、可执行文件。 在博客文章里也详细介绍了开发过程,其中第一篇文章里已经对libmpv做了详细的介绍,第二篇文章里完成了完整的播放器开发,...
软件开发设计:PHP、QT、应用软件开发、系统软件开发、移动应用开发、网站开发C++、Java、python、web、C#等语言的项目开发与学习资料 硬件与设备:单片机、EDA、proteus、RTOS、包括计算机硬件、服务器、网络设备、...
包含前端、后端、移动开发、操作系统、人工智能、物联网、信息化管理、数据库、硬件开发、大数据、课程资源、音视频、网站开发等各种技术项目的源码。 包括STM32、ESP8266、PHP、QT、Linux、iOS、C++、Java、python...
包含前端、后端、移动开发、操作系统、人工智能、物联网、信息化管理、数据库、硬件开发、大数据、课程资源、音视频、网站开发等各种技术项目的源码。 包括STM32、ESP8266、PHP、QT、Linux、iOS、C++、Java、python...
VLC-Qt是一个基于Qt框架的跨平台视频播放器开发库,可以方便地将VLC媒体播放器引入到您的Qt应用程序中。它是一个非常强大的工具,可以播放各种格式的音频和视频文件,并支持许多高级功能,如字幕,音轨,截图等。 ...
软件开发设计:PHP、QT、应用软件开发、系统软件开发、移动应用开发、网站开发C++、Java、python、web、C#等语言的项目开发与学习资料 硬件与设备:单片机、EDA、proteus、RTOS、包括计算机硬件、服务器、网络设备、...
包含前端、后端、移动开发、操作系统、人工智能、物联网、信息化管理、数据库、硬件开发、大数据、课程资源、音视频、网站开发等各种技术项目的源码。 包括STM32、ESP8266、PHP、QT、Linux、iOS、C++、Java、python...
包含前端、后端、移动开发、操作系统、人工智能、物联网、信息化管理、数据库、硬件开发、大数据、课程资源、音视频、网站开发等各种技术项目的源码。 包括STM32、ESP8266、PHP、QT、Linux、iOS、C++、Java、python...
Qt技术是一套强大的跨平台应用程序开发框架,专注于使用C++语言来构建高性能、高可维护性的软件解决方案。自1991年首次推出以来,Qt因其卓越的跨平台能力、丰富的功能集、高效的开发工具和良好的社区支持而广受...
Qt技术是一套强大的跨平台应用程序开发框架,专注于使用C++语言来构建高性能、高可维护性的软件解决方案。自1991年首次推出以来,Qt因其卓越的跨平台能力、丰富的功能集、高效的开发工具和良好的社区支持而广受...
Qt技术是一套强大的跨平台应用程序开发框架,专注于使用C++语言来构建高性能、高可维护性的软件解决方案。自1991年首次推出以来,Qt因其卓越的跨平台能力、丰富的功能集、高效的开发工具和良好的社区支持而广受...
Qt技术是一套强大的跨平台应用程序开发框架,专注于使用C++语言来构建高性能、高可维护性的软件解决方案。自1991年首次推出以来,Qt因其卓越的跨平台能力、丰富的功能集、高效的开发工具和良好的社区支持而广受...
包含前端、后端、移动开发、操作系统、人工智能、物联网、信息化管理、数据库、硬件开发、大数据、课程资源、音视频、网站开发等各种技术项目的源码。 包括STM32、ESP8266、PHP、QT、Linux、iOS、C++、Java、python...
Qt技术是一套强大的跨平台应用程序开发框架,专注于使用C++语言来构建高性能、高可维护性的软件解决方案。自1991年首次推出以来,Qt因其卓越的跨平台能力、丰富的功能集、高效的开发工具和良好的社区支持而广受...
毕设&课设&项目&实训-基于Qt实现的组态软件运行时系统原型:1. 具有模块化的图元代码设计,及基于该设计实现的组态编辑器动态生成Qt组态画 【项目资源】: 包含前端、后端、移动开发、操作系统、人工智能、物联网、...
包含前端、后端、移动开发、操作系统、人工智能、物联网、信息化管理、数据库、硬件开发、大数据、课程资源、音视频、网站开发等各种技术项目的源码。 包括STM32、ESP8266、PHP、QT、Linux、iOS、C++、Java、python...