简体   繁体   English

使用QMediaPlayer在资源中播放mp3文件

[英]Play mp3 file in the resource with QMediaPlayer

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. 根据这个职位它说QMediaPlayer需要被调用play()的回调后mediaStatusChanged()这是我所做过确切。 So what's the problem??? 所以有什么问题???

PS I could play the mp3 file from the QMediaPlayer Example as local file. PS我可以将QMediaPlayer示例中的mp3文件作为本地文件播放。

UPDATE 1: I can play the mp3 file as local file... 更新1:我可以将mp3文件作为本地文件播放...

Your problem is now no longer a problem, you can play a Qt resource in the QMediaPlayer. 您的问题现在不再是问题,您可以在QMediaPlayer中播放Qt资源。 Answer found here , and I'm confirming it here in case people are looking. 这里找到答案,我在这里确认,以防人们在寻找。

This code works for me when testing in my local project. 在我的本地项目中测试时,此代码适用于我。

player->setMedia(QUrl("qrc:/audio/audio/Revival_Song01.mp3"));

You should play a file from disk; 你应该从磁盘播放文件; not a Qt resource. 不是Qt资源。 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 : 您可以随时将文件从资源复制到硬盘驱动器,然后播放:

QFile::copy(":/files/FarAway.mp3" , "/some/path/FarAway.mp3");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM