How to play audio and delete a file in QMediaPlayer?

I am using qt5.0. I created a dll and put all my audio files inside the dll. Now I create a file from the resource and the game. It works fine.

But the problem is that after playing the file, I can’t delete this file and recreate the new file.

if I try to delete manually, I also get an error. "some other program using this file". as soon as I stop the program, then I can only delete the file.

How to delete a file after immediate palyback. here is my code

player = new QMediaPlayer; connect(player,SIGNAL(mediaStatusChanged(QMediaPlayer::MediaStatus)),this,SLOT(mediaStatusChanged(QMediaPlayer::MediaStatus))); QFile file2(QDir::tempPath() + "/temp0.mp3"); if (file2.open(QIODevice::ReadWrite)) { QFile workFile(":/AUDIO/" + fn +".mp3"); if(workFile.open(QIODevice::ReadOnly)) { file2.write(workFile.readAll()); workFile.close(); } file2.close(); } player->setMedia(QMediaContent(QUrl::fromLocalFile(QDir::tempPath() + "/temp0.mp3"))); player->setVolume(100); player->play(); 

 void Audio::mediaStatusChanged(QMediaPlayer::MediaStatus state) { if(state==QMediaPlayer::EndOfMedia) { QFile::remove(QDir::tempPath() + "/temp0.mp3"); qDebug()<<"Audio played"; } } 

I get the message "Audio play", but does not delete the file.

please help me solve this problem.

+6
source share
2 answers

I recently met the same problem. I solved this by changing the compiler from msvs to mingw. I think that the guys from Qt did not solve this error, since its status "needs additional information." The code for the release of the media file may be as follows.

 player->setMedia(QMediaContent()); 

However, if I use the mingw compiler, the video does not play in win7 x86 VM and reports DirectShowPlayerService::doRender: Unresolved error code 80040266 . It seems to me that this is a dead end.

+3
source

docs tell us about QMediaPlayer :: setMedia:

Setting this property to null QMediaContent will cause the player to discard all information related to the current source, stop all I / O operations associated with this media.

Unfortunately, this does not work on Windows 7 with Qt 5.1.1. So I filed a bug report

Even the people on the Qt irc channel told me that I do not use QMediaPlayer on Windows. They suggested using SDL. A bit strange for a cross-platform platform with a multimedia API.

+1
source

All Articles