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

 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();
posted @ 2017-09-27 19:21  云胡同学  阅读(630)  评论(0编辑  收藏  举报