Certainly! It looks like you want to convert your JavaScript code into an AutoHotkey (AHK) script that replicates the same functionality. Below is an AHK script that:
- Defines a list of videos with their icons, paths, and durations.
- Creates a GUI with clickable image buttons for each video.
- Plays the selected video using VLC Media Player.
- Handles looping of the default video after the selected video finishes.
AHK Script:
#Persistent
; Define the list of videos
videos := []
videos.Push({icon: A_ScriptDir "\assets\globe1.png", video: A_ScriptDir "\video1.mp4", time: 320})
videos.Push({icon: A_ScriptDir "\assets\globe2.png", video: A_ScriptDir "\video2.mp4", time: 240})
videos.Push({icon: A_ScriptDir "\assets\globe3.png", video: A_ScriptDir "\video3.mp4", time: 180})
videos.Push({icon: A_ScriptDir "\assets\globe4.png", video: A_ScriptDir "\video4.mp4", time: 150})
videos.Push({icon: A_ScriptDir "\assets\globe5.png", video: A_ScriptDir "\video5.mp4", time: 420})
videos.Push({icon: A_ScriptDir "\assets\globe1.png", video: A_ScriptDir "\video1.mp4", time: 420}) ; Default video
; Set default video
defaultVideo := videos[1]
; Initialize variables
currentTimeout := 0
VideoPlayerPID := 0
currentVideoIndex := 0
currentShouldLoop := false
; Create a mapping between control HWNDs and video indexes
controlHwndToIndex := {}
; Create GUI and buttons
Gui, +Resize
Gui, Margin, 10,10
Gui, Add, Text,, Click a video:
xPos := 10
yPos := 50
loop % videos.Length()
{
index := A_Index
video := videos[index]
iconPath := video.icon
; Create picture control
Gui, Add, Picture, x%xPos% y%yPos% w100 h80 gPlayVideo hwndMyHwnd, %iconPath%
; Map the control's HWND to the index
controlHwndToIndex[MyHwnd] := index
xPos += 110
if (Mod(index, 5) = 0) ; Start new row after 5 images
{
xPos := 10
yPos += 90
}
}
Gui, Show,, Video Player
; Start playing the default video in a loop
GoToDefault()
Return
PlayVideo:
; Get the HWND of the control that was clicked
Critical
MouseGetPos,,, idCtrl
index := controlHwndToIndex[idCtrl]
if index
PlayVideoFunc(index, false)
Return
PlayVideoFunc(index, shouldLoop:=false)
{
global currentTimeout, defaultVideo, VideoPlayerPID
global currentVideoIndex, currentShouldLoop
; Validate the video index
if (index < 1 || index > videos.Length())
return
; Clear any existing timeout
if currentTimeout
{
SetTimer, TimeoutHandler, Off
currentTimeout := 0
}
; Stop the current video player if running
if VideoPlayerPID
{
Process, Close, %VideoPlayerPID%
VideoPlayerPID := 0
}
; Get video details
video := videos[index]
videoPath := video.video
; Start playing the video using VLC Media Player (adjust path if necessary)
Run, "C:\Program Files\VideoLAN\VLC\vlc.exe" --fullscreen --play-and-exit "%videoPath%", , , VideoPlayerPID
; Set a timer based on the video's duration
currentTimeout := video.time * 1000
SetTimer, TimeoutHandler, %currentTimeout%
; Store current index and loop flag
currentVideoIndex := index
currentShouldLoop := shouldLoop
Return
}
TimeoutHandler:
{
global currentVideoIndex, currentShouldLoop
if currentShouldLoop
{
PlayVideoFunc(currentVideoIndex, true)
}
else
{
GoToDefault()
}
Return
}
GoToDefault()
{
global defaultVideo
index := videos.IndexOf(defaultVideo)
if (index = "")
index := 1
PlayVideoFunc(index, true)
}
Stop()
{
global currentTimeout, VideoPlayerPID
; Clear the timer
if currentTimeout
{
SetTimer, TimeoutHandler, Off
currentTimeout := 0
}
; Stop the video player
if VideoPlayerPID
{
Process, Close, %VideoPlayerPID%
VideoPlayerPID := 0
}
}
GuiClose:
ExitApp
Return
Instructions:
-
Adjust the Paths:
- Ensure that the paths to your video files and icons are correct.
- Modify the VLC Media Player path in the
Run command if VLC is installed in a different directory.
-
Place Assets in the Correct Directory:
- Create an
assets folder in the same directory as your script and place your icon images there.
- Ensure that your video files are in the same directory as your script or adjust the paths accordingly.
-
Run the Script:
- Save the script with a
.ahk extension.
- Double-click the script to run it.
-
Interacting with the GUI:
- Click on any of the video icons to play the corresponding video.
- The default video (the first one in the list) will play on a loop when no other video is selected.
-
Stopping the Video:
- You can add a button or hotkey to call the
Stop() function if you need to stop the video manually.
Notes:
- This script uses VLC Media Player to play videos. Make sure VLC is installed on your system.
- The script uses the
--fullscreen and --play-and-exit options to play the video in full screen and exit VLC after the video ends.
- The default video loops indefinitely until another video is selected.
- The images in the GUI act as buttons due to the
gPlayVideo label.
Troubleshooting:
- If videos do not play, check that the video paths are correct and that VLC Media Player is installed in the specified location.
- If images do not appear in the GUI, ensure the icon paths are correct and that image files are accessible.
- For security reasons, ensure that running scripts and executing programs is allowed on your system.
Customization:
- You can add more videos by appending to the
videos array.
- Adjust the GUI layout by modifying the
xPos, yPos, and spacing calculations.
This AHK script replicates the functionality of your JavaScript code and runs independently on your Windows system, providing a GUI interface to select and play videos with the specified behavior.