Bear35645
Bear35645

Reputation: 213

Trying to play video

I am attempting to play a video file using the QMedia player. Here is my code. It does pop up a new window but the file does not play. The variable video is a QString holding the relative path to the file in this case it would be "videos/wildlife.wmv". What am I doing wrong?

    QMediaPlayer* player = new QMediaPlayer(this);
    QMediaPlaylist* list = new QMediaPlaylist();
    QVideoWidget *vw = new QVideoWidget();
    list->addMedia(QUrl::fromLocalFile(video));
    player->setVolume(100);
    player->setPlaylist(list);
    player->setVideoOutput(vw);
    vw->show();
    list->setCurrentIndex(0);
    player->play();

I noticed in the Application output window I get this following message: setGeometry: Unable to set geometry 0x0+480+190 on QWidgetWindow/'QVideoWidgetClassWindow'. Resulting geometry: 116x0+480+190 (frame: 8, 30, 8, 8, custom margin: 0, 0, 0, 0, minimum size: 0x0, maximum size: 16777215x16777215).

Thanks. Bear

Upvotes: 2

Views: 284

Answers (2)

Jablonski
Jablonski

Reputation: 18504

This error can be caused by wrong path to file(when your file did not load, then player has nothing to show). You will get same error when you for example set unexisting pixmap to label.

Try:

Set full path to your file and check is it normal now.

Use QFile::exists() to be sure that your file(relative path) exists.

When I tried your code on my computer (MOV file format) I got same error and I didn't see video, but I heard voice. How did I fixe it on my computer? Try at least next, maybe it helps:

I resize QVideoWidget programmatically. For example:

//your code
player->setVideoOutput(vw);
vw->show();
vw->resize(1000,600);//Add this line
list->setCurrentIndex(0);
player->play();

Upvotes: 1

Oleg Shparber
Oleg Shparber

Reputation: 2752

You need to assign playlist to player:

player->setPlaylist(list);

Also start from the beginning of the list:

list->setCurrentIndex(0);

Upvotes: 2

Related Questions