How to use LibVLC with Qt 5

梦想的初衷 提交于 2019-12-08 05:13:18

问题


I'm trying to use LibVLC in a Qt 5 program to open a VLC instance and play a video. The following code comes from https://wiki.videolan.org/LibVLC_Tutorial/ I'm using Linux.

.pro :

TEMPLATE = app
TARGET = projectLoic
INCLUDEPATH += . vlc
QT += widgets
# Input
HEADERS += 
SOURCES += main.cpp
LIBS +=-lvlc

main :

#include <vlc/vlc.h>
#include <QApplication>

int main(int argc, char* argv[])
{
    QApplication app(argc, argv);


    libvlc_instance_t * inst;
    libvlc_media_player_t *mp;
    libvlc_media_t *m;

         // Load the VLC engine
         inst = libvlc_new(0, NULL);

         // Create a new item

         m = libvlc_media_new_path (inst, "/home/........mp3");

         // Create a media player playing environement
         mp = libvlc_media_player_new_from_media (m);

         // play the media_player
         libvlc_media_player_play (mp);

     return app.exec();
}

The compilation is fine. But the program immediatly crashes when I build it (with Qt Creator). Any idea?

Many thanks


回答1:


Many things could cause this crash. The best is to get VLC source code to trace back the issue. Passing the option '--verbose=2' when initializing libVLC can help as well.

In my case the cause of the crash was due to this bug in the ubuntu package of vlc: https://bugs.launchpad.net/ubuntu/+source/vlc/+bug/1328466

When calling libvlc_new() vlc modules and their dependent libraries are loaded into memory. The qt module of LibVLC was searching for Qt4 shared objects instead of Qt5 (manually installed).

The solution was to rebuild the module cache which was outdated an pointing to Qt4 binaries. You can reset it on the command line:

sudo /usr/lib/vlc/vlc-cache-gen -f /usr/lib/vlc/plugins/

Or pass to vlc the option:

--reset-plugins-cache



回答2:


I have never used this library, but Are you using exactly this code?

 m = libvlc_media_new_path (inst, "/home/........mp3");

This path may be the problem.




回答3:


What distribution of Linux are you using?

I ran into this same problem with Qt5 and LibVLC, and the primary cause (Ubuntu 12.04 LTS and 14.04 LTS), was that LibVLC was loading the qt4 interface plugin, which conflicted with Qt5. If you check your call stack, you'll most likely see that at a Qt4 library was being loaded, causing the crash.

If this is the problem, there are only 3 options (tested with LibVLC 2.2 and Qt5 on Ubuntu 12.04 and 14.04 LTS 64 bit).

The first (worst) is to delete the qt4 user interface plugin. You can test this is the problem by moving and running and then setting it back. Deleting will break your regular VLC player most likely.

Second option is to create a copy of the plugins directory and set that in your runtime path using VLC_PLUGIN_PATH, environment variable. but I've had trouble getting that to work without changing the original plugin path folder also (which will break your VLC player unless you modify your shortcuts, etc. to also set VLC_PLUGIN_PATH.

The third option, and what I ended up doing, was to custom build my own static LibVLC binary with a few edits to the source code so that it will not load the qt4 interface plugin installed with VLC. You can follow these steps to do this.

1)  Download VLC Source Code for your platforms distribution.

    http://download.videolan.org/pub/videolan/vlc/

    Make sure you download the version matching your distribution. For
    example, match the VLC version to what is installed with Ubuntu.

2)  Extract source code to folder
3)  Install dependencies for the OS distribution
    sudo apt-get build-dep vlc
4)  Modify src/modules/bank.c
    Edit the module_InitDynamic function
    Add the following code at the top of the function:

    // HACK TO DISABLE QT4 PLUGIN
    if(strstr(path, "qt4") != NULL)
        return NULL;
    // END HACK

3)  From terminal
    ./bootstrap
    ./configure --disable-qt --disable-skins2 --enable-xcb --prefix=/home/$USER/vlc-custom_build_output_folder_name
    ./make
    ./make install

4)  Copy/Save the resulting files in the install folder.

Then just link against this library instead.



来源:https://stackoverflow.com/questions/25068127/how-to-use-libvlc-with-qt-5

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!