Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Play Audio and remove file in QMediaPlayer?

I'm using qt5.0. I created dll and put all my audio files inside dll. now i am creating file from resource and playing. it's working fine.

But the problem is after playing the file I'm not able to delete that file and recreate new file.

if I try to delete manually also I getting error. "some other program using that file". once i stop the program then only able to delete the file.

How to delete the file after immediate palyback. here 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'm getting "Audio played" message but it's not deleteing the file.

please help me to solve this issue.

like image 850
Saravanan Avatar asked Feb 26 '13 20:02

Saravanan


2 Answers

Recently,I met the same problem. I solved it by changing the compiler from msvs to mingw. I think the Qt guys did not solve this bug as its status is 'need more information'. The code to release media file could be as follows.

player->setMedia(QMediaContent());

However, if I use mingw compiler, the video does not play in a VM win7 x86 and reports DirectShowPlayerService::doRender: Unresolved error code 80040266. This seems a deadlock to me.

like image 98
Shuo Li Avatar answered Oct 01 '22 14:10

Shuo Li


The docs tell us for QMediaPlayer::setMedia:

Setting this property to a null QMediaContent will cause the player to discard all information relating to the current media source and to cease all I/O operations related to that media.

Unfortunately this does not work on Windows 7 with Qt 5.1.1 - This why I filed a bug report

Even the folks in the Qt irc channel told me not use QMediaPlayer on Windows. They suggested using SDL. Little bit strange for a cross platform framework with a Multimedia API.

like image 38
Christian Rapp Avatar answered Oct 01 '22 12:10

Christian Rapp