QMediaPlayer don't play sound from QByteArray in macOS
This is my code :
QFile file("sound.mp3"); file.open(QIODevice::ReadOnly); QByteArray arr = file.readAll(); QMediaPlayer *player = new QMediaPlayer(this); QBuffer *buffer = new QBuffer(player); buffer->setData(arr); buffer->open(QIODevice::ReadOnly); player->setMedia(QMediaContent(),buffer); player->play();
This code work in windows fine but in macOS sound don't play, i can just play sound from file in macOS
From a quick look at the current macOS implementation, it seems that the device is not used in the backend.
@SGaist I have the same problem. Can you please explain what does that mean? It just does not work on mac?
Yes, that's that. However, you don't need a QBuffer if you already load from a file. Just use it with QMediaPlayer..
@SGaist But that is much slower :/ that is the problem
Maybe as a alternative, you can use the MediaContent instead of a buffer for preloading sound files:
QMediaPlayer* mp=new QMediaPlayer; mp->setMedia(QUrl::fromLocalFile("some.mp3")); QMediaContent mc=mp->media(); // get the MediaContent delete mp; mp=new QMediaPlayer; // create a new MedaiPlayer mp->setMedia(mc); // set the media with the previous content mp->setVolume(50); mp->play();
Works for me with Qt 5.8, OSX 10.9
no need of MediaPlayer for loading !
QMediaContent(const QUrl &url)
will work alone ;)