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