本文介绍了QMediaPlayer :: error()从不发射,即使视频无法播放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在连接 QMediaPlayer :: error()信号并尝试播放视频文件:

I'm connecting the QMediaPlayer::error() signal and trying to play a video file:

QMediaPlayer *player = new QMediaPlayer;
QMediaPlaylist *playlist = new QMediaPlaylist(player);
playlist->addMedia(QUrl::fromLocalFile("/path/to/file.mp4"));

QVideoWidget *videoWidget = new QVideoWidget;
player->setVideoOutput(videoWidget);

videoWidget->resize(640, 340);
videoWidget->show();
ErrorPrinter *errorPrinter = new ErrorPrinter(player);
QObject::connect(player, SIGNAL(error(QMediaPlayer::Error)), errorPrinter, SLOT(printError(QMediaPlayer::Error)));
player->play();

视频小部件显示,但没有任何播放,所以它必须在某处失败。但是, QMediaPlayer :: error()信号从不发射!应用程序输出为空,没有声明, play()函数为 void 成功或失败),并。

The video widget shows, but nothing is playing, so it must have failed somewhere. However, the QMediaPlayer::error() signal is never emitted! The Application Output is empty, there are no asserts, the play() function is void (no return value to indicate success or failure), and playlist->addMedia always returns true.

我应该怎么知道出了什么问题?

How am I supposed to find out what went wrong?

推荐答案

QMediaPlaylist(player) $ c> QObject parent。它不会将播放列表链接到播放器 - 播放器不知道播放列表。

The QMediaPlaylist(player) construction only sets a QObject parent. It doesn't link the playlist to player - the player is unaware of the playlist.

因此,您从未在播放器上设置播放列表。您可能还需要将播放列表索引设置为1或可能为零(? - 文档不清楚)。

So, you've never set the playlist on the player. You may also need to set the playlist index - to 1 or perhaps zero (? - the docs are not clear on that).

playlist->setCurrentIndex(1);
player->setPlayList(playlist);
player->play();

这篇关于QMediaPlayer :: error()从不发射,即使视频无法播放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 23:56
查看更多