This is probably a very noob question... I am very new to Qt, and trying programming in Qt creator. Now I created a new Qt Widgets Application from Qt creator, and want to play a video with it. I now have the following 6 files in my project:
Where and how exactly should I write my code to make it play Demo.mp4 when I run my application (maybe using some tools called QVideoPlayer)? Should I add some Qwidget onto my videoplayer.ui? I now have four buttons "play", "pause", "full_screen" and "rearrange" on my videoplayer.ui. The result I want is something with features of:
You are looking for Qt Multimedia Widgets . (You might need to install extra packages when running Linux).
The basic idea goes like this:
QVideoWidget
. This is where the video is displayed.
.ui
file. fullScreen
property. QMediaPlayer
which controls what is played and when it's played.
QMediaPlayer::setVideoOutput(yourVideoWidgetGoesHere);
. QMediaPlaylist
to your QMediaPlayer
. QMediaPlayer::play()
and you should be good to go Then you want some basic controls if this works so far. QMediaPlayer
provides the following slots that exactly do as their names suggest:
pause()
play()
stop()
setPosition(int)
, argument is in milliseconds. duration()
might be of interest. setVolume(int)
and setMuted(bool)
. Volume goes from 0 to 100. setPlaybackRate(double)
metaData(QString key)
: http://qt-project.org/doc/qt-5/qmediaobject.html#metaData Each of these also has a corresponding change signal, very interesting for you is probably the positionChanged(int)
signal to update a slider or something similar with the current position.
Basic example courtesy of the Qt documentation:
player = new QMediaPlayer;
playlist = new QMediaPlaylist(player);
playlist->addMedia(QUrl("http://example.com/myclip1.mp4"));
playlist->addMedia(QUrl("http://example.com/myclip2.mp4"));
videoWidget = new QVideoWidget;
player->setVideoOutput(videoWidget);
videoWidget->show();
playlist->setCurrentIndex(1);
player->play();
What you are looking for is to implement a QMediaPlayer if you are on Qt 5
, a QVideoPlayer if you are on Qt 4.4
For QMediaPlayer
, you'll have to use a following implementation for adding one movie :
QMediaPlayer *player = new QMediaPlayer;
player->setMedia(QUrl::fromLocalFile("/users/somebody/somewhere/demo.mp4"));
player->setVolume(10);
player->play();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.