首页 > 代码库 > 【QT】视频播放

【QT】视频播放

在网上没找到,在书上也没有。后来突然想直接在官网的类里面找Video 居然就有了。

把http://qt-project.org/doc/qt-5/qmediaplayer.html的例子补充完整后就可以运行了。

注意pro文件中要添加内容,还有各种必要的头文件要添加。

 

开发环境Qt 5.3.2 

代码如下:

pro文件

QT       += core gui            multimedia            multimediawidgetsgreaterThan(QT_MAJOR_VERSION, 4): QT += widgetsTARGET = Video_TryTEMPLATE = appSOURCES += main.cpp        mainwindow.cppHEADERS  += mainwindow.hFORMS    += mainwindow.ui

mainwindow.h

#ifndef MAINWINDOW_H#define MAINWINDOW_H#include <QMainWindow>#include <QMediaPlayer>#include <QMediaPlaylist>#include <QVideoWidget>#include <QUrl>class MainWindow : public QMainWindow{    Q_OBJECTpublic:    QMediaPlaylist * playlist;    QMediaPlayer * player;    QVideoWidget * videoWidget;    MainWindow();    ~MainWindow();};#endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"#include "ui_mainwindow.h"MainWindow::MainWindow(){    playlist = new QMediaPlaylist;    player = new QMediaPlayer;    videoWidget = new QVideoWidget;    setCentralWidget(videoWidget);    playlist->addMedia(QUrl::fromLocalFile("D:/Users/zhuwenqian/Qt_project/Video_Try/3.avi"));    playlist->setCurrentIndex(1);    player->setPlaylist(playlist);    player->setVideoOutput(videoWidget);    videoWidget->show();    player->play();}MainWindow::~MainWindow(){}

main

#include "mainwindow.h"#include <QApplication>int main(int argc, char *argv[]){    QApplication a(argc, argv);    MainWindow w;    w.show();    return a.exec();}

 

【QT】视频播放