Qvideowidget displays nothing

Asked

Viewed 127 times

11

I’m trying to use the classes QMediaPlayer and QVideoWidget to display a video on a system I’m producing. Only these class rays don’t work as expected. The QVideoWidget just doesn’t show up, doesn’t show the video, doesn’t do anything!

THE MCVE

To illustrate the problem, I built a Minimum, Complete and Verifiable Example on the basis of in the Qt 5 documentation example itself. Below are the configuration files of Cmake (for setting up the environment - i.e., creating the Visual Studio or Makefile project) and code:

Cmakelists.txt:

cmake_minimum_required(VERSION 2.8.11)
project (Teste)

if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
  set(CMAKE_BUILD_TYPE Release CACHE STRING "Choose the type of build." FORCE)
  set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release"
    "MinSizeRel" "RelWithDebInfo")
endif()

# Configuração do Qt
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC ON)
find_package(Qt5Widgets REQUIRED)
find_package(Qt5Multimedia REQUIRED)
find_package(Qt5MultimediaWidgets REQUIRED)

# Cria o executável
if(WIN32)
    add_executable(Teste WIN32 main.cpp)
else()
    add_executable(Teste main.cpp)
endif()

# Adiciona as bibliotecas necessárias
target_link_libraries(Teste Qt5::Widgets Qt5::Multimedia Qt5::MultimediaWidgets)

main.cpp:

#include <QApplication>
#include <QMediaPlayer>
#include <QVideoWidget>
#include <QMediaPlaylist>

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

    QMediaPlayer *player;
    QMediaPlaylist *playlist;
    QVideoWidget *videoWidget;

    // Código tirado do exemplo em: http://doc.qt.io/qt-5/qvideowidget.html#details
    // -------------------------
    player = new QMediaPlayer;

    playlist = new QMediaPlaylist(player);
    playlist->addMedia(QUrl("http://www.sample-videos.com/video/mp4/720/big_buck_bunny_720p_1mb.mp4"));
    playlist->addMedia(QUrl("http://techslides.com/demos/sample-videos/small.mp4"));

    videoWidget = new QVideoWidget;
    player->setVideoOutput(videoWidget);

    videoWidget->show();
    playlist->setCurrentIndex(1);
    player->play();
    // -------------------------

    return app.exec();
}

The Problem

When I run the code, the component (QVideoWidget) apparently not "is created" (despite being shown, since to ensure that the problem is not another I call show directly on the bastard), because the window appears "shortened" (without any standard size):

inserir a descrição da imagem aqui

But even if I use the mouse to resize the window, there is nothing displayed:

inserir a descrição da imagem aqui

The code does not generate any exceptions or errors. In the minimum example I use video Urls just to facilitate the tests (and as it is in the documentation), but on my local system I read local files and the problem is exactly the same.

The Environment

My development environment is:

  • Windows 10 64 bits
  • Cmake 3.5.0-rc3
  • Qt 5.6.0 32 bits
  • Visual Studio 2015

EDIT

I just tested on Ubuntu (16.04 64 bits) with Qt 5.7 (64 bits) and the problem is exactly the same:

inserir a descrição da imagem aqui

  • 2

    I found a little strange your test with external urls, maybe it is necessary to download first, because the player does not pass the data pro encoder if the download does not finish, maybe it recognizes fractions of the file, but it may still be necessary to create a temporary Handler. Tell me, tried with local files?

  • 3

    Ah, even your questions come here to "humiliate" people who can’t post anything right :D Pity I can’t help ;)

  • 1

    @Guilhermenascimento In my code I use local files, and it doesn’t work either. I only used external Urls because it makes it easier to build [mcve]. Anyway, it should work equally (and as the example of the documentation). :)

  • 2

    @bigown Opa, sorry ai. rs :)

  • Just ask me a question, prompted the necessary codecs? If running in DEBUG mode appear errors of attempts to use the codecs.

  • @Guilhermenascimento I believe so. Because my machine runs the videos (the places I try to run are .mp4) on VLC without problems for example. In addition, I have already run in DEBUG and no error has appeared. But, now you take me a doubt: the minimum example I did runs normally there for you?

Show 1 more comment

1 answer

4


I tested his example and he really doesn’t even try to run the file, when looking at the example he sent from the site it shows like this:

playlist = new QMediaPlaylist(player);

It seems to me that there are some errors in the documentation, the QMediaPlaylist you do not have the parameter to receive QMediaPlayer, but rather a QObject, that is to say it should only be to define the parent, has no relationship with creating the player, it seems an error of their tutorial, see how the class is QMediaPlaylist:

QMediaPlaylist(QObject *parent = 0);
virtual ~QMediaPlaylist();

Another thing I noticed when adding the "media items" you used QUrl, but I believe the right thing to do QMediaContent, for both remote and local files.

Another thing I noticed that it is necessary to set the minimum size of the QVideoWidget, otherwise he will render as 0, it is also possible to add it inside a QWidget or QMainWindow customized.

The @Luizviera also gave me a detail, the QUrl does not add the file:/// in local files, so it is necessary to use QUrl::fromLocalFile.

The code for basic operation would be like this:

QMediaContent videoItem1(QUrl::fromLocalFile("C:/Users/Guilherme/exemplo.mp4"));
playlist->addMedia(videoItem1);

I believe the right way is this:

player = new QMediaPlayer;

playlist = new QMediaPlaylist();

//Define o playlist no player
player->setPlaylist(playlist);

//item index 0
playlist->addMedia(QMediaContent(QUrl::fromLocalFile("http://www.sample-videos.com/video/mp4/720/big_buck_bunny_720p_1mb.mp4")));

//item index 1
playlist->addMedia(QMediaContent(QUrl::fromLocalFile("http://techslides.com/demos/sample-videos/small.mp4")));

videoWidget = new QVideoWidget;
player->setVideoOutput(videoWidget);

videoWidget->show();

//Define o minimo do tamanho do video
videoWidget->setMinimumSize(100, 100);

playlist->setCurrentIndex(1);
player->play();

Only one note: a similar message appears in the Qt log (under Windows):

DirectShowPlayerService::doRender: Unresolved error code 80040266

It is due to lack of Codecs for x86/32bit version (as per your version)

One thing you can add is the events to check for other errors (change [SUA CLASSE] by the class where will organize the main, perhaps the custom window of a player):

  • Your file .h usually:

    public slots:
        void statusChanged(QMediaPlayer::MediaStatus status);
        void displayErrorMessage();
    
  • Your file .cpp usually:

    void [SUA CLASSE]::statusChanged(QMediaPlayer::MediaStatus status)
    {
        switch (status) {
        case QMediaPlayer::UnknownMediaStatus:
        case QMediaPlayer::NoMedia:
        case QMediaPlayer::LoadedMedia:
        case QMediaPlayer::BufferingMedia:
        case QMediaPlayer::BufferedMedia:
            qDebug() << status;
            break;
        case QMediaPlayer::LoadingMedia:
            qDebug() << "Loading...";
            break;
        case QMediaPlayer::StalledMedia:
            qDebug() << "Media Stalled";
            break;
        case QMediaPlayer::EndOfMedia:
            qDebug() << "Media end";
            break;
        case QMediaPlayer::InvalidMedia:
            displayErrorMessage();
            break;
        }
    }
    
    void [SUA CLASSE]::displayErrorMessage()
    {
        qDebug() << player->errorString();
    }
    
  • And add the signals to the SLOTS:

    connect(player, SIGNAL(error(QMediaPlayer::Error)), this, SLOT(displayErrorMessage()));
    connect(player, SIGNAL(mediaStatusChanged(QMediaPlayer::MediaStatus)), this, SLOT(statusChanged(QMediaPlayer::MediaStatus)));
    

There is also a complete example to study in the documentation:

A test carried out:

video gato

video link: http://9gag.com/gag/adj442N/cat-exe-stops-working

  • @Luizvieira I just noticed two things, the first is that due to the Qvideowidget not having fixed width to height the screen is blank, you need to set, or else add your Qvideowidget to an existing window, in both cases the video worked :)

  • 1

    Exactly. It worked. To recap, the problems are then: 1) the QVideoWidget does not have a standard size and needs to be adjusted manually (I even noticed that if you leave it to the user to make the adjustment with the mouse, the sound works but the video does not render correctly); 2) to open local files is necessary to use QUrl::fromLocalFile, because the url accurate have "file:///" at the beginning; 3) continue...

  • 1
    1. Documentation contains important errors: it is necessary to build a QMediaContent and uploading a remote file (from an "http://..." URL) is not automatically handled by QVideoWidget. I will already accept your answer, but if you can do a more succinct summary it will be useful for other people in the future (because I think we should remove all these comments). Another thing: add in your reply the link to the most complete example, that works and is much more useful.
  • Ah, remove the part that says the problem is due to lack of Codecs (because it’s not). And thanks again for the help! :)

  • 1

    @Luizvieira I was creating an example that resizes the Qvideowidget, as soon as I can finish I will add in the reply :)

  • 1

    @Luizvieira left the part of the codecs to help other users, I remember that many questions I saw on Soen speak of the problem, I think it is a useful addition to the community :)

  • @Luizvieira I edited the answer to add the details you quoted, only a note the Qvideowidget think does not treat, who makes the treatment is the Qmediaplayer, so much so that you can control the Network of his class.

  • Thank you William. :)

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.