Reputation: 151
I'm writing a music player for my assessment. And currently I need to make it so that the tracks will play in order.
Currently the bit that plays music of it looks like this:
repeat
//Other code that's related to responding to user input
while i < trackCount - 1 do
begin
if not MusicPlaying() then
begin
PlayMusic(trackName, 1);
trackNumber := trackNumber + 1;
i := i + 1;
end;
end;
until WindowCloseRequested();
Basically it's saying "While the list is not finished yet, if no music is playing, then play a track and increase i
so the next track can be played once it's finished". Apparently that made the program crashed, or seemed like it anyway. I couldn't interact with the program once I hit play, but the music was still playing in order, which told me that the logic was working perfectly. The program did return to normal afterwards, but returned to the same state the moment I tried to play the list again. Does this mean it's a bad code and I should be trying to think of a different way around it? Or is there something I didn't know about having a loop inside a loop?
Upvotes: 0
Views: 71
Reputation: 21033
No, your program does not really crash, it is just held up in the while i < trackCount - 1 do
loop.
i
changes (is incremented) only when MusicPlaying()
returns false, which presumably only happens initially and then after a track has finished playing. All this time between, your program is caught in the while
loop just repeatedly waiting for MusicPlaying()
to return false, and therefore appears non-responsive.
I suspect the repeat..until
loop is the main loop of your program. If that is true, then you could simply change the word while
to an if
.
repeat
// Other code that's related to responding to user input
if i < trackCount - 1 do
begin
if not MusicPlaying() then
begin
PlayMusic(trackName, 1);
trackNumber := trackNumber + 1;
i := i + 1;
end;
end;
until WindowCloseRequested();
Upvotes: 1