I am a student writing a project using Qt. I need to play an audio file when the button is pushed, so I write the code:
void DrumsWindow::on_pushButton_dHH_clicked()
{
m_player = new QMediaPlayer(this);
m_playlist = new QMediaPlaylist(m_player);
m_player->setPlaylist(m_playlist);
m_playlist->addMedia(QUrl::fromLocalFile("sound/HH.wav"));
m_player->play();
}
But when I hit the pushbutton, it doesn't work, an error occures:
DirectShowPlayerService::doSetUrlSource: Unresolved error code 0x80040216 ()
Please, help me! What am I doing wrong?
P.S.: I was told to do the task without using resource files. Of course, this code works:
void DrumsWindow::on_pushButton_dHH_clicked()
{
m_player = new QMediaPlayer(this);
m_playlist = new QMediaPlaylist(m_player);
m_player->setPlaylist(m_playlist);
m_playlist->addMedia(QUrl("qrc:/aud/sound/HH.wav"));
m_player->play();
}
But I'm not allowed to use this way.
You should write your complete file directory to solve the problem. Alternatively you can receive the song path from file browser or a QLineEdit
filled by user.
Update You can use QDir::currentPath() to find your current directory (executing directory) and move wherever you want.
void DrumsWindow::on_pushButton_dHH_clicked()
{
QString filePath = QDir::currentPath();
m_player = new QMediaPlayer(this);
m_playlist = new QMediaPlaylist(m_player);
m_player->setPlaylist(m_playlist);
m_playlist->addMedia(QUrl::fromLocalFile(filePath + "/sound/HH.wav"));
m_player->play();
}
Also move your creating code of m_player and m_playlist to the constructor to prevent memory leak in your application.
m_player = new QMediaPlayer(this);
m_playlist = new QMediaPlaylist(m_player);
User contributions licensed under CC BY-SA 3.0