简体   繁体   English

如何在Qt Creator中的Qt Widgets应用程序中播放本地视频?

[英]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个文件:

  • Project 项目
    • Project.pro Project.pro
    • Headers
      • videoplayer.h videoplayer.h
    • Sources 来源
      • main.cpp main.cpp中
      • videoplayer.cpp videoplayer.cpp
    • Forms 形式
      • videoplayer.ui videoplayer.ui
    • Other files 其他文件
      • /Users/somebody/somewhere /用户/人/某处
        • Demo.mp4 Demo.mp4

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: 我想要的结果具有以下特征:

  • Press play button to play Demo.mp4 on the top-left of the entire window 按下播放按钮以播放整个窗口左上角的Demo.mp4
  • Press Pause to pause 按下暂停即可暂停
  • Press full_screen to enter full screen (if QMediaPlayer can enter/quit full screen itself, then this feature is not necessary) 按full_screen进入全屏(如果QMediaPlayer本身可以进入/退出全屏,则不需要此功能)
  • press rearrange to put the playing window from top-left to top-right 按重新排列将播放窗口从左上角移到右上角
  • additional: fast forward and rewind (again, if QVideoPlayer or something Qt provided not already has this feature) 另外:快进和快退(同样,如果QVideoPlayer或Qt提供的功能尚未具有此功能)

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: 基本思路如下:

  • On the UI side of things you use a QVideoWidget . 在UI方面,您可以使用QVideoWidget This is where the video is displayed. 这是显示视频的地方。
    • This is what you would add to your .ui file. 这就是您要添加到.ui文件中的内容。
    • Note the fullScreen property. 注意fullScreen属性。
  • On the logic side of things you use a QMediaPlayer which controls what is played and when it's played. 在逻辑上,您可以使用QMediaPlayer来控制播放的内容和播放的时间。
    • The two are connected by calling QMediaPlayer::setVideoOutput(yourVideoWidgetGoesHere); 通过调用QMediaPlayer::setVideoOutput(yourVideoWidgetGoesHere);来连接QMediaPlayer::setVideoOutput(yourVideoWidgetGoesHere); .
    • Then you add a QMediaPlaylist to your QMediaPlayer . 然后,将QMediaPlaylist添加到QMediaPlayer
    • Finally call 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提供了以下插槽,其名称完全相同:

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.4Qt 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM