-1

i'm trying to loop the background music and i always got this SIGSEGV problem that crashed my program. If i remove the QMediaPlaylist playback mode then the program ran just fine.

Here the github link: https://github.com/KhanhSonPham/simplePlaneShooting

QMediaPlaylist * playlist = new QMediaPlaylist();
playlist->setPlaybackMode(QMediaPlaylist::CurrentItemInLoop);
playlist->addMedia(QUrl("qrc:/sounds/engine.mp3"));
playlist->setCurrentIndex(1);


sound = new QMediaPlayer();
sound->setPlaylist(playlist);
sound->play();

error output

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Son Pham
  • 1
  • 1

1 Answers1

0

This line of code should change to

playlist->setCurrentIndex(0);

Actually you have one item in your playlist. So, it should be on index 0. I assume that it's a typo but for some information about why C/C++ or many other programming languages use zero-based indices I refer you here.

Soheil Armin
  • 2,725
  • 1
  • 6
  • 18
  • Hi, it actually happened with or without the set index function. I know this is wrong but it probably not what caused the error cause the program should have crashed the moment i turned it on – Son Pham Nov 03 '19 at 16:09
  • @SonPham, please try to move the call to```playlist->setPlaybackMode(QMediaPlaylist::CurrentItemInLoop)``` after calling ```sound->play();``` – Soheil Armin Nov 03 '19 at 16:34
  • here is what i did, i created a slot, which is play, in there i play the mediaplayer then set the loop and the program crash immediately.. Audio::Audio(QObject *parent):QMediaPlayer(parent) { playlist = new QMediaPlaylist(); playlist->addMedia(QUrl("qrc:/sounds/engine.mp3")); setPlaylist(playlist); } void Audio::play() { play(); playlist->setPlaybackMode(QMediaPlaylist::Loop); } – Son Pham Nov 03 '19 at 17:25
  • I have update the code to git up, every thing is in audio.cpp, can u check it out. I have been finding the solution for entire afternoon now.... – Son Pham Nov 03 '19 at 18:28