2016-12-25 79 views
3

我嘗試在OS X El Capitan v10.11.6上使用Qt5播放電影。 我使用QMediaPlayer,QMediaPlaylist和QVideoWidget進行播放。無法使用QMediaPlayer播放電影

編寫與Qt文檔相同的源代碼,但它只顯示黑色窗口,不播放任何電影。

這是我的源代碼。

的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」 在同一個目錄中。

問題在哪裏?我應該如何修復源代碼來解決這個問題?

+1

後'在二進制文件目錄box.mp4'?嘗試在命令行中運行這個程序,看看是否有一些輸出。 –

+0

添加完整路徑 – eyllanesc

+0

@Shueluel H:在命令行中運行程序,但得到相同的輸出。 –

回答

1

只需將媒體文件路徑修改爲完整路徑mainwindow.cpp即可。

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

之前

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