[英]How to play a local video in a Qt Widgets Application, in Qt creator?
This is probably a very noob question... I am very new to Qt, and trying programming in Qt creator. 这可能是一个非常菜鸟的问题...我对Qt还是很陌生,并尝试在Qt Creator中进行编程。 Now I created a new Qt Widgets Application from Qt creator, and want to play a video with it. 现在,我从Qt创作者创建了一个新的Qt Widgets应用程序,并希望使用它来播放视频。 I now have the following 6 files in my project: 我的项目中现在有以下6个文件:
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)? 在运行应用程序时(也许使用一些称为QVideoPlayer的工具),我应该在哪里以及如何确切地编写代码以使其能够播放Demo.mp4? Should I add some Qwidget onto my videoplayer.ui? 我应该在我的videoplayer.ui上添加一些Qwidget吗? I now have four buttons "play", "pause", "full_screen" and "rearrange" on my videoplayer.ui. 现在,我的videoplayer.ui上有四个按钮“播放”,“暂停”,“全屏”和“重新排列”。 The result I want is something with features of: 我想要的结果具有以下特征:
You are looking for Qt Multimedia Widgets . 您在寻找Qt Multimedia Widgets 。 (You might need to install extra packages when running Linux). (运行Linux时,您可能需要安装其他软件包)。
The basic idea goes like this: 基本思路如下:
QVideoWidget
. 在UI方面,您可以使用QVideoWidget
。 This is where the video is displayed. 这是显示视频的地方。
.ui
file. 这就是您要添加到.ui
文件中的内容。 fullScreen
property. 注意fullScreen
属性。 QMediaPlayer
which controls what is played and when it's played. 在逻辑上,您可以使用QMediaPlayer
来控制播放的内容和播放的时间。
QMediaPlayer::setVideoOutput(yourVideoWidgetGoesHere);
通过调用QMediaPlayer::setVideoOutput(yourVideoWidgetGoesHere);
来连接QMediaPlayer::setVideoOutput(yourVideoWidgetGoesHere);
. 。 QMediaPlaylist
to your QMediaPlayer
. 然后,将QMediaPlaylist
添加到QMediaPlayer
。 QMediaPlayer::play()
and you should be good to go 最后调用QMediaPlayer::play()
,您应该会很好 Then you want some basic controls if this works so far. 然后,您需要一些基本的控件(如果到目前为止)。 QMediaPlayer
provides the following slots that exactly do as their names suggest: QMediaPlayer
提供了以下插槽,其名称完全相同:
pause()
play()
stop()
setPosition(int)
, argument is in milliseconds. setPosition(int)
,参数以毫秒为单位。 duration()
might be of interest. duration()
可能很有趣。 setVolume(int)
and setMuted(bool)
. setVolume(int)
和setMuted(bool)
。 Volume goes from 0 to 100. 音量从0到100。 setPlaybackRate(double)
metaData(QString key)
: http://qt-project.org/doc/qt-5/qmediaobject.html#metaData 可通过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. 它们每个都有一个相应的更改信号,您可能非常感兴趣的是positionChanged(int)
信号,用于更新滑块或与当前位置类似的内容。
Basic example courtesy of the Qt documentation: 基本示例由Qt文档提供:
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
您正在寻找的是在Qt 5
上实现QMediaPlayer ,在Qt 4.4
上Qt 4.4
QVideoPlayer 。
For QMediaPlayer
, you'll have to use a following implementation for adding one movie : 对于QMediaPlayer
,您必须使用以下实现来添加一部电影:
QMediaPlayer *player = new QMediaPlayer;
player->setMedia(QUrl::fromLocalFile("/users/somebody/somewhere/demo.mp4"));
player->setVolume(10);
player->play();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.