Sorry if my qustion is lame, but i am new in Qt(and ofc my english level too). So, I have a console application in QT, and i use c++. I would like to create a text-based mini game and i need some background music. I used QMediaPlayer QT class for this, and it worked... but i realise i can not loop the music. I found another QT Class for this the QMediaPlaylist. But the loop function did not work (it played just once). I tried it in a maindwindow application and if i used in the mainwindow.cpp it worked fine. And i dont know why, and where is my fault? I tried this way:
main.cpp
int main(int argc, char *argv[])
{
Menu();
return a.exec();
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
Menu();
return a.exec();
}
To copy to clipboard, switch view to plain text mode
menu.cpp
{
///...........
this->mp = new MediaPlay();
mp->PlayMusic("backgroundmusic.mp3");
///...........
}
Menu::Menu(QObject *parent) : QObject(parent)
{
///...........
this->mp = new MediaPlay();
mp->PlayMusic("backgroundmusic.mp3");
///...........
}
To copy to clipboard, switch view to plain text mode
Mediaplay.cpp
{
}
void MediaPlay
::PlayMusic(QString name
) {
this->playlist = new QMediaPlaylist(this);
playlist
->addMedia
(QUrl::fromLocalFile(".//res//"+name
));
playlist->setPlaybackMode(QMediaPlaylist::PlaybackMode::Loop);
this->player = new QMediaPlayer(this);
player->setVolume(settings->value("iVolume").toInt());
player->setPlaylist(playlist);
player->play();
}
void MediaPlay::SetVolume(int valume)
{
settings->setValue("iVolume",valume);
player->setVolume(valume);
}
MediaPlay::MediaPlay(QObject *parent) : QObject(parent)
{
}
void MediaPlay::PlayMusic(QString name)
{
this->playlist = new QMediaPlaylist(this);
this->settings = new QSettings(QDir::currentPath() + "/settings.ini", QSettings::IniFormat);
playlist->addMedia(QUrl::fromLocalFile(".//res//"+name));
playlist->setPlaybackMode(QMediaPlaylist::PlaybackMode::Loop);
this->player = new QMediaPlayer(this);
player->setVolume(settings->value("iVolume").toInt());
player->setPlaylist(playlist);
player->play();
}
void MediaPlay::SetVolume(int valume)
{
settings->setValue("iVolume",valume);
player->setVolume(valume);
}
To copy to clipboard, switch view to plain text mode
/////////////////////////
follow-up text: I have an exit function in Menu class. In the exit function i call "qApp->quit();" and then not close the console, it sarts playing the background music in loop... but why?
Bookmarks