问题
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