无法使用 QMediaPlayer 播放电影

Cannot play movie with QMediaPlayer

我尝试在 OS X El Capitan v10.11.6 上使用 Qt5 播放电影。 我使用QMediaPlayer、QMediaPlaylist、QVideoWidget来播放

按照Qt的文档编写源代码,但只显示黑色window并且不播放任何电影

这是我的源代码。

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);
}

我检查 "box.mp4" 存在于同一目录中。

问题出在哪里?我应该如何修复源代码来解决这个问题?

只需将mainwindow.cpp中的媒体文件路径修改为完整路径即可。

之前

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

之后

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