Qt6 QMediaPlayer播放视频没有声音或者无法播放声音报错handleSourceError: 0x8007007B解决方法(无需下载解码器) 您所在的位置:网站首页 放烟花视频无声 Qt6 QMediaPlayer播放视频没有声音或者无法播放声音报错handleSourceError: 0x8007007B解决方法(无需下载解码器)

Qt6 QMediaPlayer播放视频没有声音或者无法播放声音报错handleSourceError: 0x8007007B解决方法(无需下载解码器)

2024-07-03 09:00| 来源: 网络整理| 查看: 265

Qt6 QMediaPlayer播放视频没有声音

Qt5到Qt6时变动了许多类QMediaPlayer也在其中。曾经只需要这样即可播放视频。

player = new QMediaPlayer(this); videoWidget = new QVideoWidget(this); videoWidget->resize(500, 300); player->setVideoOutput(videoWidget); player->setMedia(QUrl::fromLocalFile("D:/video.mp4")); player->play();

而如今需要这样

player = new QMediaPlayer(this); videoWidget = new QVideoWidget(this); videoWidget->resize(500, 300); player->setVideoOutput(videoWidget); player->setSource(QUrl("D:/video.mp4")); player->play();

这样写完后运行,发现视频可以播放了,但是发现了个问题,播放的视频没有声音。打开浏览器搜索,发现好像没有类似问题的文章。至少笔者没找到太多类似的。有也是大部分都是Qt5的。碰到类似问题的朋友应该也有。所以笔者决定写这篇文章。希望能帮到大家,顺便笔者也是自己记录一下。 Qt6中想要正常播放视频(正常播放音乐)需要另外配置音频输出设备的。如下代码可以正常播放

player = new QMediaPlayer(this); audioOutput = new QAudioOutput(this); videoWidget = new QVideoWidget(this); videoWidget->resize(500, 260); player->setAudioOutput(audioOutput); player->setVideoOutput(videoWidget); player->setSource(QUrl("D:/video.mp4")); player->play();

有些细心的朋友可以已经发现了,我在上面用的全是绝对路径。但是我们在写项目的时候很少用绝对路径。Qt项目中我们经常用qrc文件。如果我们把上面的代码中的绝对路径换成相对路径后发现又报错了。报错代码是handleSourceError: 0x8007007B。

player = new QMediaPlayer(this); audioOutput = new QAudioOutput(this); videoWidget = new QVideoWidget(this); videoWidget->resize(500, 260); player->setAudioOutput(audioOutput); player->setVideoOutput(videoWidget); player->setSource(QUrl(":/video.mp4")); player->play(); // 报错:handleSourceError: 0x8007007B

如果想要在qrc文件下正常使用QMediaPlayer文件路径不仅可以写成绝对路径还可以写成URL。怎么把路径写成URL?很简单,就在你的相对路径最前面加上qrc即可。

例如:":/res/video.mp4"->"qrc:/res/video.mp4"

完整测试代码如下:

// widget.h #ifndef WIDGET_H #define WIDGET_H #include #include #include #include class Widget : public QWidget { Q_OBJECT public: Widget(QWidget *parent = nullptr); ~Widget(); QMediaPlayer *m_player = nullptr; QAudioOutput *m_audioOutput = nullptr; QVideoWidget *m_videoWidget = nullptr; }; #endif // WIDGET_H // widget.cpp #include "widget.h" #include #include #include Widget::Widget(QWidget *parent) : QWidget(parent) { this->setFixedSize(500, 300); this->setWindowFlag(Qt::WindowMaximizeButtonHint, false); m_player = new QMediaPlayer(this); m_audioOutput = new QAudioOutput(this); m_videoWidget = new QVideoWidget(this); m_videoWidget->resize(500, 260); m_player->setAudioOutput(m_audioOutput); m_player->setVideoOutput(m_videoWidget); QPushButton *btn1 = new QPushButton(this); btn1->move(200, 270); btn1->setText("play"); connect(btn1, &QPushButton::clicked, [=]() { QString str = QFileDialog::getOpenFileName(); m_player->setSource(QUrl(str)); // m_player->setSource(QUrl("qrc:/res/what_are_you_doing_one.mp3")); m_player->play(); }); } Widget::~Widget() { } // main.cpp #include "widget.h" #include int main(int argc, char *argv[]) { QApplication a(argc, argv); Widget w; w.show(); return a.exec(); }


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有