QMediaPlayer::error() is never emitted even though video fails to play

喜你入骨 提交于 2020-01-06 14:10:49

问题


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:


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


来源:https://stackoverflow.com/questions/29654457/qmediaplayererror-is-never-emitted-even-though-video-fails-to-play

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!