Cannot play movie with QMediaPlayer

1k views Asked by At

I try to play movie with Qt5 on OS X El Capitan v10.11.6. I use QMediaPlayer, QMediaPlaylist, and QVideoWidget to play.

Write source code as same as Qt's documentation, but it show only black window and not play any movie.

Here is my source code.

main.cpp

#include <QApplication>

#include "mainwindow.h"

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

    MainWindow mainwindow;
    mainwindow.show();

    return app.exec();
}

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QWidget>

class QMediaPlayer;
class QMediaPlaylist;
class QVideoWidget;

class MainWindow : public QWidget
{
    Q_OBJECT

public:
    MainWindow(QWidget* parent = 0);

private:
    QMediaPlayer* player;
    QMediaPlaylist* playlist;
    QVideoWidget* videoWidget;
};

#endif

mainwindow.cpp

#include <QtWidgets>
#include <QMediaPlayer>
#include <QMediaPlaylist>
#include <QVideoWidget>

#include "mainwindow.h"

MainWindow::MainWindow(QWidget* parent)
    : QWidget(parent)
{
    player = new QMediaPlayer;
    playlist = new QMediaPlaylist;
    videoWidget = new QVideoWidget;

    player->setPlaylist(playlist);
    player->setVideoOutput(videoWidget);

    playlist->addMedia(QUrl::fromLocalFile("box.mp4"));

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

    QHBoxLayout* mainLayout = new QHBoxLayout;
    mainLayout->addWidget(videoWidget);

    setLayout(mainLayout);
}

I check "box.mp4" exists in the same directory.

Where is problem? How should I fix source code to solve this problem?

1

There are 1 answers

0
AudioBubble On BEST ANSWER

Just modify media file path to full path in mainwindow.cpp.

Before

playlist->addMedia(QUrl::fromLocalFile("box.mp4"));

After

playlist->addMedia(QUrl::fromLocalFile("/path/to/box.mp4"));