How to play two sounds at once?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
mikeyww
Posts: 26973
Joined: 09 Sep 2014, 18:38

Re: How to play two sounds at once?

24 Mar 2024, 14:40

Rohwedder has demonstrated how to use the Sleep command, so you can add more such commands where you like them. SoundPlay has nothing to do with a GUI, though you may add a GUI if you like.

Code: Select all

; This script plays sounds concurrently
#Requires AutoHotkey v1.1.33.11
sound := ["Tea Time.mp3", "ENG.mp3"]
Global playFile := A_ScriptDir "\play.ahk"
GroupAdd playing, % playFile " ahk_class AutoHotkey"
OnExit("stop")
Gui Font, s10
For fileNum in sound
 Gui Add, Button, w210 gGo, % "Play " fileNum
Gui Add, Button, wp gF2, Play all
Gui Add, Button, wp gF3, Stop
Gui Show,, Sounds
Return

Go:
If RegExMatch(A_GuiControl, ".+?\K\d+$", fileNum)
 play(sound[fileNum])
Return

F3::stop() ; F3 = Stop play
F2::       ; F2 = Start play
For each, filePath in sound {
 play(filePath)
 Sleep 2000
}
Return

play(filePath) {  ; Play a sound file
 If FileExist(filePath) {
  If !FileExist(playFile)
   FileAppend,
    ( % LTrim
    #Requires AutoHotkey v1.1.33.11
    #SingleInstance Off
    #NoTrayIcon
    SoundPlay % A_Args[1], % WAIT := True
    ExitApp
    ), % playFile
  Run % playFile " """ filePath """"
 } Else MsgBox 48, Error, % "File not found.`n`n" filePath
}

stop(exitReason := "", exitCode := "") {
 DetectHiddenWindows On
 WinClose ahk_group playing ; Close all playing files (scripts)
}
Last edited by mikeyww on 24 Mar 2024, 14:59, edited 6 times in total.
User avatar
jaka1
Posts: 17
Joined: 27 Nov 2022, 08:09

Re: How to play two sounds at once?

24 Mar 2024, 14:45

Rohwedder wrote:Comment out the #NoTrayIcon of SoundPlay.ahk
I removed the #NoTrayIcon, and only one ahk file appeared. It is still the same, it ends after the setted Sleep time.
User avatar
jaka1
Posts: 17
Joined: 27 Nov 2022, 08:09

Re: How to play two sounds at once?

24 Mar 2024, 15:26

I probably did not express it well, I know that SoundPlay and Gui are independent commands, although my knowledge of writting codes is very poor… I am surprised, how quickly you wrote this syntax. Works fine:-) As standalone Gui for playing, it is great. But I am not sure, if I will be able to implement it for my need.
I was doing some testing with the first syntax from Rohwedder, and it seems to be good for me, however, there is still problem with stopping the both audios. And also that the first time both sounds do not play, and the second time they do.
Well, I will try to make some testing again tommorow and possibly to give you an example with the Gui. Thanks:-)
Last edited by jaka1 on 25 Mar 2024, 08:20, edited 1 time in total.
User avatar
mikeyww
Posts: 26973
Joined: 09 Sep 2014, 18:38

Re: How to play two sounds at once?

24 Mar 2024, 17:51

What the script does:
  1. Use a hotkey or a GUI to play all sounds, with a delay between starts.
  2. Use the GUI to play one or more individual sounds.
  3. Use a hotkey or the GUI to stop all sounds.
  4. All sounds stop when the script exits (including reloads).
As far as I can tell, this addresses all of your comments.
User avatar
jaka1
Posts: 17
Joined: 27 Nov 2022, 08:09

Re: How to play two sounds at once?

25 Mar 2024, 14:23

Thank you mikeyww for enumerated functions. That is not out of point for me. It seems that it works on my side as you have said. I am glad for this syntax.
However, it was intended to be two (possibly 3) songs on one button as it is in the exapmle below. But there only the first button works.

PS: I also tried to add other button with assigned audio in your syntax, but I have not managed it.

Code: Select all

:*:KEY::
SoundSet, 8
Gui Font, s10
Gui Add, Button, w210 gONE, Button 1
Gui Add, Button, wp   gTWO, Button 2
Gui Add, Button, wp      , Stop
Gui Show,, TEST
Return

ONE:
; {PART OF ROHWEDDER´S_FIRST SYNTAX}
SoundPlay, %A_WorkingDir%\Tea time.mp3
Sleep, 3000
SoundPlay(A_WorkingDir "\ENG.mp3")
; SOME OTHER CODE 

Return 
TWO:
; {PART OF ROHWEDDER´S_FIRST SYNTAX}
SoundPlay, %A_WorkingDir%\OtherAudio1.mp3
Sleep, 2000
SoundPlay(A_WorkingDir "\OtherAudio2.mp3")
; SOME OTHER CODE 
Return 

ButtonStop:
;  This my attempt do not work at all.
;  It would be good if the code below be functional without the hotkey through this stop button.

Send, q
Return 
; {PART OF ROHWEDDER´S_FIRST SYNTAX}
q::SoundPlayEnd() ; Ends all running SoundPlay(File)

SoundPlay(File) {
    FileDelete, ~.ahk
    FileAppend,#NoTrayIcon`nFileDelete`,~.ahk`nSoundPlay`,%File%`,1,~.ahk,UTF-8
    Run, ~.ahk
} SoundPlayEnd() {
	DetectHiddenWindows On
	SetTitleMatchMode, 2
	While, WinExist("~.ahk")
		WinClose 
}
Return 
User avatar
mikeyww
Posts: 26973
Joined: 09 Sep 2014, 18:38

Re: How to play two sounds at once?

25 Mar 2024, 14:47

I see no issues. You can omit the individual buttons if you wish.

Code: Select all

; This script plays sounds concurrently
#Requires AutoHotkey v1.1.33.11
sound := ["Tea Time.mp3", "ENG.mp3", "test3.mp3"]
Global playFile := A_ScriptDir "\play.ahk"
GroupAdd playing, % playFile " ahk_class AutoHotkey"
OnExit("stop")
Gui Font, s10
; For fileNum in sound
;  Gui Add, Button, w210 gGo, % "Play " fileNum
Gui Add, Button, w210   , Play all
Gui Add, Button, wp   gq, Stop
Gui Show,, Sounds

q::stop() ; q = Stop play

; Go:
; If RegExMatch(A_GuiControl, ".+?\K\d+$", fileNum)
;  play(sound[fileNum])
; Return

ButtonPlayAll:
For each, filePath in sound {
 play(filePath)
 Sleep 2000
}
Return

play(filePath) {  ; Play a sound file
 If FileExist(filePath) {
  If !FileExist(playFile)
   FileAppend,
    ( % LTrim
    #Requires AutoHotkey v1.1.33.11
    #SingleInstance Off
    #NoTrayIcon
    SoundPlay % A_Args[1], % WAIT := True
    ExitApp
    ), % playFile
  Run % playFile " """ filePath """"
 } Else MsgBox 48, Error, % "File not found.`n`n" filePath
}

stop(exitReason := "", exitCode := "") {
 DetectHiddenWindows On
 WinClose ahk_group playing ; Close all playing files (scripts)
}
User avatar
jaka1
Posts: 17
Joined: 27 Nov 2022, 08:09

Re: How to play two sounds at once?

25 Mar 2024, 15:19

Thank you mikeyww for the very quick response. The syntax works, and it is as I wanted:-) But I am not sure, if I will be able to add some other button(s) as I am going to… :) It is not easy for me to read such a high level code:-)
I will try it tomorrow. Hovever, your „speed keyboard“ would certainly be a safer choice :-)
User avatar
mikeyww
Posts: 26973
Joined: 09 Sep 2014, 18:38

Re: How to play two sounds at once?

25 Mar 2024, 16:15

You can do it your own way and can keep it basic.

Code: Select all

#Requires AutoHotkey v1.1.33.11
Gui Font, s10
Gui Add, Button, w250, 1
Gui Add, Button, w250, 2
Gui Add, Button, w250, Text
Gosub F3               ; Show the GUI

F3::Gui Show,, Buttons ; F3 = Show the GUI

Button1:               ; Button "1" was activated
MsgBox 1
Return

Button2:               ; Button "2" was activated
MsgBox 2
Return

ButtonText:            ; Button "Text" was activated
MsgBox % A_GuiControl  ; Show the button's variable name or text
Return
User avatar
jaka1
Posts: 17
Joined: 27 Nov 2022, 08:09

Re: How to play two sounds at once?

26 Mar 2024, 06:19

There seems to be a misunderstanding here, in any case I do not understand at all why you sent me such a syntax. I do not need simple GUI.
Basically all I wanted was to add another button to play other audios (multiple audios at one button). And the point is that I have problem to get it together in this type of code. I have tried it with both Rohwedder´s and your code, but without success. The last syntax I posted was an attempt at this.
User avatar
mikeyww
Posts: 26973
Joined: 09 Sep 2014, 18:38

Re: How to play two sounds at once?

26 Mar 2024, 07:32

Below is a way to play the sounds of any set of sounds among multiple sets.

Code: Select all

; This script plays sounds concurrently
#Requires AutoHotkey v1.1.33.11
set := [["Tea Time.mp3", "ENG.mp3"]  ; Sets of sounds
      , ["ENG.mp3", "test3.mp3"   ]]
Global playFile := A_ScriptDir "\play.ahk"
GroupAdd playing, % playFile " ahk_class AutoHotkey"
OnExit("stop")
Gui Font, s10
For setNum in set
 Gui Add, Button, w210 gGo, % "Play set " setNum
Gui Add, Button, wp   gq, Stop
Gui Show,, Sounds

q::stop() ; q = Stop play

Go:
If RegExMatch(A_GuiControl, ".+?\K\d+$", setNum)
 For each, filePath in set[setNum]
  play(filePath)
Return

play(filePath) {  ; Play a sound file
 If FileExist(filePath) {
  If !FileExist(playFile)
   FileAppend,
    ( % LTrim
    #Requires AutoHotkey v1.1.33.11
    #SingleInstance Off
    #NoTrayIcon
    SoundPlay % A_Args[1], % WAIT := True
    ExitApp
    ), % playFile
  Run % playFile " """ filePath """"
 } Else MsgBox 48, Error, % "File not found.`n`n" filePath
}

stop(exitReason := "", exitCode := "") {
 DetectHiddenWindows On
 WinClose ahk_group playing ; Close all playing files (scripts)
}
garry
Posts: 3771
Joined: 22 Dec 2013, 12:50

Re: How to play two sounds at once?

26 Mar 2024, 14:11

@mikeyww works fine , as test played 2 sounds together , remembers me on Dual-tone multi-frequency signaling (DTMF) telecommunication signaling system
Windows defender said , this ahk-script is a virus, TrojanDownloader: VBS/Adodb.D
I not allowed windows defender to check *.txt and *.ahk-files , only the text-file ahkcollected.txt in folder > a_appdata . "\Notepad++\Backup\ahkcollected.txt@2024-03-26_172834" was deleted . ( I used editor notepad++ )
Have no idea which part was detected as virus ...
User avatar
mikeyww
Posts: 26973
Joined: 09 Sep 2014, 18:38

Re: How to play two sounds at once?

26 Mar 2024, 14:23

Thank you, garry. You can also exclude AutoHotkey.exe, its variants, and its directory. I never knew that my scripts had uses in DTMF telecommunication systems, but great if they do! :)
garry
Posts: 3771
Joined: 22 Dec 2013, 12:50

Re: How to play two sounds at once?

26 Mar 2024, 14:35

@mikeyww thank you, I excluded > a_programfiles . "\Autohotkey" and also > a_programfiles . "\Autohotkey\autohotkey.exe" (?)
User avatar
jaka1
Posts: 17
Joined: 27 Nov 2022, 08:09

Re: How to play two sounds at once?

26 Mar 2024, 14:40

Thank you very much mikeyww, your syntax works well, and even I myself was able to add another button:-) And it is able to play out three audios at once, which is great.
But, I do not know how to add sleep, which means delay between the audio on the particular button (the audios playing on one button).
Also, I do not know how to implement other code to a particular button – the intention is that there will be another code on some buttons.
That was ther reason, I sent the example with GUI and that is why there was the part „SOME OTHER CODE“ under the specific button.
Then, I believe, it should be OK.
User avatar
mikeyww
Posts: 26973
Joined: 09 Sep 2014, 18:38

Re: How to play two sounds at once?

26 Mar 2024, 14:50

Even I myself was able to add another button
Glad to hear it. You can now add as many buttons as you wish. How to use buttons in a GUI
I do not know how to add sleep
How to add Sleep to your script
User avatar
jaka1
Posts: 17
Joined: 27 Nov 2022, 08:09

Re: How to play two sounds at once?

27 Mar 2024, 10:50

I looked at the links, but still I did not found a clue how to implement these basic commands of GUI or Sleep to your not-easy-for-me-to-read code. This is simply beyond my capabilities. So I am still basically where I was.

Below is my unsuccessful try with Sleep and SoundSet. And I am totally lost as far as assigning an other code under the Button.
So can I ask you for another dose of code?


SoundSet, 13
set := [["Tea Time.mp3", Sleep, 3000 SoundSet, 19 "ENG.mp3", Sleep, 3000 SoundSet, 21 "Audio1.mp3"] ; Sets of sounds
, ["ENG.mp3", "Audio1.mp3" "Audio2.mp3" ]
, ["ENG.mp3", "Audio2.mp3" ]]
Global playFile := A_WorkingDir "\play.ahk"
GroupAdd playing, % playFile " ahk_class AutoHotkey"
OnExit("stop")
Gui Font, s10
For setNum in set
Gui Add, Button, w210 gGo, % "Play set " setNum
Gui Add, Button, wp gq, Stop
Gui Show,, Sounds
...
...
User avatar
mikeyww
Posts: 26973
Joined: 09 Sep 2014, 18:38

Re: How to play two sounds at once?

27 Mar 2024, 10:56

Instead of working on two different problems at the same time, start by solving one problem.

Sleep is a command rather than an audio file to play, so it does not belong in your array.

The following script shows an example, on line 24, of using the Sleep command following a call to the "play" function.

viewtopic.php?p=565188#p564882

The enclosing braces denote a code block.

You can add a Sleep command to the place in the script where you would like the sleep to happen.
User avatar
jaka1
Posts: 17
Joined: 27 Nov 2022, 08:09

Re: How to play two sounds at once?

28 Mar 2024, 13:22

I know that „Sleep is a command rather than an audio file to play“, but I simply do not know how to apply it in the presented code… Link to the „code of block“ did not help me…
User avatar
mikeyww
Posts: 26973
Joined: 09 Sep 2014, 18:38

Re: How to play two sounds at once?

28 Mar 2024, 14:12

The way this command works is that you type, on a dedicated line, "Sleep " followed by the number of milliseconds to sleep. My earlier script provided an example of this command. I do not know how else to show the way, so I give up. Perhaps others will have more successful approaches to this. Best wishes for success.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Bing [Bot], GEOVAN, mikeyww and 216 guests