Home > Software design >  Qt 6.2: QMediaPlayer & QByteArray
Qt 6.2: QMediaPlayer & QByteArray

Time:10-08

Good day. Has anyone tried QMediaPlayer in Qt 6.2 already? I'm trying this code, but Media Status always remains as "NoMedia" and no any sound :). Full test project: https://github.com/avttrue/MediaPlayerTest

#include "mainwindow.h"

#include <QDebug>
#include <QBuffer>
#include <QFile>
#include <QAudioOutput>
#include <QMediaPlayer>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    QFile file("../test/Bankrobber.mp3");
    if(!file.open(QIODevice::ReadOnly))
        qDebug() << "File not opened";
    qDebug() << "File size:" << file.size(); // File size: 11181085

    QByteArray ba = file.readAll();
    qDebug() << "ByteArray size:" << ba.size(); // ByteArray size: 11181085

    QBuffer* buffer = new QBuffer(this);
    buffer->setData(ba);
    if(!buffer->open(QIODevice::ReadOnly))
        qDebug() << "Buffer not opened";
    qDebug() << "Buffer size:" << buffer->size(); // Buffer size: 11181085

    buffer->seek(qint64(0));

    auto audioOutput = new QAudioOutput(this);
    auto player = new QMediaPlayer(this);
    player->setAudioOutput(audioOutput);
    audioOutput->setVolume(50);
    player->setSourceDevice(buffer);
    qDebug() << "Device:" << player->sourceDevice(); // Device: QBuffer(0x563180493020)

    QObject::connect(player, &QMediaPlayer::mediaStatusChanged,
                     [=](QMediaPlayer::MediaStatus status)
    { qDebug() << "MediaStatus:" << player->mediaStatus() << "|" << status; });

    QObject::connect(player, &QMediaPlayer::errorOccurred,
                     [=](QMediaPlayer::Error error)
    { qDebug() << "Error:" << player->errorString() << "|" << error; });

    QObject::connect(player, &QMediaPlayer::playbackStateChanged,
                     [=](QMediaPlayer::PlaybackState state)
    { qDebug() << "PlaybackState:" << player->playbackState() << "|" << state; });

    player->play();
    qDebug() << "MediaStatus:" << player->mediaStatus(); // MediaStatus: QMediaPlayer::NoMedia
} 

CodePudding user response:

The function setSourceDevice() you are using isn't doing what you think? Maybe you wanted setSource() instead?

Qt has great documentation: https://doc.qt.io/qt-6/qmediaplayer.html#setSourceDevice

Even good examples:

player = new QMediaPlayer;
audioOutput = new QAudioOutput;
player->setAudioOutput(audioOutput);
connect(player, SIGNAL(positionChanged(qint64)), this, SLOT(positionChanged(qint64)));
player->setSource(QUrl::fromLocalFile("/Users/me/Music/coolsong.mp3"));
audioOutput->setVolume(50);
player->play();

Ref. https://doc.qt.io/qt-6/qmediaplayer.html#details

CodePudding user response:

May be this is as variant, but i think it not good:

QTemporaryFile tfile;
if (!tfile.open())
     qDebug() << "TemporaryFile not opened";
 else
 {
     qDebug() << "TemporaryFile writed:" << tfile.write(ba);
     if(tfile.size() != ba.size())
         qDebug() << "TemporaryFile not complited";
     else
         player->setSource(QUrl::fromLocalFile(tfile.fileName()));
 }
  •  Tags:  
  • c qt
  • Related