用QMediaPlayer + QVideoWidget + QMediaPlaylist实现播放器

用QMediaPlayer实现播放器

界面截图
用QMediaPlayer + QVideoWidget + QMediaPlaylist实现播放器_第1张图片用QMediaPlayer + QVideoWidget + QMediaPlaylist实现播放器_第2张图片
添加三个关键部件

    QVideoWidget* videoWidget = new QVideoWidget(this);
    ui->playWidgetLayout->addWidget(videoWidget);

    m_mediaPlayer = new QMediaPlayer(this);
    m_mediaPlayer->setVideoOutput(videoWidget);

    m_mediaPlayList = new QMediaPlaylist(this);
    m_mediaPlayer->setPlaylist(m_mediaPlayList);

添加本地文件并播放

   QString path = QFileDialog::getOpenFileName();
   QFileInfo fileInfo(path);
   QString url = QUrl::fromLocalFile(fileInfo.absoluteFilePath()).toString();
   QString fileName = fileInfo.fileName();

   ui->playList->addItem(fileName);
   m_mediaPlayList->addMedia(QMediaContent(url));

   QTimer::singleShot(800, this, [this](){
       m_mediaPlayer->play();
   });

   m_nameList.append(fileName);
   m_ulrMap[fileName] = url;
   savePlayList();

添加远端URL播放列表

    if (getMediaURLUi->exec() != QDialog::Accepted)
    {
        return;
    }
    QString displayName = getMediaURLUi->getDisplay();
    QString url = getMediaURLUi->getURL();

    ui->playList->addItem(displayName);
    m_mediaPlayList->addMedia(QMediaContent(url));

    QTimer::singleShot(800, this, [this](){
        m_mediaPlayer->play();
    });

    m_nameList.append(displayName);
    m_ulrMap[displayName] = url;
    savePlayList();

暂停、播放、停止、拖动位置

m_mediaPlayer->play();
m_mediaPlayer->pause();
m_mediaPlayer->stop();
m_mediaPlayer->setPosition(position);

测试项目git地址:
gitee地址

你可能感兴趣的:(用QMediaPlayer + QVideoWidget + QMediaPlaylist实现播放器)