这里使用QT自带的QMediaPlayer

QMediaPlayer是对底层播放框架DirectShowPlayerService的封装,具体格式依赖播放框架,Windows上就是DirectShow,安装LAV Filters之类的DirectShow解码框架就可以支持更多的格式,Linux下是GStreamer

附上两个链接

LAVFilters论坛
LAVFilters下载

安装完成后即可支持更多格式的视频播放,不再提示DirectShowPlayerService::doRender: Unresolved error code 80040266这样的错误。

或者下载安装k-lite解码器

http://www.codecguide.com/download_k-lite_codec_pack_standard.htm

头文件

#pragma once
#include <QWidget>
#include <QtMultimedia/QMediaPlayer>
#include <QtMultimedia/QMediaPlaylist>
#include <QtWidgets/QWidget>
#include <QLabel>
#include <QSlider>
#include <QToolButton>
#include <QComboBox>
#include <QPushButton>
#include <QtMultimedia/QVideoProbe>
#include <QtMultimedia/QAudioProbe>
#include <QtMultimediaWidgets/QVideoWidget>
#include "ui_QtMediaplayer.h"class VideoWidget : public QVideoWidget
{Q_OBJECTpublic:VideoWidget(QWidget *parent = 0);protected:void keyPressEvent(QKeyEvent *event) override;void mouseDoubleClickEvent(QMouseEvent *event) override;void mousePressEvent(QMouseEvent *event) override;
};class QtMediaplayer : public QWidget
{Q_OBJECTpublic:QtMediaplayer(QWidget *parent = Q_NULLPTR);~QtMediaplayer();bool isPlayerAvailable() const;signals:void fullScreenChanged(bool fullScreen);private slots:void open();void durationChanged(qint64 duration);void positionChanged(qint64 progress);void metaDataChanged();void playClicked();void updateRate();void setState(QMediaPlayer::State state);void seek(int seconds);void statusChanged(QMediaPlayer::MediaStatus status);void bufferingProgress(int progress);void videoAvailableChanged(bool available);void displayErrorMessage();private:void setTrackInfo(const QString &info);void setStatusInfo(const QString &info);void handleCursor(QMediaPlayer::MediaStatus status);void updateDurationInfo(qint64 currentInfo);QMediaPlayer *player;//QMediaPlaylist *playlist;VideoWidget *videoWidget;QLabel *coverLabel;QToolButton *playButton;QComboBox *rateBox;QSlider *slider;QLabel *labelDuration;QPushButton *fullScreenButton;QVideoProbe *videoProbe;QAudioProbe *audioProbe;QString trackInfo;QString statusInfo;qint64 duration;private:Ui::QtMediaplayerClass ui;
};

cpp

#include <QtMultimedia/QMediaService>
#include <QtMultimedia/QMediaPlaylist>
#include <QtMultimedia/QVideoProbe>
#include <QtMultimedia/QAudioProbe>
#include <QtMultimedia/QMediaMetaData>
#include <QtWidgets>
#include <QKeyEvent>
#include <QMouseEvent>
#include "QtMediaplayer.h"VideoWidget::VideoWidget(QWidget *parent): QVideoWidget(parent)
{setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);QPalette p = palette();p.setColor(QPalette::Window, Qt::black);setPalette(p);setAttribute(Qt::WA_OpaquePaintEvent);
}void VideoWidget::keyPressEvent(QKeyEvent *event)
{if (event->key() == Qt::Key_Escape && isFullScreen()) {setFullScreen(false);event->accept();}else if (event->key() == Qt::Key_Enter && event->modifiers() & Qt::Key_Alt) {setFullScreen(!isFullScreen());event->accept();}else {QVideoWidget::keyPressEvent(event);}
}void VideoWidget::mouseDoubleClickEvent(QMouseEvent *event)
{setFullScreen(!isFullScreen());event->accept();
}void VideoWidget::mousePressEvent(QMouseEvent *event)
{QVideoWidget::mousePressEvent(event);
}QtMediaplayer::QtMediaplayer(QWidget *parent): QWidget(parent), videoWidget(0), coverLabel(0), slider(0)
{ui.setupUi(this);player = new QMediaPlayer(this);connect(player, SIGNAL(durationChanged(qint64)), SLOT(durationChanged(qint64)));connect(player, SIGNAL(positionChanged(qint64)), SLOT(positionChanged(qint64)));connect(player, SIGNAL(metaDataChanged()), SLOT(metaDataChanged()));connect(player, SIGNAL(mediaStatusChanged(QMediaPlayer::MediaStatus)),this, SLOT(statusChanged(QMediaPlayer::MediaStatus)));connect(player, SIGNAL(bufferStatusChanged(int)), this, SLOT(bufferingProgress(int)));connect(player, SIGNAL(videoAvailableChanged(bool)), this, SLOT(videoAvailableChanged(bool)));connect(player, SIGNAL(error(QMediaPlayer::Error)), this, SLOT(displayErrorMessage()));connect(player, SIGNAL(stateChanged(QMediaPlayer::State)), this, SLOT(setState(QMediaPlayer::State)));videoWidget = new VideoWidget(this);player->setVideoOutput(videoWidget);slider = new QSlider(Qt::Horizontal, this);slider->setRange(0, player->duration() / 1000);labelDuration = new QLabel(this);connect(slider, SIGNAL(sliderMoved(int)), this, SLOT(seek(int)));videoProbe = new QVideoProbe(this);videoProbe->setSource(player);audioProbe = new QAudioProbe(this);audioProbe->setSource(player);QPushButton *openButton = new QPushButton(tr("Open"), this);connect(openButton, SIGNAL(clicked()), this, SLOT(open()));fullScreenButton = new QPushButton(tr("FullScreen"), this);fullScreenButton->setCheckable(true);playButton = new QToolButton(this);playButton->setIcon(style()->standardIcon(QStyle::SP_MediaPlay));connect(playButton, SIGNAL(clicked()), this, SLOT(playClicked()));rateBox = new QComboBox(this);rateBox->addItem("0.5x", QVariant(0.5));rateBox->addItem("1.0x", QVariant(1.0));rateBox->addItem("2.0x", QVariant(2.0));rateBox->setCurrentIndex(1);connect(rateBox, SIGNAL(activated(int)), this, SLOT(updateRate()));QBoxLayout *displayLayout = new QHBoxLayout;displayLayout->addWidget(videoWidget);QBoxLayout *controlLayout = new QHBoxLayout;controlLayout->setMargin(0);controlLayout->addWidget(openButton);controlLayout->addStretch(1);controlLayout->addWidget(playButton);controlLayout->addStretch(1);controlLayout->addWidget(rateBox);controlLayout->addStretch(1);controlLayout->addWidget(fullScreenButton);QBoxLayout *layout = new QVBoxLayout;layout->addLayout(displayLayout, 10);QHBoxLayout *hLayout = new QHBoxLayout;hLayout->addWidget(slider);hLayout->addWidget(labelDuration);layout->addLayout(hLayout);layout->addLayout(controlLayout);setLayout(layout);if (!isPlayerAvailable()) {QMessageBox::warning(this, tr("Service not available"),tr("The QMediaPlayer object does not have a valid service.\n"\"Please check the media service plugins are installed."));openButton->setEnabled(false);fullScreenButton->setEnabled(false);}metaDataChanged();
}QtMediaplayer::~QtMediaplayer()
{
}bool QtMediaplayer::isPlayerAvailable() const
{return player->isAvailable();
}void QtMediaplayer::open()
{QFileDialog fileDialog(this);fileDialog.setAcceptMode(QFileDialog::AcceptOpen);fileDialog.setWindowTitle(tr("Open Files"));QStringList supportedMimeTypes = player->supportedMimeTypes();if (!supportedMimeTypes.isEmpty()) {supportedMimeTypes.append("audio/x-m3u"); // MP3 playlistsfileDialog.setMimeTypeFilters(supportedMimeTypes);}fileDialog.setDirectory(QStandardPaths::standardLocations(QStandardPaths::MoviesLocation).value(0, QDir::homePath()));if (fileDialog.exec() == QDialog::Accepted){player->setMedia(fileDialog.selectedUrls().at(0));player->play();}
}void QtMediaplayer::durationChanged(qint64 duration)
{this->duration = duration / 1000;slider->setMaximum(duration / 1000);
}void QtMediaplayer::positionChanged(qint64 progress)
{if (!slider->isSliderDown()) {slider->setValue(progress / 1000);}updateDurationInfo(progress / 1000);
}void QtMediaplayer::metaDataChanged()
{if (player->isMetaDataAvailable()) {setTrackInfo(QString("%1 - %2").arg(player->metaData(QMediaMetaData::AlbumArtist).toString()).arg(player->metaData(QMediaMetaData::Title).toString()));if (coverLabel) {QUrl url = player->metaData(QMediaMetaData::CoverArtUrlLarge).value<QUrl>();coverLabel->setPixmap(!url.isEmpty()? QPixmap(url.toString()): QPixmap());}}
}void QtMediaplayer::playClicked()
{switch (player->state()) {case QMediaPlayer::StoppedState:case QMediaPlayer::PausedState:player->play();break;case QMediaPlayer::PlayingState:player->pause();break;}
}void QtMediaplayer::updateRate()
{player->setPlaybackRate(rateBox->itemData(rateBox->currentIndex()).toDouble());
}void QtMediaplayer::setState(QMediaPlayer::State state)
{switch (state) {case QMediaPlayer::PlayingState:playButton->setIcon(style()->standardIcon(QStyle::SP_MediaPause));break;case QMediaPlayer::PausedState:playButton->setIcon(style()->standardIcon(QStyle::SP_MediaPlay));break;}
}void QtMediaplayer::seek(int seconds)
{player->setPosition(seconds * 1000);
}void QtMediaplayer::statusChanged(QMediaPlayer::MediaStatus status)
{handleCursor(status);// handle status messageswitch (status) {case QMediaPlayer::UnknownMediaStatus:case QMediaPlayer::NoMedia:case QMediaPlayer::LoadedMedia:case QMediaPlayer::BufferingMedia:case QMediaPlayer::BufferedMedia:setStatusInfo(QString());break;case QMediaPlayer::LoadingMedia:setStatusInfo(tr("Loading..."));break;case QMediaPlayer::StalledMedia:setStatusInfo(tr("Media Stalled"));break;case QMediaPlayer::EndOfMedia:QApplication::alert(this);break;case QMediaPlayer::InvalidMedia:displayErrorMessage();break;}
}void QtMediaplayer::handleCursor(QMediaPlayer::MediaStatus status)
{
#ifndef QT_NO_CURSORif (status == QMediaPlayer::LoadingMedia ||status == QMediaPlayer::BufferingMedia ||status == QMediaPlayer::StalledMedia)setCursor(QCursor(Qt::BusyCursor));elseunsetCursor();
#endif
}void QtMediaplayer::bufferingProgress(int progress)
{setStatusInfo(tr("Buffering %4%").arg(progress));
}void QtMediaplayer::videoAvailableChanged(bool available)
{if (!available) {disconnect(fullScreenButton, SIGNAL(clicked(bool)),videoWidget, SLOT(setFullScreen(bool)));disconnect(videoWidget, SIGNAL(fullScreenChanged(bool)),fullScreenButton, SLOT(setChecked(bool)));videoWidget->setFullScreen(false);}else {connect(fullScreenButton, SIGNAL(clicked(bool)),videoWidget, SLOT(setFullScreen(bool)));connect(videoWidget, SIGNAL(fullScreenChanged(bool)),fullScreenButton, SLOT(setChecked(bool)));if (fullScreenButton->isChecked())videoWidget->setFullScreen(true);}
}void QtMediaplayer::setTrackInfo(const QString &info)
{trackInfo = info;if (!statusInfo.isEmpty())setWindowTitle(QString("%1 | %2").arg(trackInfo).arg(statusInfo));elsesetWindowTitle(trackInfo);
}void QtMediaplayer::setStatusInfo(const QString &info)
{statusInfo = info;if (!statusInfo.isEmpty())setWindowTitle(QString("%1 | %2").arg(trackInfo).arg(statusInfo));elsesetWindowTitle(trackInfo);
}void QtMediaplayer::displayErrorMessage()
{setStatusInfo(player->errorString());
}void QtMediaplayer::updateDurationInfo(qint64 currentInfo)
{QString tStr;if (currentInfo || duration) {QTime currentTime((currentInfo / 3600) % 60, (currentInfo / 60) % 60, currentInfo % 60, (currentInfo * 1000) % 1000);QTime totalTime((duration / 3600) % 60, (duration / 60) % 60, duration % 60, (duration * 1000) % 1000);QString format = "mm:ss";if (duration > 3600)format = "hh:mm:ss";tStr = currentTime.toString(format) + " / " + totalTime.toString(format);}labelDuration->setText(tStr);
}

main

#include "QtMediaplayer.h"
#include <QtWidgets/QApplication>
#include <QCommandLineParser>
#include <QCommandLineOption>
#include <QDir>int main(int argc, char *argv[])
{QApplication app(argc, argv);QCoreApplication::setApplicationName("Player Example");QCoreApplication::setOrganizationName("QtProject");QCoreApplication::setApplicationVersion(QT_VERSION_STR);QCommandLineParser parser;parser.setApplicationDescription("Qt MultiMedia Player Example");parser.addHelpOption();parser.addVersionOption();parser.addPositionalArgument("url", "The URL to open.");parser.process(app);QtMediaplayer player;#if defined(Q_WS_SIMULATOR)player.setAttribute(Qt::WA_LockLandscapeOrientation);player.showMaximized();
#elseplayer.show();
#endifreturn app.exec();
}

如果发布后,发现报错:Error : The QMediaPlayer object does not have a valid service ;

则需要找到 plugins / mediaservice,将整个mediaservice文件夹复制到与exe同一目录下即可。

QT5视频播放器制作相关推荐

  1. java视频播放器制作_java创建简易视频播放器

    java创建简易视频播放器 发布时间:2020-09-23 04:28:09 来源:脚本之家 阅读:98 作者:南柯一梦xihe 最近有个多媒体的作业,要求使用visualC++和OpenCV编写一个 ...

  2. Java 视频播放器制作,包含代码流程和资源需求。

    简单说明 技术方面还请各位海涵,代码和资源引用还有下载方式全部在这里了,这是一个利用Java Swing实现一个简单的视频播放器.由于视频播放器需要解码,这里引用的Java媒体框架(JMF)完成视频解 ...

  3. MFC视频播放器制作(OpenCV)

    界面效果: ---------------------------------------------------------------------------------------------- ...

  4. 学雷前辈暑期小学期课简单视频播放器制作笔记(二)

    因为论文的方向分了(是的,是分的,不是自己选的,个中原因一言难尽.研究生毕设,刚开始,12月份答辩,之前做的全是内容开发,虽然很绝望但是还是要一点点做,论文方向是HEVC的编码优化,目前没啥思路,看了 ...

  5. Python+QT+Selenium制作在线视频播放器

    最近突然想做一个视频播放器,可以在线看视频,关键还没用广告,不用会员,下面给大家介绍一下怎么制作 工具: Python Qt phantomjs 先给大家展示一下效果 下面上代码 导入库: from ...

  6. Linux系统的madplay、mplayer音视频播放器的制作

    Linux系统音视频播放器的制作 madplay和mplayer的安装环境 一.Linux系统录音播放源码的下载和移植 1.需要下载alsa-lib-1.2.6.tar.bz2(声音驱动的内核组件库) ...

  7. 制作统一样式的H5视频播放器

    前言 之前,忙于考试未来及更新文章.本文适用于掌握JavaScript html css jQuery的基本内容的读者.不知大家有没注意原生的H5视频播放器用不同的浏览器打开所呈现的效果并不一样.而如 ...

  8. 【Unity3D自学记录】制作VR视频播放器

    最近VR火的不要不要的,但是综合起来,VR资源最多的还是全景图片和全景视频,今天在这里给大家简单介绍一下如何用Unity制作简单的VR视频播放器. 首先找到EasyMovieTexture这个插件,A ...

  9. 【简便的PyQt5】制作一个极具特色的视频播放器

    [简便的PyQt5]制作一个极具特色的视频播放器 写在前面 效果展示 开源代码 loadInputVideo.py demo.py 写在前面   实现了以下功能 : ❤  PyQt5本地上传视频 ❤  ...

最新文章

  1. Wordpress安装简要说明
  2. Linux出现NOKEY
  3. c语言 有趣的代码,分享一段有趣的小代码
  4. shell -eom_EOM的完整形式是什么?
  5. python控制结构实验结果分析_实验1_Python语法及控制结构
  6. Netty 5 io.netty.util.IllegalReferenceCountException 异常
  7. 内存条引发的各类故障解析
  8. HDU 3577 Fast Arrangement ( 线段树 成段更新 区间最值 区间最大覆盖次数 )
  9. 数学建模比赛论文模板格式
  10. TRNSYS模块中英文对照
  11. 第二次作业,问卷星的使用
  12. php人民币转换,PHP转换,如何实现人民币中文大写与数字相互转换?
  13. 核磁共振基本原理——核磁共振现象
  14. linux下企业邮件服务器的搭建
  15. android标题栏 状态栏,android设置无标题栏 、 状态栏
  16. 编译 ORB-SLAM2/3的ROS工程造成(You should double-check your ROS_PACKAGE_PATH...)
  17. 智能教育硬件的大竞争时代
  18. 操作系统——如何求磁盘的物理地址
  19. 【转】深度整理 | 欧盟《一般数据保护法案》(GDPR)核心要点
  20. FMS与Vcam实现flv网络电视直播 FMS直播

热门文章

  1. 无影无踪的增量(又理解下java的的引用....)
  2. KNN实现海伦约会预测
  3. 淘宝直播回放如何下载
  4. freemarker简单使用
  5. 外购入库单,金蝶KIS旗舰版盘点机PDA,生产企业仓库条码管理进销存
  6. scrum看板协作工具协作式脑图软件
  7. matlab肤色计算心率
  8. 计算机专业考研分数最低的北京学校,计算机考研难度排行榜前十名的院校
  9. 浅谈模块化UPS对提高数据中心适应性的作用
  10. vb.net 教程 3-7 窗体编程 菜单和工具栏 3 StatusStrip 1