I try to play mp3 files declared in the resource, but it show:
我试着播放资源中声明的mp3文件,但它显示:
Btn clicked
current media: "qrc://sound/sound/FarAway.mp3"
Error : QMediaPlayer::FormatError
Media state : QMediaPlayer::InvalidMedia
Here's how I set media:
我是这样设置媒体的:
player = new QMediaPlayer(this);
player->setMedia(QUrl(mediaFilePath));
qDebug() << "current media: " << player->currentMedia().canonicalUrl().toString();
connect(player, SIGNAL(stateChanged(QMediaPlayer::State)), SLOT(handleStateChanged(QMediaPlayer::State)));
connect(player, SIGNAL(mediaStatusChanged(QMediaPlayer::MediaStatus)), SLOT(handleMediaStateChanged(QMediaPlayer::MediaStatus)));
connect(player, SIGNAL(error(QMediaPlayer::Error)), SLOT(handleError(QMediaPlayer::Error)));
According to this post it said QMediaPlayer
need to be called play()
after the callback of mediaStatusChanged()
, which is what I've done exactly. So what's the problem???
根据这篇文章,它说在mediaStatusChanged()回调后,QMediaPlayer需要被称为play(),这正是我所做的。所以有什么问题? ? ?
P.S. I could play the mp3 file from the QMediaPlayer Example as local file.
我可以从QMediaPlayer示例中播放mp3文件作为本地文件。
UPDATE 1: I can play the mp3 file as local file...
更新1:我可以播放mp3文件作为本地文件…
2 个解决方案
#1
3
Your problem is now no longer a problem, you can play a Qt resource in the QMediaPlayer. Answer found here, and I'm confirming it here in case people are looking.
现在您的问题不再是问题,您可以在QMediaPlayer中播放Qt资源。在这里找到答案,我在这里确认一下,以防有人看。
This code works for me when testing in my local project.
这个代码在我的本地项目中测试时适用。
player->setMedia(QUrl("qrc:/audio/audio/Revival_Song01.mp3"));
#2
2
You should play a file from disk; not a Qt resource. Since the resources are not supported yet. You can copy the file from resource to your hard drive on the fly and then play it :
您应该从磁盘播放文件;不是一个Qt的资源。因为资源还没有被支持。您可以将文件从资源复制到您的硬盘上,然后播放它:
QFile::copy(":/files/FarAway.mp3" , "/some/path/FarAway.mp3");
#1
3
Your problem is now no longer a problem, you can play a Qt resource in the QMediaPlayer. Answer found here, and I'm confirming it here in case people are looking.
现在您的问题不再是问题,您可以在QMediaPlayer中播放Qt资源。在这里找到答案,我在这里确认一下,以防有人看。
This code works for me when testing in my local project.
这个代码在我的本地项目中测试时适用。
player->setMedia(QUrl("qrc:/audio/audio/Revival_Song01.mp3"));
#2
2
You should play a file from disk; not a Qt resource. Since the resources are not supported yet. You can copy the file from resource to your hard drive on the fly and then play it :
您应该从磁盘播放文件;不是一个Qt的资源。因为资源还没有被支持。您可以将文件从资源复制到您的硬盘上,然后播放它:
QFile::copy(":/files/FarAway.mp3" , "/some/path/FarAway.mp3");