媒体播放器范例

Playing audio and video using Qt Widgets.

媒体播放器 demonstrates a simple multimedia player that can play audio and video files using various codecs.

运行范例

要运行范例从 Qt Creator ,打开 欢迎 模式,然后选择范例从 范例 。更多信息,见 Qt Creator: Tutorial: Build and run .

范例使用 QMediaPlayer 对象被传入 QVideoWidget to control the video output. To give the application playlist capability we also use a QMediaPlaylist object. QMediaPlaylist was removed from our API in Qt 6, but its implementation is included in this example.

An audio level meter shows peak and RMS levels during playback. This is implemented by setting a QAudioBufferOutput to QMediaPlayer , processing each QAudioBuffer it emits on a separate thread and visualising the values through QWidget::paintEvent .

为激活对话框中 (譬如:播放和停止) 各种功能,按钮点击事件要发射 play() 和 stop() 信号并连接到 play() 和 stop() 槽对于 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());
						

可以让 Widget 更改 volume 以改变音量

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

范例还允许改变视频特性通过 QVideoWidget 对象。可以单击按钮进入全屏模式状态,然后再返回。

范例工程 @ code.qt.io