简体   繁体   English

Qt 6.2: QMediaPlayer & QByteArray

[英]Qt 6.2: QMediaPlayer & QByteArray

Good day.再会。 Has anyone tried QMediaPlayer in Qt 6.2 already?有没有人在 Qt 6.2 中尝试过 QMediaPlayer? I'm trying this code, but Media Status always remains as "NoMedia" and no any sound:).我正在尝试此代码,但媒体状态始终保持为“NoMedia”并且没有任何声音:)。 Full test project: https://github.com/avttrue/MediaPlayerTest完整测试项目: 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
} 

As the docs points out:正如文档指出的那样:

void QMediaPlayer::setSourceDevice(QIODevice *device, const QUrl &sourceUrl = QUrl()) void QMediaPlayer::setSourceDevice(QIODevice *device, const QUrl &sourceUrl = QUrl())

Sets the current source device.设置当前源设备。

The media data will be read from device.媒体数据将从设备中读取。 The sourceUrl can be provided to resolve additional information about the media, mime type etc. The device must be open and readable.可以提供 sourceUrl 来解析有关媒体、mime 类型等的附加信息。设备必须是开放的和可读的。

For macOS the device should also be seek-able.对于 macOS,设备也应该是可搜索的。

Note: This function returns immediately after recording the specified source of the media.注意:这个function录制指定媒体源后立即返回。 It does not wait for the media to finish loading and does not check for errors.它不会等待媒体完成加载,也不会检查错误。 Listen for the mediaStatusChanged() and error() signals to be notified when the media is loaded, and if an error occurs during loading.监听 mediaStatusChanged() 和 error() 信号,以便在加载媒体时以及加载过程中是否发生错误时收到通知。

(emphasis mine) (强调我的)

QMediaPlayer does not know how to deduce the file format so it does not load it. QMediaPlayer 不知道如何推断文件格式,所以它不会加载它。 The solution is to point out that it is an mp3:解决方法是指出是mp3:

player->setSourceDevice(buffer, QUrl("foo.mp3"));

The function setSourceDevice() you are using isn't doing what you think?您正在使用的 function setSourceDevice() 没有按照您的想法进行操作? Maybe you wanted setSource() instead?也许你想要 setSource() 而不是?

Qt has great documentation: https://doc.qt.io/qt-6/qmediaplayer.html#setSourceDevice Qt 有很棒的文档: 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 https://doc.qt.io/qt-6/qmediaplayer.html#details

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM