媒体播放器示例

使用Qt Widgets.NET 播放音频和视频

Media Player演示了一个简单的多媒体播放器,可使用各种编解码器播放音频和视频文件。

运行示例

运行示例 Qt Creator,打开Welcome 模式,然后从Examples 中选择示例。更多信息,请参阅Qt Creator: 教程:构建和运行

该示例使用传入QVideoWidgetQMediaPlayer 对象来控制视频输出。为使应用程序具有播放列表功能,我们还使用了 QMediaPlaylist 对象。在 Qt 6 中,QMediaPlaylist 已从我们的 API 中删除,但本示例中包含了它的实现。

音频电平表显示播放过程中的峰值和有效值电平。实现的方法是将QAudioBufferOutput 设置为QMediaPlayer ,在单独的线程上处理它发出的每个QAudioBuffer ,并通过QWidget::paintEvent 可视化这些值。

为了激活对话框上的播放和停止等各种功能,点击按钮事件会发出播放()和停止()信号,这些信号与QMediaPlayer 的播放()和停止()插槽相连。

connect(controls, SIGNAL(play()), player, SLOT(play()));
connect(controls, SIGNAL(pause()), player, SLOT(pause()));
connect(controls, SIGNAL(stop()), player, SLOT(stop()));

我们可以获取音量(并设置用户界面表示法)

controls->setVolume(player->volume());

我们可以让小部件 "音量 "改变音量

connect(controls, SIGNAL(changeVolume(int)), player, SLOT(setVolume(int)));

该示例还允许我们通过QVideoWidget 对象更改视频属性。我们只需点击一下按钮就能进入全屏模式,然后再返回全屏模式。

示例项目 @ code.qt.io

© 2025 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.