Mapped Keys Only Work Once Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
WeedTrek
Posts: 75
Joined: 22 Mar 2019, 14:29
Location: Cache Creek BC Canada
Contact:

Mapped Keys Only Work Once

15 Jun 2021, 13:54

I'm making a playlist thingie which plays randomly ordered song files out of different folders, using mapped keys:
1:: play contents of folder 1
2:: play contents of folder 2
3:: play contents of folder 3
and they work great, until I try to press 1, 2 or 3 for a second time ... which does not work. Is it because of the SoundPlay loop? How do I correct this?
I can press "1" and folder 1 starts playing, can be interrupted by pressing "2" then "3" but then none of the keys work again.

Code: Select all

#SingleInstance, Force

1::
FileList := ""							; clears file list
Loop, %A_ScriptDir%\playlist01\*.*, 1	; loops through "playlist01" folder for (presumably) audio files
	FileList .= A_LoopFileName "`n"		; puts audio files on a list
Sort, FileList, Random U				; randomizes list and removes duplicates
MsgBox, %FileList%						; (temp) shows the randomized list of files
Loop, parse, FileList, `n				; reads file list
	Soundplay, %A_ScriptDir%\playlist01\%A_LoopField%, wait	; plays each entry on list
return

2::
FileList := ""
Loop, %A_ScriptDir%\playlist02\*.*, 1
	FileList .= A_LoopFileName "`n"
Sort, FileList, Random U
MsgBox, %FileList%
Loop, parse, FileList, `n
	Soundplay, %A_ScriptDir%\playlist02\%A_LoopField%, wait
return

3::
FileList := ""
Loop, %A_ScriptDir%\playlist03\*.*, 1
	FileList .= A_LoopFileName "`n"
Sort, FileList, Random U
MsgBox, %FileList%
Loop, parse, FileList, `n
	Soundplay, %A_ScriptDir%\playlist03\%A_LoopField%, wait
return

ESC::
ExitApp
My Weed Trek video archive: http://weedtrek.ca
User avatar
mikeyww
Posts: 26599
Joined: 09 Sep 2014, 18:38

Re: Mapped Keys Only Work Once

15 Jun 2021, 14:15

You can debug this by displaying the file path just before the SoundPlay. What do you find in those cases where the file is not played?
User avatar
WeedTrek
Posts: 75
Joined: 22 Mar 2019, 14:29
Location: Cache Creek BC Canada
Contact:

Re: Mapped Keys Only Work Once

15 Jun 2021, 14:22

mikeyww wrote:
15 Jun 2021, 14:15
You can debug this by displaying the file path just before the SoundPlay. What do you find in those cases where the file is not played?
if I insert "msgbox, %A_LoopFileFullPath%" (or A_LoopField) right before soundplay, it shows nothing. The files get played no problem, but if I press 1, 2 or 3 again they do nothing like they don't register.
My Weed Trek video archive: http://weedtrek.ca
User avatar
boiler
Posts: 16768
Joined: 21 Dec 2014, 02:44

Re: Mapped Keys Only Work Once

15 Jun 2021, 14:25

As another data point, the script works as expected for me. After the playlist finishes, I can press the same hotkey again, and it plays it again in a new random order.

The file variables are not the right variables for that loop. It is not a file loop it is a parsing loop, so you have to use A_LoopField. It doesn't make sense that it would not display anything for that.
User avatar
WeedTrek
Posts: 75
Joined: 22 Mar 2019, 14:29
Location: Cache Creek BC Canada
Contact:

Re: Mapped Keys Only Work Once

15 Jun 2021, 14:33

boiler wrote:
15 Jun 2021, 14:25
As another data point, the script works as expected for me. After the playlist finishes, I can press the same hotkey again, and it plays it again in a new random order.

The file variables are not the right variables for that loop. It is not a file loop it is a parsing loop, so you have to use A_LoopField. It doesn't make sense that it would not display anything for that.
oops yes A_LoopField shows the filename, but then in the next line soundplay adds the full path. I wasn't sure what happened at the end of the playlist, my problem is that "while" it is playing, I can press another key to start another playlist... works as expected, anytime. but if I press the same key or a previously pressed key, nothing at all happens, it's like the mapping is lost... or it's stuck in a loop somehow?
My Weed Trek video archive: http://weedtrek.ca
User avatar
mikeyww
Posts: 26599
Joined: 09 Sep 2014, 18:38

Re: Mapped Keys Only Work Once

15 Jun 2021, 14:38

Eliminate the "2" and "3" routines. Eliminate the ESC hotkey. You are left with the "1" routine. Post the revision where you are displaying the name of the file that is about to be played and does not play.
User avatar
boiler
Posts: 16768
Joined: 21 Dec 2014, 02:44

Re: Mapped Keys Only Work Once

15 Jun 2021, 14:52

I thought you said it wouldn't work again if you pressed it after it was finished. It won't interrupt the same one because that hotkey routine hasn't finished yet. You could try changing #MaxThreadsPerHotkey, but I doubt you'll get great results. It will probably do something like wait until a song finishes because it is waiting for that command to complete (because you use the Wait option, which makes sense), and then maybe it will start the new thread and eventually jump back to the initial one and finish that.
User avatar
WeedTrek
Posts: 75
Joined: 22 Mar 2019, 14:29
Location: Cache Creek BC Canada
Contact:

Re: Mapped Keys Only Work Once

15 Jun 2021, 14:54

mikeyww wrote:
15 Jun 2021, 14:38
Eliminate the "2" and "3" routines. Eliminate the ESC hotkey. You are left with the "1" routine. Post the revision where you are displaying the name of the file that is about to be played and does not play.

Code: Select all

#SingleInstance, Force

1::
FileList := ""							; clears file list
Loop, %A_ScriptDir%\playlist01\*.*, 1	; loops through "playlist01" folder for (presumably) audio files
	FileList .= A_LoopFileName "`n"		; puts audio files on a list
Sort, FileList, Random U				; randomizes list and removes duplicates
MsgBox, %FileList%						; (temp) shows the randomized list of files
Loop, parse, FileList, `n				; reads file list
{
	MsgBox, %A_LoopField%
	Soundplay, %A_ScriptDir%\playlist01\%A_LoopField%, wait	; plays each entry on list
}
return
This works fine (as it did before) but the actual problem remains. I can press 1 all day now and it won't do anything while playing, won't reload its mapping or interrupt anything at all. Leaving 2 and 3 in place will let me interrupt "1" and start playing playlist 2 or 3, which is good but none of these keys can be used again at that point. The desired result is to press any of these keys at any time, not just when the sound files are finished playing. This is where my lack of understanding is... I'm presuming a new (or same) keypress should break that soundplay loop and start a new one, not hang on the old one somehow :)

@boiler, that makes sense, the "wait" command may have something to do with that... but then how does "2" and "3" end up instantly working as they should? They interrupt SoundPlay somehow, but only once.
My Weed Trek video archive: http://weedtrek.ca
User avatar
boiler
Posts: 16768
Joined: 21 Dec 2014, 02:44

Re: Mapped Keys Only Work Once

15 Jun 2021, 14:57

WeedTrek wrote: @boiler, that makes sense, the "wait" command may have something to do with that... but then how does "2" and "3" end up instantly working as they should? They interrupt SoundPlay somehow, but only once.
Hotkeys 2 and 3 can interrupt hotkey 1 because they're different hotkeys. You can interrupt the same hotkey if you change the max number of threads allowed, but I'm not sure you'll get the results you want. Even if it interrupts itself cleanly, it should go back and finish the one you interrupted after the new one finishes.
User avatar
boiler
Posts: 16768
Joined: 21 Dec 2014, 02:44

Re: Mapped Keys Only Work Once

15 Jun 2021, 15:00

I'm a little surprised that a different hotkey interrupts mid-song because I thought it would wait until the current command finishes, and the Wait option would cause the current command to continue for a while (I would have thought). So maybe changing the number of threads per hotkey will interrupt the same hotkey routine mid-song, but I still think it will go back and finish the rest of the playlist from the initial hotkey press when the new hotkey routine finishes.
User avatar
WeedTrek
Posts: 75
Joined: 22 Mar 2019, 14:29
Location: Cache Creek BC Canada
Contact:

Re: Mapped Keys Only Work Once

15 Jun 2021, 15:03

boiler wrote:
15 Jun 2021, 14:57
Hotkeys 2 and 3 can interrupt hotkey 1 because they're different hotkeys. You can interrupt the same hotkey if you change the max number of threads allowed, but I'm not sure you'll get the results you want. Even if it interrupts itself cleanly, it should go back and finish the one you interrupted after the new one finishes.

I'm a little surprised that a different hotkey interrupts mid-song because I thought it would wait until the current command finishes, and the Wait option would cause the current command to continue for a while (I would have thought). So maybe changing the number of threads per hotkey will interrupt the same hotkey routine mid-song, but I still think it will go back and finish the rest of the playlist from the initial hotkey press when the new hotkey routine finishes.
that makes sense and then doesn't again lol! i'll go try that. :) thanks
My Weed Trek video archive: http://weedtrek.ca
User avatar
boiler
Posts: 16768
Joined: 21 Dec 2014, 02:44

Re: Mapped Keys Only Work Once  Topic is solved

15 Jun 2021, 15:04

My testing with adding #MaxThreadsPerHotkey, 2 does show that the same hotkey will interrupt mid-song, but that it indeed does go back and finish the playlist from the first press after the playlist from the second press finishes.
User avatar
WeedTrek
Posts: 75
Joined: 22 Mar 2019, 14:29
Location: Cache Creek BC Canada
Contact:

Re: Mapped Keys Only Work Once

15 Jun 2021, 15:11

boiler wrote:
15 Jun 2021, 15:04
My testing with adding #MaxThreadsPerHotkey, 2 does show that the same hotkey will interrupt mid-song, but that it indeed does go back and finish the playlist from the first press after the playlist from the second press finishes.
thanks very much for the info, I was always a little unclear about that, I tried out #MaxThreads 2 and saw that hotkey 2 interrupted hotkey 1 but then 3 does not work at all, confirming the first thread had been only "paused" while the second thread happens.
I need to figure out a way to make this happen :)
I think if I make three separate scripts, then a fourth one to open or close them, that may work.
My Weed Trek video archive: http://weedtrek.ca
User avatar
boiler
Posts: 16768
Joined: 21 Dec 2014, 02:44

Re: Mapped Keys Only Work Once

15 Jun 2021, 15:17

One way you could get it to simply interrupt and not go back and finish the playlist from the first press (whether it was interrupted by the same hotkey or a different one) is by setting a flag to indicate if it is actively playing a playlist when another hotkey is pressed, and if so, set a value in a .ini file and reload the script. The script would check the .ini file to see if it should play a certain playlist immediately upon reloading. It may sound more complicated than it is.
User avatar
WeedTrek
Posts: 75
Joined: 22 Mar 2019, 14:29
Location: Cache Creek BC Canada
Contact:

Re: Mapped Keys Only Work Once

15 Jun 2021, 15:54

boiler wrote:
15 Jun 2021, 15:17
One way you could get it to simply interrupt and not go back and finish the playlist from the first press (whether it was interrupted by the same hotkey or a different one) is by setting a flag to indicate if it is actively playing a playlist when another hotkey is pressed, and if so, set a value in a .ini file and reload the script. The script would check the .ini file to see if it should play a certain playlist immediately upon reloading. It may sound more complicated than it is.
that's an awesome idea, thanks very much! I think it will work with an INI entry, say if I press "2" it would write "2" and reload, then play "2" as retrieved from the INI. quite simple and elegant lol I'll report back
My Weed Trek video archive: http://weedtrek.ca
User avatar
boiler
Posts: 16768
Joined: 21 Dec 2014, 02:44

Re: Mapped Keys Only Work Once

15 Jun 2021, 17:11

Make sure after reading the value from the ini file, you write 0 to it so it doesn’t auto-play a playlist next time you run the script without a reload situation.
User avatar
WeedTrek
Posts: 75
Joined: 22 Mar 2019, 14:29
Location: Cache Creek BC Canada
Contact:

Re: Mapped Keys Only Work Once

15 Jun 2021, 18:22

I ended up on a slightly different route, has a few extra files but works great. I broke out each key mapping into their own EXE files, and I control them all with this:

Code: Select all

1::
run, 1.exe
Process, Close, 2.exe
Process, Close, 3.exe
return

2::
run, 2.exe
Process, Close, 1.exe
Process, Close, 3.exe
return

3::
run, 3.exe
Process, Close, 1.exe
Process, Close, 2.exe
return

Esc::
Process, Close, 1.exe
Process, Close, 2.exe
Process, Close, 3.exe
return
and then the other compiled EXEs are three versions of this (1, 2 and 3) each assigned to their own folders to play:

Code: Select all

#SingleInstance, Force
FileList := ""									; clears file list
Loop, Files, %A_ScriptDir%\playlist01\*.mp3, R	; loops through "playlist01" folder (and subfolders) for mp3 files (substitute playlist02 and playlist 03 for each compiled EXE)
	FileList .= A_LoopFileFullPath "`n"			; puts audio files on a list
Sort, FileList, Random U						; randomizes list and removes duplicates
Loop, parse, FileList, `n						; reads file list
	Soundplay, %A_LoopField%, wait				; plays each entry on list
ExitApp

Space::
SoundPlay, NoSuchFileHere.wav
return
Spacebar can interrupt the current loop iteration by playing nothing, after that the previous thread continues (but at the next item on the playlist), effectively making a "skip to next song" button :)
My Weed Trek video archive: http://weedtrek.ca

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 158 guests