2

I have a QByteArray which created like this:

QByteArray data;
QFile file("/path/to/music.mp3");

if (file.open(QIODevice::ReadOnly))
{
    data = file.readAll();
}

And i get it somewhere else, how could i play it using QMediaPlayer without save it to file ?

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Anthony
  • 98
  • 7

1 Answers1

1

If you have .mp3 file directly, you can call it by directly setting the URL to QMediaPlayer.

You can find below example in documentation.

QMediaPlayer* player = new QMediaPlayer;
connect(player, SIGNAL(positionChanged(qint64)), this, SLOT(positionChanged(qint64)));
player->setMedia(QUrl::fromLocalFile("/path/to/music.mp3"));
player->setVolume(50);
player->play();

https://doc.qt.io/qt-5/qmediaplayer.html#setMedia

If for obvious reasons, you have to go with QByteArray, May be you can try as said below (Not tried and tested):

//BYTE ARRAY
QByteArray data;
if (file.open(QIODevice::ReadOnly))
{
    data = file.readAll();
}

//CREATE A BUFFER OBJECT WITH BYTE ARRAY
QBuffer buffer(&data);
buffer.open(QIODevice::ReadOnly);

//CREATE MEDIA PLAYER OBJECT
QMediaPlayer* player = new QMediaPlayer;

//SET MEDIA CONTENT AND BUFFER.
player->setMedia(QUrl::fromLocalFile("/path/to/music.mp3"),&buffer);
player->play();
Pavan Chandaka
  • 11,671
  • 5
  • 26
  • 34
  • Thank you, but i don't have any file to mentioned in the first argument of `setMedia()` function. – Anthony Aug 15 '20 at 03:13