If we use QML directly, we can easily use the following code to play our video file:
Rectangle {
width:800
height:600
color: "Black"
MediaPlayer {
id:player
Source: "file:// VIDEO.WEBM "
autoplay:true
}
videooutput {
id:videooutput
source:player
Anchors.fill: Parent
}
}
If we use our QT C + + code directly, we can also use Qmediaplayer to combine our qwidget to play our video files. We can find a corresponding example in the QT SDK.
If, if our project needs, we need to add playlist management in C + + code, so we have to use Qmediaplayer to play our multimedia files, but we need to display our video in QML. So how can we show our video in QML?
For this purpose, we can define a class called Mymediaplayer. Mymediaplay.h
#ifndef mymediaplayer_h
#define MYMEDIAPLAYER_H
#include <QMediaPlayer>
#include < Qabstractvideosurface>
class Mymediaplayer:public Qmediaplayer
{
q_object public
:
q_ Property (qabstractvideosurface* videosurface READ getvideosurface WRITE setvideosurface)
Q_property (QString FileName READ filename WRITE setfilename)
q_invokable void Play ();
Public:
Mymediaplayer (QObject * parent = 0, Flags flags = 0);
QString fileName () const;
void Setfilename (const QString &);
Public slots:
void Setvideosurface (qabstractvideosurface* surface);
qabstractvideosurface* getvideosurface ();
void Onmetadataavailablechanged (bool available);
Private:
qabstractvideosurface* m_surface;
QString m_filname;
};
#endif//Mymediaplayer_h
Mymediaplayer.cpp
#include "mymediaplayer.h" void Mymediaplayer::p lay () {qdebug () << "Play ...";
Qmediaplayer::setmedia (Qurl::fromlocalfile (m_filname));
Qmediaplayer::p lay ();
QString strtitle = Qmediaplayer::metadata ("Title"). ToString ();
QString strsize= qmediaplayer::metadata ("Size"). ToString ();
Qdebug () << "title:" + strtitle + "size:" + strsize;} Mymediaplayer::mymediaplayer (qobject* parent, flags Flags): Qmediaplayer (parent, flags) {Connect (this, SIGNAL (metadat
Aavailablechanged (BOOL)), this, SLOT (onmetadataavailablechanged (bool));
} void Mymediaplayer::setvideosurface (qabstractvideosurface* surface) {qdebug () << "changing surface";
M_surface = surface;
Setvideooutput (M_surface);
} qabstractvideosurface* Mymediaplayer::getvideosurface () {return m_surface;} void Mymediaplayer::onmetadataavailablechanged (bool available) {//Data display Qdebug () << "Onmetadataavailablecha
Nged "; if (available) {foreach (QString str,avAilablemetadata ()) {qdebug () <<str<< ":" <<metadata (str). toString () ToUtf8 (). data ();
}//playlist->setcurrentindex (++count);
} QString Mymediaplayer::filename () const {return m_filname;}
void Mymediaplayer::setfilename (const QString &filename) {m_filname = FileName;}
Our main.cpp can be written as:
main.cpp
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include "mymediaplayer.h"
int main (int argc, char *argv[])
{
Qcoreapplication::setattribute (Qt::aa_ enablehighdpiscaling);
Qguiapplication app (argc, argv);
mymediaplayer* player = new Mymediaplayer ();
Qqmlapplicationengine engine;
Engine.rootcontext ()->setcontextproperty ("Mymediaplayer", player);
Engine.load (Qurl) (Qlatin1string ("qrc:/main.qml"));
return app.exec ();
}
MAIN.QML
Import Qtquick 2.7
import qtquick.controls 2.0
Import qtquick.layouts 1.0
import Qtmultimedia 5.8
Import Qtquick.dialogs 1.0
Applicationwindow {
visible:true
width:640
height:480
(" Video Player "]
filedialog {
id:filedialog
title:" Please choose a file "
Folder:shortcuts.home
onaccepted: {
console.log ("You chose:" + filedialog.fileurls)
var path = "" + Filedialog.fileurl
var fileName = path.replace ("file://", "" ")
console.log (" filename: "+ filename)
mymediaplayer.filename = FileName
mymediaplayer.play ()
}
onrejected: {
console.log ("Canceled")
}
Component.onCompleted:visible = True
}
videooutput {
id:videooutput
anchors.fill:parent
Source:mymediaplayer
}
}
We can play our video files:
Https://github.com/liu-xiao-guo/mediaplayer