本页内容

媒体播放器示例

使用Qt Widgets 播放音频和视频。

显示视频播放和控制面板的媒体播放器

“媒体播放器”演示了一个简单的多媒体播放器,它能够使用各种编解码器播放音频和视频文件。

运行示例

您可以通过以下方式运行该示例:

该示例使用一个传递给QVideoWidget 的QMediaPlayer 对象来控制视频输出。为了使应用程序具备播放列表功能,我们还使用了一个 QMediaPlaylist 对象。QMediaPlaylist 已在 Qt 6 中从我们的 API 中移除,但其实现仍包含在此示例中。

音频电平表可在播放过程中显示峰值和均方根(RMS)电平。其实现方式是将QAudioBufferOutput 设置为QMediaPlayer ,在单独的线程中处理它发出的每个QAudioBuffer ,并通过QWidget::paintEvent 将数值可视化。

要激活对话框上的播放和停止等各项功能,按钮点击事件会发出 play() 和 stop() 信号,这些信号连接到QMediaPlayer 的 play() 和 stop() 槽。

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

© 2026 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.