QMediaPlayer::error() is never emitted even though

2019-09-04 06:45发布

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();

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?

1条回答
太酷不给撩
2楼-- · 2019-09-04 07:31

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

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();
查看更多
登录 后发表回答