Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1522507
  • 博文数量: 290
  • 博客积分: 3468
  • 博客等级: 中校
  • 技术积分: 3461
  • 用 户 组: 普通用户
  • 注册时间: 2010-12-28 22:21
文章分类

全部博文(290)

文章存档

2016年(13)

2015年(3)

2014年(42)

2013年(67)

2012年(90)

2011年(75)

分类: 嵌入式

2013-04-30 19:11:53

下面的代码用是的 vlc-1.1.5 的库
main.cpp

点击(此处)折叠或打开

  1. /* libVLC and Qt sample code
  2.  * Copyright © 2009 Alexander Maringer <maringer@maringer-it.de>
  3.  */

  4. #include "vlc_on_qt.h"
  5. #include <QtGui/QApplication>

  6. int main(int argc, char *argv[])
  7. {
  8.     QApplication a(argc, argv);
  9.     Player p;
  10.     p.resize(640,480);
  11.   // p.playFile("rtsp://192.168.9.111:8554/webcam"); // Replace with what you want to play
  12. //    p.playFile("/media/soft/amusement/movie/aganzhengzhuan_01.rmvb");
  13.     p.show();
  14.     return a.exec();
  15. }

vlc_on_qt.h

点击(此处)折叠或打开

  1. /* libVLC and Qt sample code
  2.  * Copyright © 2009 Alexander Maringer <maringer@maringer-it.de>
  3.  */
  4. #ifndef VLC_ON_QT_H
  5. #define VLC_ON_QT_H

  6. #include <vlc/vlc.h>

  7. #include <QX11EmbedContainer>
  8. #include <QWidget>
  9. #include <QLabel>
  10. #include <QLineEdit>

  11. class QVBoxLayout;
  12. class QPushButton;
  13. class QTimer;
  14. class QFrame;
  15. class QSlider;

  16. #define POSITION_RESOLUTION 10000

  17. class Player : public QWidget
  18. {
  19.     Q_OBJECT
  20.     QSlider *_positionSlider;
  21.     QSlider *_volumeSlider;

  22.     QLabel *labelPath;
  23.     QLineEdit *lineEditPath;
  24.     QPushButton *buttonBrowser;
  25.     QPushButton *buttonPlay;
  26.     QString filename;

  27.     // [20101215 JG]
  28.     // Tested on Linux OpenSuse and VLC 1.2.0. This version of VLC is not completely compatible with previous versions of VLC.
  29.     // [20101201 Ondra Spilka]
  30.     // on Linux/Ubuntu Lucid and VLC >= 1.0 (at least 1.1.5 was tested) XWindow handle have to be passed
  31.     // therefore QX11EmbedContainer have to be used instead of QFrame
  32. #ifdef Q_WS_X11
  33.     QX11EmbedContainer *_videoWidget;
  34. #else
  35.     QFrame *_videoWidget;
  36. #endif
  37.     // [20101215 JG] If KDE is used like unique desktop environment, only use QFrame *_videoWidget;
  38.     QTimer *poller;
  39.     bool _isPlaying;
  40.     //libvlc_exception_t _vlcexcep; // [20101215 JG] Used for versions prior to VLC 1.2.0.
  41.     libvlc_instance_t *_vlcinstance;
  42.     libvlc_media_player_t *_mp;
  43.     libvlc_media_t *_m;

  44. public:
  45.     Player();
  46.     ~Player();
  47.     //void raise(libvlc_exception_t * ex); // [20101215 JG] Used for versions prior to VLC 1.2.0.

  48. public slots:
  49.     void playFile(QString file);
  50.     void updateInterface();
  51.     void changeVolume(int newVolume);
  52.     void changePosition(int newPosition);
  53.     void slotBrowser();
  54.     void slotPlay();

  55. };
  56. #endif
vlc_on_qt.cpp

点击(此处)折叠或打开

  1. /* libVLC and Qt sample code
  2.  * Copyright © 2009 Alexander Maringer <maringer@maringer-it.de>
  3.  */
  4. #include "vlc_on_qt.h"

  5. #include <QX11EmbedContainer>
  6. #include <QVBoxLayout>
  7. #include <QPushButton>
  8. #include <QSlider>
  9. #include <QTimer>
  10. #include <QFrame>
  11. #include <QFileDialog>
  12. #include <QMessageBox>
  13. #include <QDebug>

  14. Player::Player()
  15. : QWidget()
  16. {
  17.     //preparation of the vlc command
  18.     const char * const vlc_args[] = {
  19.               "-I", "dummy", /* Don't use any interface */
  20.               "--ignore-config", /* Don't use VLC's config */
  21.               "--extraintf=logger", //log anything
  22.               "--verbose=2", //be much more verbose then normal for debugging purpose
  23.               "--plugin-path=C:\\vlc-0.9.9-win32\\plugins\\" };

  24. #ifdef Q_WS_X11
  25.     _videoWidget=new QX11EmbedContainer(this);
  26. #else
  27.     _videoWidget=new QFrame(this);
  28. #endif
  29.     // [20101215 JG] If KDE is used like unique desktop environment, only use _videoWidget=new QFrame(this);

  30.     _volumeSlider=new QSlider(Qt::Horizontal,this);
  31.     _volumeSlider->setMaximum(100); //the volume is between 0 and 100
  32.     _volumeSlider->setToolTip("Audio slider");

  33.     // Note: if you use streaming, there is no ability to use the position slider
  34.     _positionSlider=new QSlider(Qt::Horizontal,this);
  35.     _positionSlider->setMaximum(POSITION_RESOLUTION);

  36.     labelPath = new QLabel(tr("PATH"));
  37.     lineEditPath = new QLineEdit();
  38.     buttonBrowser = new QPushButton(tr("Browser"));
  39.     buttonPlay = new QPushButton(tr("Play"));

  40.     QGridLayout *gLayout = new QGridLayout();
  41.     gLayout->addWidget(labelPath, 0, 0);
  42.     gLayout->addWidget(lineEditPath, 0, 1);
  43.     gLayout->addWidget(buttonBrowser, 0, 2);
  44.     gLayout->addWidget(buttonPlay, 1, 2);

  45.     QVBoxLayout *layout = new QVBoxLayout;
  46.     layout->addWidget(_videoWidget);
  47.     layout->addWidget(_positionSlider);
  48.     layout->addWidget(_volumeSlider);
  49.     layout->addLayout(gLayout);
  50.     setLayout(layout);

  51.     _isPlaying=false;
  52.     poller=new QTimer(this);

  53.     //Initialize an instance of vlc
  54.     //a structure for the exception is neede for this initalization
  55.     //libvlc_exception_init(&_vlcexcep); // [20101215 JG] Used for versions prior to VLC 1.2.0.

  56.     //create a new libvlc instance
  57.     _vlcinstance=libvlc_new(sizeof(vlc_args) / sizeof(vlc_args[0]), vlc_args); //tricky calculation of the char space used
  58.     //_vlcinstance=libvlc_new(sizeof(vlc_args) / sizeof(vlc_args[0]), vlc_args,&_vlcexcep); // [20101215 JG] Used for versions prior to VLC 1.2.0.
  59.     //raise (&_vlcexcep); // [20101215 JG] Used for versions prior to VLC 1.2.0.

  60.     // Create a media player playing environement
  61.     _mp = libvlc_media_player_new (_vlcinstance);
  62.     //_mp = libvlc_media_player_new (_vlcinstance, &_vlcexcep); // [20101215 JG] Used for versions prior to VLC 1.2.0.
  63.     //raise (&_vlcexcep); // [20101215 JG] Used for versions prior to VLC 1.2.0.

  64.     //connect the two sliders to the corresponding slots (uses Qt's signal / slots technology)
  65.     connect(poller, SIGNAL(timeout()), this, SLOT(updateInterface()));
  66.     connect(_positionSlider, SIGNAL(sliderMoved(int)), this, SLOT(changePosition(int)));
  67.     connect(_volumeSlider, SIGNAL(sliderMoved(int)), this, SLOT(changeVolume(int)));

  68.     poller->start(100); //start timer to trigger every 100 ms the updateInterface slot

  69.     connect(this->buttonBrowser, SIGNAL(clicked()), this, SLOT(slotBrowser()));
  70.     connect(this->buttonPlay, SIGNAL(clicked()), this, SLOT(slotPlay()));
  71. }

  72. //desctructor
  73. Player::~Player()
  74. {
  75.     /* Stop playing */
  76.     libvlc_media_player_stop (_mp);
  77.     //libvlc_media_player_stop (_mp, &_vlcexcep); // [20101215 JG] Used for versions prior to VLC 1.2.0.

  78.     /* Free the media_player */
  79.     libvlc_media_player_release (_mp);

  80.     libvlc_release (_vlcinstance);
  81.     //raise (&_vlcexcep); // [20101215 JG] Used for versions prior to VLC 1.2.0.
  82. }

  83. void Player::slotBrowser()
  84. {
  85.     filename = QFileDialog::getOpenFileName(this, tr("open file"), tr("/"), tr("file(*.avi *.rmvb *.h264)"));
  86.     this->lineEditPath->setText(filename);
  87. }

  88. void Player::slotPlay()
  89. {
  90.     if (filename.isEmpty()) {
  91.         filename = this->lineEditPath->text();

  92.         if (filename.isEmpty()) {
  93.             QMessageBox::warning(this, tr("play video"), tr("the path is null"));
  94.         }
  95.     }

  96.     qDebug() << "************************************" << filename ;
  97.     this->playFile(filename);
  98. }

  99. void Player::playFile(QString file)
  100. {
  101.     //the file has to be in one of the following formats /perhaps a little bit outdated)
  102.     /*
  103.     [file://]filename Plain media file
  104.     http://ip:port/file HTTP URL
  105.     ftp://ip:port/file FTP URL
  106.     mms://ip:port/file MMS URL
  107.     screen:// Screen capture
  108.     [dvd://][device][@raw_device] DVD device
  109.     [vcd://][device] VCD device
  110.     [cdda://][device] Audio CD device
  111.     udp:[[<source address>]@[<bind address>][:<bind port>]]
  112.     */

  113.     /* Create a new LibVLC media descriptor */
  114.     _m = libvlc_media_new_path(_vlcinstance, file.toAscii());
  115.     //_m = libvlc_media_new (_vlcinstance, file.toAscii(), &_vlcexcep); // [20101215 JG] Used for versions prior to VLC 1.2.0.
  116.     //raise(&_vlcexcep); // [20101215 JG] Used for versions prior to VLC 1.2.0.

  117.     libvlc_media_player_set_media (_mp, _m);
  118.     //libvlc_media_player_set_media (_mp, _m, &_vlcexcep); // [20101215 JG] Used for versions prior to VLC 1.2.0.
  119.     //raise(&_vlcexcep); // [20101215 JG] Used for versions prior to VLC 1.2.0.

  120.     // Please note
  121.     //
  122.     // passing the widget to the lib shows vlc at which position it should show up
  123.     // vlc automatically resizes the video to the ?given size of the widget
  124.     // and it even resizes it, if the size changes at the playing

  125.     /* Get our media instance to use our window */
  126.     #if defined(Q_OS_WIN)
  127.         libvlc_media_player_set_drawable(_mp, reinterpret_cast<unsigned int>(_videoWidget->winId()));
  128.         //libvlc_media_player_set_drawable(_mp, reinterpret_cast<unsigned int>(_videoWidget->winId()), &_vlcexcep ); // [20101215 JG] Used for versions prior to VLC 1.2.0.
  129.         //libvlc_media_player_set_hwnd(_mp, _videoWidget->winId(), &_vlcexcep ); // for vlc 1.0
  130.     #elif defined(Q_OS_MAC)
  131.         libvlc_media_player_set_drawable(_mp, _videoWidget->winId());
  132.         //libvlc_media_player_set_drawable(_mp, _videoWidget->winId(), &_vlcexcep ); // [20101215 JG] Used for versions prior to VLC 1.2.0.
  133.         //libvlc_media_player_set_agl (_mp, _videoWidget->winId(), &_vlcexcep); // for vlc 1.0
  134.     #else //Linux
  135.         //[20101201 Ondrej Spilka] obsolete call on libVLC >=1.1.5
  136.         //libvlc_media_player_set_drawable(_mp, _videoWidget->winId(), &_vlcexcep );
  137.         //libvlc_media_player_set_xwindow(_mp, _videoWidget->winId(), &_vlcexcep ); // for vlc 1.0

  138.      /* again note X11 handle on Linux is needed
  139.         winID() returns X11 handle when QX11EmbedContainer us used */

  140.         int windid = _videoWidget->winId();
  141.         libvlc_media_player_set_xwindow (_mp, windid );

  142.     #endif
  143.     //raise(&_vlcexcep); // [20101215 JG] Used for versions prior to VLC 1.2.0.

  144.     /* Play */
  145.     libvlc_media_player_play (_mp);
  146.     //libvlc_media_player_play (_mp, &_vlcexcep ); // [20101215 JG] Used for versions prior to VLC 1.2.0.
  147.     //raise(&_vlcexcep); // [20101215 JG] Used for versions prior to VLC 1.2.0.

  148.     _isPlaying=true;
  149. }

  150. void Player::changeVolume(int newVolume)
  151. {
  152.     //libvlc_exception_clear(&_vlcexcep); // [20101215 JG] Used for versions prior to VLC 1.2.0.
  153.     libvlc_audio_set_volume (_mp,newVolume);
  154.     //libvlc_audio_set_volume (_vlcinstance,newVolume , &_vlcexcep); // [20101215 JG] Used for versions prior to VLC 1.2.0.
  155.     //raise(&_vlcexcep); // [20101215 JG] Used for versions prior to VLC 1.2.0.
  156. }

  157. void Player::changePosition(int newPosition)
  158. {
  159.     //libvlc_exception_clear(&_vlcexcep); // [20101215 JG] Used for versions prior to VLC 1.2.0.
  160.     // It's possible that the vlc doesn't play anything
  161.     // so check before
  162.     libvlc_media_t *curMedia = libvlc_media_player_get_media (_mp);
  163.     //libvlc_media_t *curMedia = libvlc_media_player_get_media (_mp, &_vlcexcep); // [20101215 JG] Used for versions prior to VLC 1.2.0.
  164.     //libvlc_exception_clear(&_vlcexcep); // [20101215 JG] Used for versions prior to VLC 1.2.0.
  165.     if (curMedia == NULL)
  166.         return;

  167.     float pos=(float)(newPosition)/(float)POSITION_RESOLUTION;
  168.     libvlc_media_player_set_position (_mp, pos);
  169.     //libvlc_media_player_set_position (_mp, pos, &_vlcexcep); // [20101215 JG] Used for versions prior to VLC 1.2.0.
  170.     //raise(&_vlcexcep); // [20101215 JG] Used for versions prior to VLC 1.2.0.
  171. }

  172. void Player::updateInterface()
  173. {
  174.     if(!_isPlaying)
  175.         return;

  176.     // It's possible that the vlc doesn't play anything
  177.     // so check before
  178.     libvlc_media_t *curMedia = libvlc_media_player_get_media (_mp);
  179.     //libvlc_media_t *curMedia = libvlc_media_player_get_media (_mp, &_vlcexcep); // [20101215 JG] Used for versions prior to VLC 1.2.0.
  180.     //libvlc_exception_clear(&_vlcexcep); // [20101215 JG] Used for versions prior to VLC 1.2.0.
  181.     if (curMedia == NULL)
  182.         return;

  183.     float pos=libvlc_media_player_get_position (_mp);
  184.     //float pos=libvlc_media_player_get_position (_mp, &_vlcexcep); // [20101215 JG] Used for versions prior to VLC 1.2.0.
  185.     int siderPos=(int)(pos*(float)(POSITION_RESOLUTION));
  186.     _positionSlider->setValue(siderPos);
  187.     int volume=libvlc_audio_get_volume (_mp);
  188.     //int volume=libvlc_audio_get_volume (_vlcinstance,&_vlcexcep); // [20101215 JG] Used for versions prior to VLC 1.2.0.
  189.     _volumeSlider->setValue(volume);
  190. }
  191. /*void Player::raise(libvlc_exception_t * ex)
  192. {
  193.     if (libvlc_exception_raised (ex))
  194.     {
  195.          fprintf (stderr, "error: %s\n", libvlc_exception_get_message(ex));
  196.          exit (-1);
  197.     }
  198. }*/ // [20101215 JG] Used for versions prior to VLC 1.2.0.
vlc_on_qt.pro

点击(此处)折叠或打开

  1. ######################################################################
  2. # Automatically generated by qmake (2.01a) Tue Apr 30 15:34:04 2013
  3. ######################################################################

  4. TEMPLATE = app
  5. TARGET =
  6. DEPENDPATH += .
  7. INCLUDEPATH += .

  8. # Input
  9. HEADERS += vlc_on_qt.h
  10. SOURCES += main.cpp vlc_on_qt.cpp


  11. INCLUDEPATH += /opt/vlc-1.1.5//include/
  12. LIBS += -L/opt/vlc-1.1.5/lib -lvlc


参考:

关于vlc 的编译:http://blog.chinaunix.net/uid-20648944-id-3637113.html

阅读(6409) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~