QT播放MP4视频文件

上面是我创建的群聊,欢迎新朋友的加入。

1.安装需要的库

网址:http://www.open-terrain.org/index.php/Pong/August30th2016QMediaPlayerOnUbuntu16-04LTS

sudo apt-get install ubuntu-restricted-extras

sudo add-apt-repository ppa:mc3man / gstffmpeg-keep
sudo apt-get update
sudo apt-get install gstreamer0.10-ffmpeg

sudo apt-get install gstreamer0.10
sudo apt-get install gstreamer0.10-plugins-base
sudo apt-get install gstreamer0.10-plugins-good
sudo apt-get install gstreamer0.10-fluendo-mp3
sudo apt-get install gstreamer0.10-pulseaudio

2.QT界面

参考:https://www.cnblogs.com/zhangyi-studio/p/8596837.html

QT播放MP4视频文件_第1张图片

3.头文件及模块

QT += multimedia
QT += multimediawidgets
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include 

#include 
#include 
#include 
#include 

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
    void addToPlaylist(const QStringList& fileNames);

private slots:
    void openFile();
    void playVideo();
    void stopVideo();
    void fullScr();

private:
    Ui::MainWindow *ui;
    QMediaPlayer *player;
    QVideoWidget *videoWidget;
    QMediaPlaylist *Playlist;
    QString m_fileName;
    QMediaPlayer::State  m_playerState;

};

#endif // MAINWINDOW_H

4.源码

#include "mainwindow.h"
#include "ui_mainwindow.h"

#include 

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    //MainWindow自带layout,所以需要自定义layout并把它设置成中心layout
    //否则将会出现不能设置layout错误
    QWidget *widget = new QWidget;
    this->setCentralWidget(widget);

    player = new QMediaPlayer;

    Playlist = new QMediaPlaylist();
    player->setPlaylist(Playlist);

    videoWidget = new QVideoWidget(widget);
    player->setVideoOutput(videoWidget);
    ui->progressBar->setRange(0, player->duration() / 1000);

    //水平布局,控制按钮
    QBoxLayout *ctlLayout = new QHBoxLayout;
    ctlLayout->addWidget(ui->openFileBtn);
    ctlLayout->addWidget(ui->playBtn);
    ctlLayout->addWidget(ui->stopBtn);
    ctlLayout->addWidget(ui->fullScrBtn);

    //垂直布局:视频播放器、进度条、控制按钮布局
    QBoxLayout *mainLayout = new QVBoxLayout;
    mainLayout->addWidget(videoWidget);
    mainLayout->addWidget(ui->progressBar);
    mainLayout->addLayout(ctlLayout);

    //设置布局
    widget->setLayout(mainLayout);


    //信号槽
    QObject::connect(ui->openFileBtn,SIGNAL(clicked()),this,SLOT(openFile()));
    QObject::connect(ui->playBtn,SIGNAL(clicked()),this,SLOT(playVideo()));
    QObject::connect(ui->stopBtn,SIGNAL(clicked()),this,SLOT(stopVideo()));
    QObject::connect(ui->fullScrBtn,SIGNAL(clicked()),this,SLOT(fullScr()));
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::openFile()
{
    QStringList fileNames = QFileDialog::getOpenFileNames(this, tr("Open Files"));
    addToPlaylist(fileNames);
}

void MainWindow::playVideo()
{
    m_playerState = QMediaPlayer::PlayingState;
    player->play();
}

void MainWindow::stopVideo()
{
    m_playerState = QMediaPlayer::StoppedState;
    player->stop();
}

void MainWindow::fullScr()
{
    if(m_playerState == QMediaPlayer::PlayingState)
    {
        videoWidget->setFullScreen(true);
    }
    else
    {
        videoWidget->setFullScreen(false);
    }

}

void MainWindow::addToPlaylist(const QStringList& fileNames)
{
    foreach (QString const &argument, fileNames) {
        QFileInfo fileInfo(argument);
        if (fileInfo.exists()) {
            QUrl url = QUrl::fromLocalFile(fileInfo.absoluteFilePath());
            if (fileInfo.suffix().toLower() == QLatin1String("m3u")) {
                Playlist->load(url);
            } else
                Playlist->addMedia(url);
        } else {
            QUrl url(argument);
            if (url.isValid()) {
                Playlist->addMedia(url);
            }
        }
    }
}

5.Unknown module(s) in QT: multimedia

在ubuntu中解决方法如下:

qtmultimedia5-de

sudo apt-get install qtmultimedia5-dev

6.requestService(): no service found for - "org.qt-project.qt.mediaplayer"

sudo apt-get install gstreamer0.10-*

sudo apt-get install libqt5multimedia5-plugins

7.测试

QT播放MP4视频文件_第2张图片

6.源码

https://download.csdn.net/download/jun626/11607577

你可能感兴趣的:(QT播放MP4视频文件)