您的位置:首页 > 编程语言 > Qt开发

qt弹出对话框选择视频并播放

2017-09-27 19:21 405 查看
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();
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: