Qt QMediaPlayer cannot play mp3
I am using Qt 5.3.2 QMediaPlayer to play MP3 file under OSX 10.10, until now I could not play anything.
The code I use is something like this:
player = new QMediaPlayer;
player->setMedia(QUrl(soundName));
qDebug()<<soundName;
player->setVolume(50);
player->play();
When using this function, I get this error in the Application Exit panel:
[19:32:52.144] FigByteFlumeCustomURLOpen signalled err=-12936 (kFigByteFlumeError_BadState) (no provider) at /SourceCache/CoreMedia/CoreMedia-1562.19/Prototypes/FigHTTP/FigByteFlumeCustomURL.c line 1486
The same code works great on Windows 8.
Can anyone please help?
+3
source to share
1 answer
Found the answer a while ago, posting it right here for future reference.
The idea is that Qt cannot play mp3 file from resources. The files must first be copied to a temporary folder. Here's a way to load files from an "mp3" file into a temporary file:
bool Dialog::loadFiles() {
bool success = true;
//through mp3 folder of resources
QDir rsc(":/mp3");
QStringList files = rsc.entryList(QStringList()<<"*.mp3", QDir::Files);
tmpPath = QDir::toNativeSeparators(QDir::tempPath());
foreach(QString s, files) {
QString srcPath = QString(":/mp3/%1").arg(s);
QString destPath = QString("%1\\%2").arg(tmpPath).arg(s);
mp3Files.insert(s, destPath);
if (!QFile::exists(destPath))
success &= QFile::copy(srcPath, destPath);
}
return success;
}
0
source to share