QString path = QFileDialog::getOpenFileName(this, tr("选择视频"), ".", tr("Image Files(*.mp4)"));//得到视频路径
mediaPlayer = new QMediaPlayer();
videoWidget = new QVideoWidget();
this->setCentralWidget(videoWidget);
//给VideoWidget画上黑色的背景,这样会更专业点(默认是灰白色的)
QPalette* palette = new QPalette();
palette->setBrush(QPalette::Background, Qt::black);
videoWidget->setPalette(*palette);
videoWidget->setAutoFillBackground(true);
delete palette;
/*
设置播放视频的比例
enum AspectRatioMode {
IgnoreAspectRatio,
KeepAspectRatio,
KeepAspectRatioByExpanding
};
*/
videoWidget->setAspectRatioMode(Qt::IgnoreAspectRatio);//保持比例
mediaPlayer->setVideoOutput(videoWidget);
//第一种方法:mediaPlayer直接setMedia()
mediaPlayer->setMedia(QUrl::fromLocalFile(path));
mediaPlayer->play();
转载于:https://www.cnblogs.com/pjc20/p/7707638.html