How can i play a random song?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
John1
Posts: 236
Joined: 11 May 2020, 11:54

How can i play a random song?

Post by John1 » 18 May 2021, 12:31

Hello,

How can i play a random song with the format mp3 from the specific folder "D:\1\Sounds1" and the Folder "D:\AllMusic\Music" ?

Thank you!
User avatar
mikeyww
Posts: 26596
Joined: 09 Sep 2014, 18:38

Re: How can i play a random song?

Post by mikeyww » 18 May 2021, 12:45

Code: Select all

#SingleInstance Force
sound := []
For k, dir in ["D:\1\Sounds1", "D:\AllMusic\Music"]
 Loop, Files, % dir "\*.mp3"
  sound.Push(A_LoopFilePath)
If !sound.Count() {
 MsgBox, 48, Error, No files were found. Aborting.
 Return
} Else Random, rand, 1, sound.Count()
MsgBox,, Sound, % "Playing " sound[rand], 1
SoundPlay, % sound[rand], Wait
John1
Posts: 236
Joined: 11 May 2020, 11:54

Re: How can i play a random song?

Post by John1 » 18 May 2021, 13:31

@mikeyww
Thank you!
How can i make the messagebox text bigger?
User avatar
mikeyww
Posts: 26596
Joined: 09 Sep 2014, 18:38

Re: How can i play a random song?

Post by mikeyww » 18 May 2021, 14:12

Code: Select all

#SingleInstance Force
sound := []
For k, dir in ["D:\1\Sounds1", "D:\AllMusic\Music"]
 Loop, Files, % dir "\*.mp3"
  sound.Push(A_LoopFilePath)
If !sound.Count() {
 MsgBox, 48, Error, No files were found. Aborting.
 Return
} Else Random, rand, 1, sound.Count()
Gui, New
Gui, Font, s22 w500
Gui, Color, F8DC75
Gui, Add, Text, w320 Center, % sound[rand]
Gui, Show,, Now playing
SoundPlay, % sound[rand], Wait
Gui, Destroy
John1
Posts: 236
Joined: 11 May 2020, 11:54

Re: How can i play a random song?

Post by John1 » 18 May 2021, 14:23

@mikeyww

Thank you!
How can i make the message box not appear in the middle, it should only appear in the taskbar. When i want to look what song it is i click manually at it on the taskbar.
So the message box can not interrupt other commands.
User avatar
mikeyww
Posts: 26596
Joined: 09 Sep 2014, 18:38

Re: How can i play a random song?

Post by mikeyww » 18 May 2021, 14:33

Use

Code: Select all

Gui, Show, Minimize, Now playing
Explained (with lots of other options): Gui
John1
Posts: 236
Joined: 11 May 2020, 11:54

Re: How can i play a random song?

Post by John1 » 18 May 2021, 14:34

@mikeyww

and how can i make it also play songs from the two folders subfolders?
User avatar
mikeyww
Posts: 26596
Joined: 09 Sep 2014, 18:38

Re: How can i play a random song?

Post by mikeyww » 18 May 2021, 14:36

See Loop, Files. Use mode R.
John1
Posts: 236
Joined: 11 May 2020, 11:54

Re: How can i play a random song?

Post by John1 » 18 May 2021, 14:43

@mikeyww
Thanks again for your help! Have a good day!
John1
Posts: 236
Joined: 11 May 2020, 11:54

Re: How can i play a random song?

Post by John1 » 21 May 2021, 06:28

@mikeyww

How can i do this? :
when the music is already playing and i press the button again then it stops playing.


Thank you!
User avatar
mikeyww
Posts: 26596
Joined: 09 Sep 2014, 18:38

Re: How can i play a random song?

Post by mikeyww » 21 May 2021, 06:42

Code: Select all

#SingleInstance Force
#MaxThreadsPerHotkey 2
sound := []
For k, dir in ["D:\1\Sounds1", "D:\AllMusic\Music"]
 Loop, Files, % dir "\*.mp3", R
  sound.Push(A_LoopFilePath)

F3::
If !sound.Count() {
 MsgBox, 48, Error, No files were found. Aborting.
 Return
}
If (on := !on) {
 Random, rand, 1, sound.Count()
 Gui, New
 Gui, Font, s22 w500
 Gui, Color, F8DC75
 Gui, Add, Text, w320 Center, % sound[rand]
 Gui, Show,, Now playing
 SoundPlay, % sound[rand], Wait
}
Gui, Destroy
on := False
SoundPlay, ""
Return
John1
Posts: 236
Joined: 11 May 2020, 11:54

Re: How can i play a random song?

Post by John1 » 25 May 2021, 06:25

@mikeyww

How can I play only mp3 files longer than 60 seconds?

Thank you!
User avatar
mikeyww
Posts: 26596
Joined: 09 Sep 2014, 18:38

Re: How can i play a random song?

Post by mikeyww » 25 May 2021, 07:11

You got me on that one! I think you will probably want to read the MP3 metadata to determine the audio duration or length. You could search the forum for that. Command-line tools are also available to do it.
teadrinker
Posts: 4309
Joined: 29 Mar 2015, 09:41
Contact:

Re: How can i play a random song?

Post by teadrinker » 25 May 2021, 10:32

How to Find the Duration of a Media File

Code: Select all

filePath := "J:\_MUSIC_\Progressive Rock\Gordian Knot\1998 Gordian Knot\03.Reflections.mp3"

sec := GetMediaDuration(filePath)
min := sec//60
restSec := mod(sec, 60)
MsgBox, % min . " min " . restSec . " sec"

GetMediaDuration(mediaFilePath) {
   static MF_VERSION := 0x2, MFSTARTUP_NOSOCKET := 0x1, MF_RESOLUTION_MEDIASOURCE := 1
        , IID_IMFMediaSource := "{279A808D-AEC7-40C8-9C6B-A6B492C78A66}"
        , MF_PD_DURATION     := "{6c990d33-bb8e-477a-8598-0d5d96fcd88a}"

   DllCall("Mfplat\MFStartup", "UInt", MF_VERSION, "UInt", MFSTARTUP_NOSOCKET, "UInt")
   DllCall("Mfplat\MFCreateSourceResolver", "PtrP", pIMFSourceResolver)

   ; IMFSourceResolver::CreateObjectFromURL
   DllCall(NumGet(NumGet(pIMFSourceResolver + 0) + A_PtrSize*3), "Ptr", pIMFSourceResolver, "WStr", mediaFilePath 
                                                               , "UInt", MF_RESOLUTION_MEDIASOURCE, "Ptr", 0
                                                               , "UIntP", MF_OBJECT_TYPE, "PtrP", pIUnknown, "UInt")
   pIMFMediaSource := ComObjQuery(pIUnknown, IID_IMFMediaSource)
   ObjRelease(pIUnknown)

   ; IMFMediaSource::CreatePresentationDescriptor
   DllCall(NumGet(NumGet(pIMFMediaSource + 0) + A_PtrSize*8), "Ptr", pIMFMediaSource, "PtrP", pIMFPresentationDescriptor, "UInt")

   VarSetCapacity(CLSID, 16, 0)
   DllCall("ole32\CLSIDFromString", "WStr", MF_PD_DURATION, "Ptr", &CLSID, "UInt")
   ; IMFPresentationDescriptor::GetUINT64
   DllCall(NumGet(NumGet(pIMFPresentationDescriptor + 0) + A_PtrSize*8), "Ptr", pIMFPresentationDescriptor, "Ptr", &CLSID, "UInt64P", duration)
   
   ObjRelease(pIMFPresentationDescriptor), ObjRelease(pIMFMediaSource), ObjRelease(pIMFSourceResolver)
   DllCall("Mfplat\MFShutdown")
   
   Return sec := duration//10**7
}
User avatar
rommmcek
Posts: 1470
Joined: 15 Aug 2014, 15:18

Re: How can i play a random song?

Post by rommmcek » 25 May 2021, 19:53

Awesome function to get Media Duration!

P.s.: try pIMFMediaSource := ComObjQuery(pIUnknown, IID_IMFMediaSource) seems to suffice for preventing error when Media file is corrupt.
Spoiler
teadrinker
Posts: 4309
Joined: 29 Mar 2015, 09:41
Contact:

Re: How can i play a random song?

Post by teadrinker » 25 May 2021, 20:13

It's not necessarily corrupt, it can be not native for windows.
User avatar
rommmcek
Posts: 1470
Joined: 15 Aug 2014, 15:18

Re: How can i play a random song?

Post by rommmcek » 26 May 2021, 03:24

O.k. thanks for supplemental info!
Post Reply

Return to “Ask for Help (v1)”