Qt6 QMediaPlayer plays video without sound or cannot play sound and reports an error handleSourceError: 0x8007007B solution (no need to download the decoder)

Qt6 QMediaPlayer plays video without sound

From Qt5 to Qt6, many classes of QMediaPlayer have been changed. It used to be all that was needed to play a video.

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

And now it's needed

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

After writing this way, run it, and found that the video can be played, but found a problem, the played video has no sound. Open the browser to search and find that there seems to be no articles with similar problems. At least I didn't find many similar ones. There are also most of them are Qt5. Friends who have encountered similar problems should have. So I decided to write this article. I hope it can help everyone, and by the way, I also record it myself.
In Qt6, if you want to play video normally (play music normally), you need to configure an additional audio output device. The following code can play normally

    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();

Some careful friends may have discovered that I used all absolute paths above. But we rarely use absolute paths when writing projects. We often use qrc files in Qt projects. If we replace the absolute path in the above code with a relative path, we will report an error again. The error code is 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

If you want to use the QMediaPlayer file path normally under the qrc file, you can write it not only as an absolute path but also as a URL. How to write the path as URL? It's very simple, just add qrc to the front of your relative path.

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

The complete test code is as follows:

// widget.h
#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QMediaPlayer>
#include <QVideoWidget>
#include <QAudioOutput>

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 <QPushButton>
#include <QFileDialog>
#include <QLabel>

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 <QApplication>


int main(int argc, char *argv[])
{
    
    
    QApplication a(argc, argv);
    Widget w;
    w.show();
    return a.exec();
}

Guess you like

Origin blog.csdn.net/m0_52072919/article/details/129973758