【QT】视频播放
在网上没找到,在书上也没有。后来突然想直接在官网的类里面找Video 居然就有了。
把http://qt-project.org/doc/qt-5/qmediaplayer.html的例子补充完整后就可以运行了。
注意pro文件中要添加内容,还有各种必要的头文件要添加。
开发环境Qt 5.3.2
代码如下:
pro文件
QT += core gui\ multimedia\ multimediawidgets greaterThan(QT_MAJOR_VERSION, 4): QT += widgets\ TARGET = Video_Try TEMPLATE = app SOURCES += main.cpp\ mainwindow.cpp HEADERS += mainwindow.h FORMS += 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_OBJECT public: 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/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(); }