0

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?

Community
  • 1
  • 1
sashoalm
  • 75,001
  • 122
  • 434
  • 781

1 Answers1

0

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();
Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313