Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QMediaPlayer / QVideoWidget do not show



  • Hello every one
    I have a big problem with a VideoPlayer ,
    I can hear the audio but i can't see the video.

    I'm using Qt 5.6.2 on OsX El capitan

    here's my code

    VideoPlayer::VideoPlayer(VideoMetadata* meta, QWidget* parent)
        : ContentWidget(parent)
        , metadata(meta)
        , player(new QMediaPlayer(this))
        , video(new QVideoWidget)
    {
    
        // ContentWidget is QWidget  
        QVBoxLayout* layout = new QVBoxLayout;
        layout->addWidget(video);
        setLayout(layout);
        player->setVideoOutput(video);
        player->setMedia(QUrl::fromLocalFile("/Users/me/Desktop/VIDEO_TEST/video.mp4"));
        player->stop();
    
        connect(player, &QMediaPlayer::mediaStatusChanged,
            this, &VideoPlayer::mediaStateChanged);
    
        connect(player, &QMediaPlayer::positionChanged,
            this, &VideoPlayer::positionChanged);
    }
    
    VideoPlayer::~VideoPlayer()
    {
    }
    
    
    void VideoPlayer::mediaStateChanged(QMediaPlayer::MediaStatus state)
    {
        if (state == QMediaPlayer::MediaStatus::LoadedMedia) {
            player->play();
        }
    }
    

    this looks a bit strange because same code works fine in a separate application like:

    int main(int argc, char **argv)
    {
        QApplication app(argc, argv);
    
        QWidget *widget = new QWidget;
        widget->resize(400, 300);
        QVBoxLayout *layout = new QVBoxLayout;
        QMediaPlayer *player = new QMediaPlayer;
        QVideoWidget *vw = new QVideoWidget;
        layout->addWidget(vw);
        widget->setLayout(layout);
        player->setVideoOutput(vw);
        player->setMedia(QUrl::fromLocalFile("/Users/me/Desktop/VIDEO_TEST/video.mp4"));
        player->setVolume(50);
        player->play();
        widget->show();
        qDebug() << "mediaStatus: " << player->mediaStatus() << "error: " << player->error();
    
        return app.exec();
    }
    
    

    Thanks in advanced
    GG


  • Qt Champions 2019

    @GGAllin You don't call

    video->show();
    


  • thanks for reply
    but show() is called externally (when new VideoPlayer() is called)


  • Lifetime Qt Champion

    Hi
    So /Users/me/Desktop/VIDEO_TEST/video.mp4 does work in
    other code, on same pc ?


  • Lifetime Qt Champion

    Hi,

    Why are you calling stop in your custom widget constructor rather that play ?



  • @mrjj doesn't work just on VideoPlayer.cpp
    VideoPlayer.cpp doesn't work on OSx and Win, but on Linux work perfectly after installation of gstreamer0.10-ffmpeg

    here works perfectly

    int main(int argc, char **argv)
    {
        QApplication app(argc, argv);
    
        QWidget *widget = new QWidget;
        widget->resize(400, 300);
        QVBoxLayout *layout = new QVBoxLayout;
        QMediaPlayer *player = new QMediaPlayer;
        QVideoWidget *vw = new QVideoWidget;
        layout->addWidget(vw);
        widget->setLayout(layout);
        player->setVideoOutput(vw);
        player->setMedia(QUrl::fromLocalFile("/Users/me/Desktop/VIDEO_TEST/video.mp4"));
        player->setVolume(50);
        player->play();
        widget->show();
        qDebug() << "mediaStatus: " << player->mediaStatus() << "error: " << player->error();
    
        return app.exec();
    }
    

    @SGaist : i changed from stop to play in my contructor, but nothing's changed
    ps: sorry for my bad english!


  • Lifetime Qt Champion

    Works fine on macOS, just change the path for the video.

    #include <QApplication>
    #include <QWidget>
    #include <QVBoxLayout>
    
    #include <QMediaPlayer> 
    #include <QVideoWidget>
    
    class Widget : public QWidget
    {
    public:
        Widget(QWidget *parent = 0)
            : QWidget(parent)
        {
            QVBoxLayout *layout = new QVBoxLayout(this);
            QMediaPlayer *player = new QMediaPlayer(this);
            QVideoWidget *vw = new QVideoWidget;
            layout->addWidget(vw);
            player->setVideoOutput(vw);
            player->setMedia(QUrl::fromLocalFile("/path/to/file"));
            player->setVolume(50);
            player->play();
        }
    };
    
    int main(int argc, char **argv)
    {
        QApplication app(argc, argv);
    
        Widget widget;
        widget.resize(400, 300);
        widget.show();
    
        return app.exec();
    }
    

Log in to reply