I develop an application which the user can play multiple sounds at the same time. For this I use QMediaPlayer in conjunction with a matrix to hold each sound separately. The code is simple, for each sound I use a code like this:

Qt Code:
  1. media = new QMediaPlayer();
  2. media->setMedia(QUrl::fromLocalFile(QFileInfo("sound.mp3").absoluteFilePath()));
  3. media->play();
To copy to clipboard, switch view to plain text mode 
Everything is ok. But around playing the sound 115-125 (simultaneous sounds), the new sounds emit signal error(QMediaPlayer::ResourceError) with error code: QMediaPlayer::ResourceError - A media resource couldn't be resolved. Before emit error signal, it print on output this message: DirectShowPlayerService::doPlay: Unresolved error code 0x80004005 ()

This is not a problem, because I delete those sounds as soon as I detect this error. The problem appears right after a couple of QMediaPlayer::ResourceError. If I create a new sound, an error message appear on output window: QThread::start: Failed to create thread. This error appear just after creating a new QMediaPlayer instance:

Qt Code:
  1. qDebug() << "before media = new QMediaPlayer(); (FILE)";
  2. media = new QMediaPlayer();
  3. qDebug() << "after media = new QMediaPlayer(); (FILE)" << media->error
To copy to clipboard, switch view to plain text mode 
The output:

before media = new QMediaPlayer(); (FILE)
QThread::start: Failed to create thread
after media = new QMediaPlayer(); (FILE) QMediaPlayer::NoError
This last kind of QMediaPlayer instance emit no error. More than that, the state is QMediaPlayer::PlayingState. But if I try to delete it - delete(media) - or to set the media to a null QMediaContent - media->setMedia(QMediaContent()) - to make the player to discard all information relating to the current media source, the application freezes. Those two opperations works fine on he others sounds. If I don't delete this kind of QMediaPlayer, I end to have no room for sounds. Somehow QMediaPlayer has a limited number of instances and every QMediaPlayer that I cannot delete anymore fill this number and let no room for new instances of QMediaPlayer.

The question is: how I can avoid this issue? If I limit the number of simultaneous sounds, what is the correct number of QMediaPlayer instances? If I set a limit, this limit can be too high for other machines. Or how to properly detect this malformed QMediaPlayer and properly delete it?

Also, if I wait for the first QMediaPlayer::ResourceError signal to limit the number of sounds, this is sometimes too late, because the error signal is emitted a bit later and sometimes the application already create a malformed QMediaPlayer that I cannot delete anymore and this prevent the process to close.