How to make a loop

Find quick help here to get you started with Hollywood
Post Reply
MH2
Posts: 10
Joined: Wed Mar 22, 2023 3:45 pm

How to make a loop

Post by MH2 »

Here are my code for mp3 player. I have figure out how too start, pause, resume and stop the mp3 file. But how do make a loop. After file stop, cant make it to play again, without start the program again.
@VERSION 10,0
@APPTITLE "ROD-DEMO-PLAYER"
@APPVERSION "$VER: ROD-DEMO V0.0.1 (2023.12.27)"
@APPCOPYRIGHT "(C) Copyright 1971-2071 by Magnus Hammarström"
@APPAUTHOR "Magnus Hammarström"
@REQUIRE "avcodec", {Link = True}
@DISPLAY {X=1645, Y=5, Borderless = True, DragRegion = {{X=0, Y=0, Width=350, Height=610}}}
@BGPIC 1, "BG.png", {LoadAlpha = True}

@MUSIC 1, "Media/retro.mp3"

WaitKeyDown("Return")
PlayMusic(1)

WaitKeyDown("p")
PauseMusic(1)

WaitKeyDown("ENTER")
ResumeMusic(1)

WaitKeyDown("END")
StopMusic(1)

/**************************/
/*** Keyboard Function ***/
/**************************/
Function
p_EventFunc(msg)
Switch(msg.action)
Case "OnKeyDown":

/*****************/
/* Enter key ESC */
/* Exit program */
/*****************/
If msg.key="ESC"
End()
EndIf
EndSwitch
EndFunction

InstallEventHandler({OnKeyDown = p_EventFunc})

EscapeQuit(True)

Repeat
WaitEvent
Forever
User avatar
jPV
Posts: 604
Joined: Sat Mar 26, 2016 10:44 am
Location: RNO
Contact:

Re: How to make a loop

Post by jPV »

WaitKeyDown() halts the script execution and waits until you press the correct key. So in this case you have to press the keys in exact order to get forward, and you can't start and stop music without pausing it in between.

What you need is to listen key presses in the event handling function, just as you are listening for the ESC key. The only loop you need is the WaitEvent loop at the end, as you already have it.

BTW. I'd also change OnKeyDown events to OnKeyUp events in this case, because OnKeyUp event gives just one event when you press a key... OnKeyDown may give you many unwanted events if you press a key a bit too long.

EscapeQuit(True) is also useless when you handle exiting with the ESC key.

Here's an example how I'd do it:

Code: Select all

@VERSION 10,0
@APPTITLE "ROD-DEMO-PLAYER"
@APPVERSION "$VER: ROD-DEMO V0.0.1 (2023.12.27)"
@APPCOPYRIGHT "(C) Copyright 1971-2071 by Magnus Hammarström"
@APPAUTHOR "Magnus Hammarström"
@REQUIRE "avcodec", {Link = True}
@DISPLAY {X=1645, Y=5, Borderless = True, DragRegion = {{X=0, Y=0, Width=350, Height=610}}}
@BGPIC 1, "BG.png", {LoadAlpha = True}

@MUSIC 1, "Media/retro.mp3"

/**************************/
/*** Keyboard Function ***/
/**************************/
Function p_EventFunc(msg)
    Switch(msg.action)
    Case "OnKeyUp":
        Switch msg.key
        Case "ESC":
            End()
        Case "\n": ; Enter
            PlayMusic(1, {Times=0}) ; Loop infinite times
        Case "p":
            If IsMusicPlaying(1)
                PauseMusic(1)
            Else
                ?ResumeMusic(1) ; Don't fail if there isn't anything to resume
            EndIf
        Case "END":
            StopMusic(1)
        EndSwitch
    EndSwitch
EndFunction

InstallEventHandler({OnKeyUp = p_EventFunc})

Repeat
    WaitEvent
Forever
PS. Don't "quote" source codes, but use the "code" tag when posting to the forum. In this way you don't lose formatting like tabs.
Post Reply