【Qt】使用QMediaPlayer播放视频

一、工具

        QMedaPlayer播放视频,底层依赖于第三方解码器。windows平台为directShow,linux平台为GStream。window平台可以安装K-Lite,linux平台一般各发行版都会默认安装GSteam。播放RTSP视频时,可以使用VLC播放器搭建一个视频推流服务器,用于拉流的客户端的调试。

1、解码器

K-Lite(window);GStream(Linux)

一般的linux发行版会内置GStream,可以通过如下命令确认是否安装。

gst-inspect-1.0 --version

如果未安装可以使用如下命令,在线安装

sudo apt-get update
sudo apt-get install  gstreamer1.0-tools  gstreamer1.0-alsa  gstreamer1.0-plugins-base  gstreamer1.0-plugins-good  gstreamer1.0-plugins-bad  gstreamer1.0-plugins-ugly  gstreamer1.0-libav  gtk-doc-tools -y

2、VLC播放器

vlc-3.0.21-win64

二、代码demo

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMediaPlayer>
#include <QVideoWidget>
#include <QUrl>
#include <QMessageBox>
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    //  创建 QMediaPlayer 对象
    player = new QMediaPlayer(this);


    connect(player,static_cast<void(QMediaPlayer::*)(QMediaPlayer::Error)>(&QMediaPlayer::error),this,&MainWindow::ShowError);//播放错误
    connect(player,&QMediaPlayer::mediaStatusChanged,this,&MainWindow::MediaStatusChanged);                                    //媒体状态改变
    connect(player,&QMediaPlayer::stateChanged,this,&MainWindow::StatusChaned);                                               //媒体播放状态改变


    // 创建 QVideoWidget 对象
    videoWidget = new QVideoWidget(this );

    videoWidget->setGeometry(ui->pushButton->geometry()); // 设置位置和大小
    //setCentralWidget(videoWidget); //设置 QVideoWidget 填充主窗口的中心区域
    //videoWidget->setFullScreen(true);//全屏
    videoWidget->setAspectRatioMode(Qt::IgnoreAspectRatio);//缩放自适应大小

    // 设置视频输出到 QVideoWidget
    player->setVideoOutput(videoWidget);


    //播放RTSP视频
   QString strRtsp = "rtsp://stream.strba.sk:1935/strba/VYHLAD_JAZERO.stream";
   QUrl videoUrl(strRtsp); // 请替换为你的 RTSP 流 URL

   //播放本地视频文件
   //QString videoFilePath = "F:/test.mp4"; // 请替换为你的视频文件路径
   //QUrl videoUrl = QUrl::fromLocalFile(videoFilePath);
    player->setMedia(videoUrl);


    player->play();         //播放
   // player->stop();       //停止
    //player->pause();      //暂停

}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::ShowError(QMediaPlayer::Error errorInfo)
{
    QString OutInfo;
    int nCode = 0;
    switch (errorInfo)
    {

    case QMediaPlayer::NoError:
        nCode = QMediaPlayer::NoError;
        break;
    case QMediaPlayer::ResourceError:
         nCode =QMediaPlayer::ResourceError;
        break;
    case QMediaPlayer::FormatError:
         nCode = QMediaPlayer::FormatError;
        break;
    case QMediaPlayer::NetworkError:
         nCode = QMediaPlayer::NetworkError;
        break;
    case QMediaPlayer::AccessDeniedError:
         nCode = QMediaPlayer::AccessDeniedError;
        break;
    case QMediaPlayer::ServiceMissingError:
         nCode = QMediaPlayer::ServiceMissingError;
        break;
    case QMediaPlayer::MediaIsPlaylist:
         nCode = QMediaPlayer::MediaIsPlaylist;
        break;
    default:
        break;
    }
    OutInfo = QString("ErrorCode:%1").arg(nCode);

    QMessageBox::critical(NULL,"错误",OutInfo);

}
//媒体状态改变
void MainWindow::MediaStatusChanged(QMediaPlayer::MediaStatus status)
{
    QString OutInfo;
    int nCode = 0;
    switch (status)
    {
    case QMediaPlayer::UnknownMediaStatus://未知情况
        nCode = QMediaPlayer::UnknownMediaStatus;
        break;
    case QMediaPlayer::NoMedia:         //没有媒体文件
        break;
    case QMediaPlayer::LoadingMedia:    //正在加载媒体文件
        break;
    case QMediaPlayer::LoadedMedia:     //媒体加载完成
        break;
    case QMediaPlayer::StalledMedia:    //播放停滞
        break;
    case QMediaPlayer::BufferingMedia:  //正在缓冲媒体文件
        break;
    case QMediaPlayer::BufferedMedia:   //媒体缓冲完成
        break;
    case QMediaPlayer::EndOfMedia:      //播放结束
        break;
    case QMediaPlayer::InvalidMedia:    //无效媒体文件
        break;
    default:
        break;
    }

    OutInfo = QString("ErrorCode:%1").arg(nCode);
    qDebug() << OutInfo;
}
//媒体播放状态改变
void MainWindow::StatusChaned(QMediaPlayer::State state)
{

    QString OutInfo;
    int nCode = 0;
    switch (state)
    {
    case QMediaPlayer::StoppedState://停止
        break;
    case QMediaPlayer::PlayingState://播放
        break;
    case QMediaPlayer::PausedState://暂停
        break;

    }

    OutInfo = QString("ErrorCode:%1").arg(nCode);
    qDebug() << OutInfo;
}


三、注意事项

1、需要在.pro工程文件中引用对应模块

QT +=   multimedia multimediawidgets

2、需要安装解码器,否则报 ServiceMissingError 无法找到服务的错误

3、如果没有添加Qt环境变量,请把关联的Qt5MultimediaWidgets.dll和Qt5Multimedia.dll放到进程路径下否则也会报ServiceMissingError错误

四、下载路径

1、解码器

Releases · Nevcairiel/LAVFilters · GitHub

2、代码demo

https://download.youkuaiyun.com/download/vfw2014/89928825

3、VLC播放器

VLC media player - Download

在网上找了一个别人的旧版本的播放器~自己更改了一天,终于可运行了,希望分享下同共学习; 不过出了少少问题:希望有经验的一起指导下: 运行环境:qt creator +windows 1、原计划是可支持视频播放的,现在只能加载间频(mp3),一加载视频就卡死 2.、自定义的进度条不起作用,不知为什么百分比槽连接不成功 connect(wmp, SIGNAL(PositionChange(double, double)),this, SLOT(Slot_onPositionChange(double, double))); 3、我用的是QAxWidget控件,这个是不是只支持windows的呢?或者还有没其实更好的? /******************************************************************************************* 项目名:QT播放Qt Mediaplayer 工程师:枫儿 完成时间:2009年12月28日 技术支持:嵌入式家园 www.studyarm.cn www.mcupark.com *******************************************************************************************/ #include "playerwindow.h" #include #include #include #include #include #include #include #include PlayerWindow::PlayerWindow() { //setCaption(tr("Media Player")); fileFilters = tr("Video files (*.mpg *.mpeg *.avi *.wmv)\n" //原来字符串换行也可这样用 "Audio files (*.mp3 *.wav)"); updateTimer = 0; setMouseTracking(true); this->wmp = new QAxWidget(this); wmp->setControl("{22D6F312-B0F6-11D0-94AB-0080C74C7E95}"); // wmp->setProperty("ShowControls", QVariant(false, 0)); wmp->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding); connect(wmp, SIGNAL(PlayStateChange(int, int)),this, SLOT(Slot_onPlayStateChange(int, int))); connect(wmp, SIGNAL(ReadyStateChange(ReadyStateConstants)),this, SLOT(Slot_onReadyStateChange(ReadyStateConstants))); connect(wmp, SIGNAL(PositionChange(double, double)),this, SLOT(Slot_onPositionChange(double, double))); this->openButton = new QPushButton(tr("&Open")); connect(openButton, SIGNAL(clicked()), this, SLOT(Slot_openFile())); this->playPauseButton = new QPushButton(tr("&Play")); connect(playPauseButton, SIGNAL(clicked()), wmp, SLOT(Play())); this->stopButton = new QPushButton(tr("&Stop")); connect(stopButton, SIGNAL(clicked()), wmp, SLOT(Stop())); this->seekSlider = new QSlider(Qt::Horizontal, this); seekSlider->setEnabled(false); connect(seekSlider, SIGNAL(valueChanged(int)),this, SLOT(Slot_sliderValueChanged(int))); connect(seekSlider, SIGNAL(sliderPressed()),wmp, SLOT(Pause())); QHBoxLayout *buttonLayout = new QHBoxLayout; buttonLayout->addWidget(openButton); buttonLayout->addWidget(playPauseButton); buttonLayout->addWidget(stopButton); QVBoxLayout *mainLayout = new QVBoxLayout(this); mainLayout->addWidget(wmp); mainLayout->addLayout(buttonLayout); mainLayout->addWidget(seekSlider); this->setLayout(mainLayout); }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值