Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Sound.ahk - Sound functions for use with AutoHotkey


  • This topic is locked This topic is locked
36 replies to this topic
fincs
  • Moderators
  • 1662 posts
  • Last active:
  • Joined: 05 May 2007
Hi,
This is a library to include in StdLib. With this functions, you got simple control on MCI (Multimedia Control Interface, I think). A sustitute of SoundPlay. Here is the code:

;
; AutoHotkey Version: 1.0.47.00
; Language:       Anyone
; Author:         Fincs <[email protected]>
;
; Script Function:
;	Functions to handle multimedia files
;

;===============================================================================
;
; Function Name:   Sound_Open
; Description::    Opens a sound file for use with other sound functions
; Parameter(s):    File - The sound file
;                  Alias [optional] - A name such as sound1, if you do not
;                                     specify one it is automatically generated
; Return Value(s): The sound handle or a 0 to indicate failure
; ErrorLevel value:	0 - No Error
;                   1 - Open failed
;                   2 - File doesn't exist
;
;===============================================================================

Sound_Open(File, Alias=""){
	Static SoundNumber = 0
	IfNotExist, %File%
	{
		ErrorLevel = 2
		Return 0
	}
	If Alias =
	{
		SoundNumber ++
		Alias = AutoHotkey%SoundNumber%
	}
	Loop, %File%
		File_Short = %A_LoopFileShortPath%
	r := Sound_SendString("open " File_Short " alias " Alias)
	If r
	{
		ErrorLevel = 1
		Return 0
	}Else{
		ErrorLevel = 0
		Return %Alias%
	}
}

;===============================================================================
;
; Function Name:   Sound_Close
; Description::    Closes a sound
; Parameter(s):    SoundHandle - Sound handle returned by Sound_Open
; Return Value(s): 1 - Success, 0 - Failure
;
;===============================================================================

Sound_Close(SoundHandle){
	r := Sound_SendString("close " SoundHandle)
	Return NOT r
}

;===============================================================================
;
; Function Name:   Sound_Play
; Description::    Plays a sound from the current position (beginning is the default)
; Parameter(s):    SoundHandle - Sound handle returned by Sound_Open
;				   Wait - If set to 1 the script will wait for the sound to finish before continuing
;						 - If set to 0 the script will continue while the sound is playing
; Return Value(s): 1 - Success, 0 - Failure
;
;===============================================================================

Sound_Play(SoundHandle, Wait=0){
	If(Wait <> 0 AND Wait <> 1)
		Return 0
	If Wait
		r := Sound_SendString("play " SoundHandle " wait")
	Else
		r := Sound_SendString("play " SoundHandle)
	Return NOT r
}

;===============================================================================
;
; Function Name: Sound_Stop
; Description::    Stops the sound
; Parameter(s):    SoundHandle - Sound handle returned by Sound_Open
; Return Value(s): 1 - Success, 0 - Failure
;
;===============================================================================

Sound_Stop(SoundHandle){
	r := Sound_SendString("seek " SoundHandle " to start")
	r2 := Sound_SendString("stop " SoundHandle)
	If(r AND r2)
	{
		Return 0
	}Else{
		Return 1
	}
}

;===============================================================================
;
; Function Name:   Sound_Pause
; Description::    Pauses the sound
; Parameter(s):    SoundHandle - Sound handle returned by Sound_Open
; Return Value(s): 1 - Success, 0  - Failure
;
;===============================================================================

Sound_Pause(SoundHandle){
	r := Sound_SendString("pause " SoundHandle)
	Return NOT r
}

;===============================================================================
;
; Function Name:   Sound_Resume
; Description::    Resumes the sound after being paused
; Parameter(s):    SoundHandle - Sound handle returned by Sound_Open
; Return Value(s): 1 - Success, 0  - Failure
;
;===============================================================================

Sound_Resume(SoundHandle){
	r := Sound_SendString("resume " SoundHandle)
	Return NOT r
}

;===============================================================================
;
; Function Name:   Sound_Length
; Description::    Returns the length of the sound
; Parameter(s):    SoundHandle - Sound handle returned by Sound_Open
; Return Value(s): Length of the sound - Success
;
;===============================================================================

Sound_Length(SoundHandle){
	r := Sound_SendString("set time format miliseconds", 1)
	If r
		Return 0
	r := Sound_SendString("status " SoundHandle " length", 1, 1)
	Return %r%
}

;===============================================================================
;
; Function Name:   Sound_Seek
; Description::    Seeks the sound to a specified time
; Parameter(s):    SoundHandle - Sound handle returned by Sound_Open
;                  Hour, Min, Sec - Time to seek to
; Return Value(s): 1 - Success, 0 - Failure,
;
;===============================================================================

Sound_Seek(SoundHandle, Hour, Min, Sec){
	milli := 0
	r := Sound_SendString("set time format milliseconds", 1)
	If r
		Return 0
	milli += Sec  * 1000
	milli += Min  * 1000 * 60
	milli += Hour * 1000 * 60 * 60
	r := Sound_SendString("seek " SoundHandle " to " milli)
	Return NOT r
}

;===============================================================================
;
; Function Name:   Sound_Status
; Description::    All devices can return the "not ready", "paused", "playing", and "stopped" values.
;				   Some devices can return the additional "open", "parked", "recording", and "seeking" values.(MSDN)
; Parameter(s):    SoundHandle - Sound handle returned by Sound_Open
; Return Value(s): Sound Status
;
;===============================================================================

Sound_Status(SoundHandle){
	Return Sound_SendString("status " SoundHandle " mode", 1, 1)
}

;===============================================================================
;
; Function Name:   Sound_Pos
; Description::    Returns the current position of the song
; Parameter(s):    SoundHandle - Sound handle returned by Sound_Open
; Return Value(s): Current Position - Success, 0 - Failure
;
;===============================================================================

Sound_Pos(SoundHandle){
	r := Sound_SendString("set time format miliseconds", 1)
	If r
		Return 0
	r := Sound_SendString("status " SoundHandle " position", 1, 1)
	Return %r%
}

;===============================================================================

Sound_SendString(string, UseSend=0, ReturnTemp=0){
	If UseSend
	{
		VarSetCapacity(stat1, 32, 32)
		DllCall("winmm.dll\mciSendStringA", "UInt", &string, "UInt", &stat1, "Int", 32, "Int", 0) 
	}Else{
		DllCall("winmm.dll\mciExecute", "str", string)
	}
	If(UseSend And ReturnTemp)
		Return stat1
	Else
		Return %ErrorLevel%
}

And a little music player:

FileSelectFile, file, 1,, Pick a sound file
if file =
	ExitApp

hSound := Sound_Open(file, "myfile")
If Not hSound
	ExitApp

playing = 0
tooltip = 1

ToolTip F9 - Play/Pause`nF10 - Stop`nF11 - Show/Hide Tooltip
Sleep 2000
ToolTip

len := Sound_Length(hSound)

Loop
{
	If !playing
		Continue
	If(Sound_Pos(hSound) = Sound_Length(hSound))
		Break
	If tooltip
		ToolTip % Tohhmmss(Sound_Pos(hSound))
}

If(NOT Sound_Close(hSound))
	MsgBox Error closing sound file

ExitApp

Tohhmmss(milli){
	min  := Floor(milli / (1000 * 60))
	hour := Floor(milli / (1000 * 3600))
	sec  := Floor(Floor(milli/1000) - (min * 60))
	Return hour ":" min ":" sec
}

F9::
status := Sound_Status(hSound)
If(status = "stopped" OR status = "paused")
{
	If status = stopped
		Sound_Play(hSound)
	Else
		Sound_Resume(hSound)
	playing = 1
}Else{
	Sound_Pause(hSound)
	playing = 0
	ToolTip
}
Return

F10::
Sound_Stop(hSound)
playing = 0
ToolTip
Return

F11::
tooltip := not tooltip
If !tooltip
	ToolTip
Return

Enjoy!

fincs
  • Moderators
  • 1662 posts
  • Last active:
  • Joined: 05 May 2007
28 views and no replies?

ManaUser
  • Members
  • 1121 posts
  • Last active: Dec 07 2016 04:24 PM
  • Joined: 24 May 2007
Looks handy. I think it's great to see some of these more powerful DllCall functions made available in a convenient format. I would have used this just recently if it had been available at the time.

jballi
  • Members
  • 1029 posts
  • Last active:
  • Joined: 01 Oct 2005

Looks handy. I think it's great to see some of these more powerful DllCall functions made available in a convenient format. I would have used this just recently if it had been available at the time.


Ditto. Thanks for sharing. :)

Sean
  • Members
  • 2462 posts
  • Last active: Feb 07 2012 04:00 AM
  • Joined: 12 Feb 2007
I think you may utilize MCI's Callback via OnMessage. It's just a small suggestion.

#SingleInstance Force

mmedia = "%1%"

DetectHiddenWindows, On
Process, Exist
hAHK := WinExist("ahk_pid " . ErrorLevel)

OnMessage(MM_MCINOTIFY	:= 0x03B9, "MM_MCINOTIFY")
;OnMessage(MM_MCISIGNAL	:= 0x03CB, "MM_MCISIGNAL")

DllCall("winmm\mciSendStringA", "str", "open " . mmedia . " alias mmedia [color=orange]type mpegvideo[/color] [color=cyan]style overlapped[/color] shareable", "Uint", 0, "Uint", 0, "Uint", 0)
DllCall("winmm\mciSendStringA", "str", "play mmedia [color=red]notify[/color]", "Uint", 0, "Uint", 0, "Uint", [color=red]hAHK[/color])

MM_MCINOTIFY(wParam, lParam)
{
/*
	MCI_NOTIFY_SUCCESSFUL = 0x1
	MCI_NOTIFY_SUPERSEDED = 0x2
	MCI_NOTIFY_ABORTED    = 0x4
	MCI_NOTIFY_FAILURE    = 0x8
*/
	DllCall("winmm\mciSendStringA", "str", "close mmedia", "Uint", 0, "Uint", 0, "Uint", 0)
	ExitApp
}


jballi
  • Members
  • 1029 posts
  • Last active:
  • Joined: 01 Oct 2005
I spent a few hours working (read: having fun) with these functions and for the most part, everything works as advertised but I had a few minor problems, issues, comments that I wanted to share.

[*:3jo2yqzz]You might want to consider renaming the post/functions from Sound_* to something else like Media_* because these functions will play a lot more than just sound files. I was able to use these functions to play video files (mpg, avi, etc.) without any difficulty. I don't know all the file types that can be played but I'm fairly certain that it depends on the media types that are registered on your computer.

[*:3jo2yqzz]The Sound_Length function does not appear to very accurate especially when playing VBR MP3s. My version of Windows Media Player has the same same problem... it must use the same code. It's kinda strange because the Sound_Pos appears to work fine.

[*:3jo2yqzz]I might be using it incorrectly but I was never able to get the Sound_Seek function to work. I tried it on several different types of media without any luck. Small parameters values (2 seconds for example), just cause the device to stop. Large parameter values actually cause to function to pop-up an error message dialog. Also, since the Sound_Length and Sound_Pos functions return time in milliseconds, it would probably make sense to change the Sound_Seek function to use milliseconds as well.Additional stuff...

This is certainly not a bug but a comment to anyone to that wants to use these functions with GUI buttons, hotkeys and/or timers. Because of the size and/or complexity of some media files, I wasn't able to keep the Sound_* functions from interfering with each other until I did 2 things:

First of all, I wrapped the key function calls in a "Critical" wrapper. For example:
Critical
Sound_Close(hMediaFile)
Critical off
sleep -1
See the Critical documentation in the AutoHotkey help file for more information.

Next, I added a global variable that I set when a media file is opened/closed then I checked it to make sure the media file is open before performing other Sound_* functions on it.

These two minor changes allowed me to button, hotkey, and timer the heck out of my script without any interference.

I'm certain that these recommendations will differ if you decide to incorporate the Notify/Callback functionality (Sean's suggestion) but so far, you haven't indicated whether you plan to use it or not. I don't know enough about it to comment on it's value.

Them be my thoughts...

fincs
  • Moderators
  • 1662 posts
  • Last active:
  • Joined: 05 May 2007

You might want to consider renaming the post/functions from Sound_* to something else like Media_*

I translated the AutoIt 3 Sound UDFs to AutoHotkey, for that reason I named the functions Sound_* (in AutoIt3 "_Sound*"). But you are right, I will rename the functions to "Media_*".

The Sound_Length function does not appear to very accurate especially when playing VBR MP3s.

This is a problem of your Windows version, not a problem of the functions! But in my PC, I don't get this problem.

I might be using it incorrectly but I was never able to get the Sound_Seek function to work.

You must write Sound_Pause(SoundHandle) after Sound_Seek and Sound_Play(SoundHandle) before Sound_Seek. Example:
h := Sound_Open("MyMp3File.mp3")
Sound_Play(h)
Sleep, 100
; Incorrect:
; Sound_Seek(h, 0, 0, 2)
; Correct:
Sound_Pause(h)
Sound_Seek(h, 0, 0, 2)
Sound_Play(h)
; -------
Sleep, 1000
Sound_Stop(h)
Sound_Close(h)

Also, since the Sound_Length and Sound_Pos functions return time in milliseconds, it would probably make sense to change the Sound_Seek function to use milliseconds as well.

You are right again. I will change the Sound_Seek function to use milliseconds and I will add the functions "Sound_ToMilliseconds" and "Sound_ToHHMMSS".

I'm certain that these recommendations will differ if you decide to incorporate the Notify/Callback functionality (Sean's suggestion) but so far, you haven't indicated whether you plan to use it or not.

I will add this functionality to Sound_Play:
Sound_Play(SoundHandle [, Wait=0, OnStopFunction=""]) ; When the file is at EOF OnStopFunction is called.


Trubbleguy
  • Members
  • 122 posts
  • Last active: Jan 15 2017 10:50 AM
  • Joined: 20 Jan 2007
nice, made 2 files to include, media1.ahk and media2.ahk, can control and play 2 files at once, dj is easy this way, i have a nice 4 second overlap happening, i also cut down the media seek code like this using sec as the whole number and not just the seconds.
Media1_Seek(SoundHandle, Sec){
;   milli := 0
;   r := Media1_SendString("set time format milliseconds", 1)
;   If r
;     Return 0
;   milli += Sec  * 1000
;   milli += Min  * 1000 * 60
;   milli += Hour * 1000 * 60 * 60
   r := Media1_SendString("seek " SoundHandle " to " Sec)
   Return NOT r
}
enables me to scan through any media file using any number less than medialengh
i also fixed a naming issue getting files from across a network by puttin "" around the file variable, this allowed filenames with spaces and also allowed the full file name eg. File_Short = "%A_LoopFileFullPath%"
i also found a bug in the file length data too, it changed if i used FFSHOW and threw all the variables out in mp3 files, but not avi or mpg.

i have a dual media player with dual video screens and can mix video and mp3 files perfectly. winamp is in trouble.....lmao

vlcek
  • Members
  • 341 posts
  • Last active: Apr 03 2014 01:27 PM
  • Joined: 19 Feb 2007
I have problem with the music player.
I run player, but

---------------------------
player.ahk
---------------------------
Error: Call to nonexistent function.

Specifically: Sound_Open(file, "myfile")

Line#
001: FileSelectFile,file,1,,Pick a sound file
002: if file =
003: ExitApp
---> 005: hSound := Sound_Open(file, "myfile")
006: if Not hSound
007: ExitApp
009: playing = 0
010: tooltip = 1
012: ToolTip,F9 - Play/Pause
F10 - Stop
F11 - Show/Hide Tooltip
013: Sleep,2000
014: ToolTip

The program will exit.
---------------------------
OK
---------------------------
Thanks.

jojo
  • Guests
  • Last active:
  • Joined: --

You must write Sound_Pause(SoundHandle) after Sound_Seek and Sound_Play(SoundHandle) before Sound_Seek.

isn't it intelligent to call sound_pause in the beginning of sound_seek and sound_play in the end?
i have the same question about the critical wrapper.....

jojo
  • Guests
  • Last active:
  • Joined: --
@vlek:
you have to include the main file in the player, or copy both into one file

playerA
  • Guests
  • Last active:
  • Joined: --
Hi Jojo, thank you 4 the script and sorry 4 my newbie's question:
I put your functions and the player in a unique file (the function before)
Run, pause and stop run very well.
Now, how to jump for example at 1'33" of a mp3?
I try:
f12::
Sound_Seek(SoundHandle, Hour, Min, Sec)
   milli := 3330
Return
But not only not run (I'm not surprized) but freeze my pc (I'm not sure if is a script responsability)
Thanks in advance.
:)

playerA
  • Guests
  • Last active:
  • Joined: --
My apologize to Fincs

Hi Jojo, thank you 4 the script

(I mean Fincs)
:oops: :oops: :oops:

Thanks Jojo however

you have to include the main file in the player, or copy both into one file

this resolve my first prob (same of vlcek) :D
But I'm looking 4:
how to jump for example at 1'33" of a mp3?
I try:
;
; AutoHotkey Version: 1.0.47.00
; Language:       Anyone
; Author:         Fincs <[email protected]>
;
; Script Function:
;   Functions to handle multimedia files
;

;===============================================================================
;
; Function Name:   Sound_Open
; Description::    Opens a sound file for use with other sound functions
; Parameter(s):    File - The sound file
;                  Alias [optional] - A name such as sound1, if you do not
;                                     specify one it is automatically generated
; Return Value(s): The sound handle or a 0 to indicate failure
; ErrorLevel value:   0 - No Error
;                   1 - Open failed
;                   2 - File doesn't exist
;
;===============================================================================

Sound_Open(File, Alias=""){
   Static SoundNumber = 0
   IfNotExist, %File%
   {
      ErrorLevel = 2
      Return 0
   }
   If Alias =
   {
      SoundNumber ++
      Alias = AutoHotkey%SoundNumber%
   }
   Loop, %File%
      File_Short = %A_LoopFileShortPath%
   r := Sound_SendString("open " File_Short " alias " Alias)
   If r
   {
      ErrorLevel = 1
      Return 0
   }Else{
      ErrorLevel = 0
      Return %Alias%
   }
}

;===============================================================================
;
; Function Name:   Sound_Close
; Description::    Closes a sound
; Parameter(s):    SoundHandle - Sound handle returned by Sound_Open
; Return Value(s): 1 - Success, 0 - Failure
;
;===============================================================================

Sound_Close(SoundHandle){
   r := Sound_SendString("close " SoundHandle)
   Return NOT r
}

;===============================================================================
;
; Function Name:   Sound_Play
; Description::    Plays a sound from the current position (beginning is the default)
; Parameter(s):    SoundHandle - Sound handle returned by Sound_Open
;               Wait - If set to 1 the script will wait for the sound to finish before continuing
;                   - If set to 0 the script will continue while the sound is playing
; Return Value(s): 1 - Success, 0 - Failure
;
;===============================================================================

Sound_Play(SoundHandle, Wait=0){
   If(Wait <> 0 AND Wait <> 1)
      Return 0
   If Wait
      r := Sound_SendString("play " SoundHandle " wait")
   Else
      r := Sound_SendString("play " SoundHandle)
   Return NOT r
}

;===============================================================================
;
; Function Name: Sound_Stop
; Description::    Stops the sound
; Parameter(s):    SoundHandle - Sound handle returned by Sound_Open
; Return Value(s): 1 - Success, 0 - Failure
;
;===============================================================================

Sound_Stop(SoundHandle){
   r := Sound_SendString("seek " SoundHandle " to start")
   r2 := Sound_SendString("stop " SoundHandle)
   If(r AND r2)
   {
      Return 0
   }Else{
      Return 1
   }
}

;===============================================================================
;
; Function Name:   Sound_Pause
; Description::    Pauses the sound
; Parameter(s):    SoundHandle - Sound handle returned by Sound_Open
; Return Value(s): 1 - Success, 0  - Failure
;
;===============================================================================

Sound_Pause(SoundHandle){
   r := Sound_SendString("pause " SoundHandle)
   Return NOT r
}

;===============================================================================
;
; Function Name:   Sound_Resume
; Description::    Resumes the sound after being paused
; Parameter(s):    SoundHandle - Sound handle returned by Sound_Open
; Return Value(s): 1 - Success, 0  - Failure
;
;===============================================================================

Sound_Resume(SoundHandle){
   r := Sound_SendString("resume " SoundHandle)
   Return NOT r
}

;===============================================================================
;
; Function Name:   Sound_Length
; Description::    Returns the length of the sound
; Parameter(s):    SoundHandle - Sound handle returned by Sound_Open
; Return Value(s): Length of the sound - Success
;
;===============================================================================

Sound_Length(SoundHandle){
   r := Sound_SendString("set time format miliseconds", 1)
   If r
      Return 0
   r := Sound_SendString("status " SoundHandle " length", 1, 1)
   Return %r%
}

;===============================================================================
;
; Function Name:   Sound_Seek
; Description::    Seeks the sound to a specified time
; Parameter(s):    SoundHandle - Sound handle returned by Sound_Open
;                  Hour, Min, Sec - Time to seek to
; Return Value(s): 1 - Success, 0 - Failure,
;
;===============================================================================

Sound_Seek(SoundHandle, Hour, Min, Sec){
   milli := 0
   r := Sound_SendString("set time format milliseconds", 1)
   If r
      Return 0
   milli += Sec  * 1000
   milli += Min  * 1000 * 60
   milli += Hour * 1000 * 60 * 60
   r := Sound_SendString("seek " SoundHandle " to " milli)
   Return NOT r
}

;===============================================================================
;
; Function Name:   Sound_Status
; Description::    All devices can return the "not ready", "paused", "playing", and "stopped" values.
;               Some devices can return the additional "open", "parked", "recording", and "seeking" values.(MSDN)
; Parameter(s):    SoundHandle - Sound handle returned by Sound_Open
; Return Value(s): Sound Status
;
;===============================================================================

Sound_Status(SoundHandle){
   Return Sound_SendString("status " SoundHandle " mode", 1, 1)
}

;===============================================================================
;
; Function Name:   Sound_Pos
; Description::    Returns the current position of the song
; Parameter(s):    SoundHandle - Sound handle returned by Sound_Open
; Return Value(s): Current Position - Success, 0 - Failure
;
;===============================================================================

Sound_Pos(SoundHandle){
   r := Sound_SendString("set time format miliseconds", 1)
   If r
      Return 0
   r := Sound_SendString("status " SoundHandle " position", 1, 1)
   Return %r%
}

;===============================================================================

Sound_SendString(string, UseSend=0, ReturnTemp=0){
   If UseSend
   {
      VarSetCapacity(stat1, 32, 32)
      DllCall("winmm.dll\mciSendStringA", "UInt", &string, "UInt", &stat1, "Int", 32, "Int", 0) 
   }Else{
      DllCall("winmm.dll\mciExecute", "str", string)
   }
   If(UseSend And ReturnTemp)
      Return stat1
   Else
      Return %ErrorLevel%
}

FileSelectFile, file, 1,, Pick a sound file
if file =
   ExitApp

hSound := Sound_Open(file, "myfile")
If Not hSound
   ExitApp

playing = 0
tooltip = 1

ToolTip F9 - Play/Pause`nF10 - Stop`nF11 - Show/Hide Tooltip
Sleep 2000
;ToolTip

len := Sound_Length(hSound)

Loop
{
   If !playing
      Continue
   If(Sound_Pos(hSound) = Sound_Length(hSound))
      Break
   If tooltip
      ToolTip % Tohhmmss(Sound_Pos(hSound))
}

If(NOT Sound_Close(hSound))
   MsgBox Error closing sound file

ExitApp

Tohhmmss(milli){
   min  := Floor(milli / (1000 * 60))
   hour := Floor(milli / (1000 * 3600))
   sec  := Floor(Floor(milli/1000) - (min * 60))
   Return hour ":" min ":" sec
}

;F9::
status := Sound_Status(hSound)
If(status = "stopped" OR status = "paused")
{
   If status = stopped
      Sound_Play(hSound)
   Else
      Sound_Resume(hSound)
   playing = 1
}Else{
   Sound_Pause(hSound)
   playing = 0
   ToolTip
}
Return

F10::
Sound_Stop(hSound)
playing = 0
ToolTip
Return

F11::
tooltip := not tooltip
If !tooltip
   ToolTip
Return

F12::
Sound_Seek(SoundHandle, Hour, Min, Sec)
   milli := 3330
Return
/*
   r := Sound_SendString("set time format milliseconds", 1)
   If r
      Return 0
   milli += Sec  * 1000
   milli += Min  * 1000 * 60
   milli += Hour * 1000 * 60 * 60
   r := Sound_SendString("seek " SoundHandle " to " milli)
   Return NOT r
*/

Thanks in advance and sorry 4 mistakes
:oops:

vlcek
  • Members
  • 341 posts
  • Last active: Apr 03 2014 01:27 PM
  • Joined: 19 Feb 2007
Helo. I have problems with resume sound

;
; AutoHotkey Version: 1.0.47.00
; Language:       Anyone
; Author:         Fincs <[email protected]>
;
; Script Function:
;   Functions to handle multimedia files
;

;===============================================================================
;
; Function Name:   Sound_Open
; Description::    Opens a sound file for use with other sound functions
; Parameter(s):    File - The sound file
;                  Alias [optional] - A name such as sound1, if you do not
;                                     specify one it is automatically generated
; Return Value(s): The sound handle or a 0 to indicate failure
; ErrorLevel value:   0 - No Error
;                   1 - Open failed
;                   2 - File doesn't exist
;
;===============================================================================

Sound_Open(File, Alias=""){
   Static SoundNumber = 0
   IfNotExist, %File%
   {
      ErrorLevel = 2
      Return 0
   }
   If Alias =
   {
      SoundNumber ++
      Alias = AutoHotkey%SoundNumber%
   }
   Loop, %File%
      File_Short = %A_LoopFileShortPath%
   r := Sound_SendString("open " File_Short " alias " Alias)
   If r
   {
      ErrorLevel = 1
      Return 0
   }Else{
      ErrorLevel = 0
      Return %Alias%
   }
}

;===============================================================================
;
; Function Name:   Sound_Close
; Description::    Closes a sound
; Parameter(s):    SoundHandle - Sound handle returned by Sound_Open
; Return Value(s): 1 - Success, 0 - Failure
;
;===============================================================================

Sound_Close(SoundHandle){
   r := Sound_SendString("close " SoundHandle)
   Return NOT r
}

;===============================================================================
;
; Function Name:   Sound_Play
; Description::    Plays a sound from the current position (beginning is the default)
; Parameter(s):    SoundHandle - Sound handle returned by Sound_Open
;               Wait - If set to 1 the script will wait for the sound to finish before continuing
;                   - If set to 0 the script will continue while the sound is playing
; Return Value(s): 1 - Success, 0 - Failure
;
;===============================================================================

Sound_Play(SoundHandle, Wait=0){
   If(Wait <> 0 AND Wait <> 1)
      Return 0
   If Wait
      r := Sound_SendString("play " SoundHandle " wait")
   Else
      r := Sound_SendString("play " SoundHandle)
   Return NOT r
}

;===============================================================================
;
; Function Name: Sound_Stop
; Description::    Stops the sound
; Parameter(s):    SoundHandle - Sound handle returned by Sound_Open
; Return Value(s): 1 - Success, 0 - Failure
;
;===============================================================================

Sound_Stop(SoundHandle){
   r := Sound_SendString("seek " SoundHandle " to start")
   r2 := Sound_SendString("stop " SoundHandle)
   If(r AND r2)
   {
      Return 0
   }Else{
      Return 1
   }
}

;===============================================================================
;
; Function Name:   Sound_Pause
; Description::    Pauses the sound
; Parameter(s):    SoundHandle - Sound handle returned by Sound_Open
; Return Value(s): 1 - Success, 0  - Failure
;
;===============================================================================

Sound_Pause(SoundHandle){
   r := Sound_SendString("pause " SoundHandle)
   Return NOT r
}

;===============================================================================
;
; Function Name:   Sound_Resume
; Description::    Resumes the sound after being paused
; Parameter(s):    SoundHandle - Sound handle returned by Sound_Open
; Return Value(s): 1 - Success, 0  - Failure
;
;===============================================================================

Sound_Resume(SoundHandle){
   r := Sound_SendString("resume " SoundHandle)
   Return NOT r
}

;===============================================================================
;
; Function Name:   Sound_Length
; Description::    Returns the length of the sound
; Parameter(s):    SoundHandle - Sound handle returned by Sound_Open
; Return Value(s): Length of the sound - Success
;
;===============================================================================

Sound_Length(SoundHandle){
   r := Sound_SendString("set time format miliseconds", 1)
   If r
      Return 0
   r := Sound_SendString("status " SoundHandle " length", 1, 1)
   Return %r%
}

;===============================================================================
;
; Function Name:   Sound_Seek
; Description::    Seeks the sound to a specified time
; Parameter(s):    SoundHandle - Sound handle returned by Sound_Open
;                  Hour, Min, Sec - Time to seek to
; Return Value(s): 1 - Success, 0 - Failure,
;
;===============================================================================

Sound_Seek(SoundHandle, Hour, Min, Sec){
   milli := 0
   r := Sound_SendString("set time format milliseconds", 1)
   If r
      Return 0
   milli += Sec  * 1000
   milli += Min  * 1000 * 60
   milli += Hour * 1000 * 60 * 60
   r := Sound_SendString("seek " SoundHandle " to " milli)
   Return NOT r
}

;===============================================================================
;
; Function Name:   Sound_Status
; Description::    All devices can return the "not ready", "paused", "playing", and "stopped" values.
;               Some devices can return the additional "open", "parked", "recording", and "seeking" values.(MSDN)
; Parameter(s):    SoundHandle - Sound handle returned by Sound_Open
; Return Value(s): Sound Status
;
;===============================================================================

Sound_Status(SoundHandle){
   Return Sound_SendString("status " SoundHandle " mode", 1, 1)
}

;===============================================================================
;
; Function Name:   Sound_Pos
; Description::    Returns the current position of the song
; Parameter(s):    SoundHandle - Sound handle returned by Sound_Open
; Return Value(s): Current Position - Success, 0 - Failure
;
;===============================================================================

Sound_Pos(SoundHandle){
   r := Sound_SendString("set time format miliseconds", 1)
   If r
      Return 0
   r := Sound_SendString("status " SoundHandle " position", 1, 1)
   Return %r%
}

;===============================================================================

Sound_SendString(string, UseSend=0, ReturnTemp=0){
   If UseSend
   {
      VarSetCapacity(stat1, 32, 32)
      DllCall("winmm.dll\mciSendStringA", "UInt", &string, "UInt", &stat1, "Int", 32, "Int", 0)
   }Else{
      DllCall("winmm.dll\mciExecute", "str", string)
   }
   If(UseSend And ReturnTemp)
      Return stat1
   Else
      Return %ErrorLevel%
}


ScriptName = Free blind player

Version=1.0
menu, tray, tip,Free blind player...
menu,tray,nostandard
menu,tray,add,go to player window,show
menu,tray,add,stop,mh4
menu,tray,add,exit,mh2
menu,tray,default,go to player window

Gui, -Sysmenu
#SingleInstance force
ifnotexist, %A_ScriptDir%\keys.txt
fileinstall, keys.txt, %A_ScriptDir%\keys.txt, 1
show:
Gui, Show, x200 y200 w200 h200,%scriptname%

menu,snd,add,vyhledat všechny mp3 soubory na disku c...,search
menu,snd,add,zvuky ve windows ...,sndwin

menu volby,add,zesílit o 5 procent,zes
menu, volby,add,zeslabit o 5 procent,zesl
Menu volby,add,přidat basy,ba
Menu volby, add,ubrat basy,baa

Menu,přehrávač1,add,složku...,ps
Menu,url,add,url,url
Menu,url,add,soubory,urlfiles
Menu,url,add,smazat soubory,urldelete
;
Menu,přehrávač1,add,URL,:url
menu,přehrávač1,Add,soubor...,MH1
Menu,Přehrávač1,add,znovu,snd1
Menu,Přehrávač1,Add,Stop,MH4
Menu,soubor,add,reset programu,res
      Menu,soubor, Add
menu,soubor,Add,&konec,MH2
menu,okno,add,minimalizovat,min
Menu,obli,add,oblíbené...,obl2
Menu,obli,add,Přidat...,obl
Menu,obli,add,smazat,vyp
Menu,nastroje,add,velikost souboru...,vel
Menu,nastroje,add,hodiny...,hod
Menu,Nápověda3,Add,O aplikaci...,mh3
menu,nápověda3,add,Klávesové skratky,keys
menu,mymenu, add
menu,mymenu,add,Soubor,:soubor
menu,mymenu,add,zobrazit,:okno
menu,mymenu,Add,přehrát,:přehrávač1
Menu,mymenu,add,oblíbené,:obli
Menu,mymenu,add,nástroje,:nastroje
  Menu, mymenu, Add, nastavení,:volby
menu,mymenu,add,doplňky,:snd
menu,myMenu,Add,nápověda,:nápověda3
menu,mymenu, add
return

sounds:
ps:

$^f::
IfWinNotActive, %ScriptName%
  {
Send, ^f
   Return
}
SoundPlay, Nonexistent.
FileSelectFolder SelectedFolder, , 3, (*.wav)
If SelectedFolder =
return
Else
{
   Loop %SelectedFolder%\*.*,0,1
      Soundplay %A_LoopFileFullPath%, WAIT
return
}
Return

MH1:
$^o::
IfWinNotActive, %ScriptName%
  {
Send, ^o
    Return
}
SoundPlay, Nonexistent.
FileSelectFile, SelectedFile, 2, , Otevři zvukový soubor, zvuky (*.WAV; *.mp3; *.rmi; *.mp1; *.mid; *.dat; *.m3u; *.amr; *.wmv; *.OGG; *.flac; *.wma; *.mp2; *.MPG)
if SelectedFile =
return
    else
{
sleep, 2000
hSound := Sound_Open(selectedfile, "myfile")
      Sound_Play(hSound)
   playing = 1
}

Return
space::
status := Sound_Status(hSound)
If(status = "paused")
{
      Sound_Resume(hSound)
   playing = 1
}
Else

   Sound_Pause(hSound)
   playing = 0

Return
MH4:
$^s::
ifwinnotactive, %scriptname%
{
send, ^s
return
}

soundplay, nonexistent
tooltip,přehrávání,zastaveno,2
Return
SND1:
#include %A_ScriptDir%\inc\snd1.ahk
return
ba:
SoundSet, +5, Master, bass  
; Increase bass level by 20%.
if ErrorLevel
    MsgBox, 16, Chyba., Je mi líto,, ale váš zvukový ovladač nepodporuje změnu nastavení basů.
Return
baa:
SoundSet, -5, Master, bass  
; Increase bass level by 20%.
if ErrorLevel
    MsgBox, 16, chyba., Je mi líto,, ale váš zvukový ovladač nepodporuje změnu nastavení basů.
Return
;-= Quit =-
MH2:
exitApp
Return
^!r::Reload  
; Assign Ctrl-Alt-R as a hotkey to restart the script.
Return
hod:
FormatTime, TimeString,, LongDate
FormatTime, TimeString, T12, Time
MsgBox, 64, čas., Je  %TimeString%.
Return
vel:
   Unit = M ; k
   FileGetSize, Size, %selectedfile%, %Unit%   
IFnotexist %SelectedFile%
MSGBOX, 64, info., Nevybrali jste žádný soubor.
else
MsgBox, 64, Info, Velikost přehrávaného souboru je: %Size% (%Unit%b)
Return
obl:
#include %A_ScriptDir%\inc\fav1.ahk
return
vyp:
#include %A_ScriptDir%\inc\del1.ahk
Return
res:
reload
Return
obl2:
#include %A_ScriptDir%\inc\fav2.ahk
return
URL:
#include %A_ScriptDir%\inc\url.ahk
return
zes:
SoundSet, +5            ; Win+Cursor up: master volume up
return
zesl:
SoundSet, -5            master volume up
Return
urlfiles:
Gui, 8:+owner1  
; Make the main window (Gui #1) the owner of the "about box" (Gui #2).
Gui +Disabled  
; Disable main window.
suspend
Gui, 8:-sysmenu
Loop %A_ScriptDir%\url\*.*
{
   mp3List .= A_LoopFilename . "|"
}
StringTrimRight mp3List, mp3List, 1
Gui 8:Add, Listbox, w200 h300 vmp3Choice, %mp3List%
Gui 8:Add, Button, w100 xm+50 g8Play Default, hrej
Gui, 8:Add, button, w1, Návrat
Gui, 8:Show, w65, soubory z internetu
Return
8play:
   Gui, 8:Submit, NoHide
   If (mp3Choice = "")
   {
      MsgBox 16, chyba, nebyl vybrán žádný soubor!
   }
   Else
{
SoundPlay, %A_ScriptDir%\url\%mp3Choice%
   }
return
8buttonnávrat:
8GuiClose:
8GuiEscape:
Gui, 1:-Disabled
Gui Destroy
suspend
Return
urldelete:
#include %A_ScriptDir%\inc\urldel.ahk
Return
#include %A_ScriptDir%\files.ahk
return
GuicontextMenu:           ; Right click popup menu
   Menu mymenu, Show    ; Show context menu
return
int:
run, http://dsprogramy.ic.cz
Return

search:
Msgbox, 52, info., Chystáte se vyhledat všechny soubory na disku c. Pokud máte větší kapacitu disku bude program vyhledávat déle. Pokračovat?
IfMsgbox, yes
{
Gui, 4:+owner1  
; Make the main window (Gui #1) the owner of the "about box" (Gui #2).
Gui +Disabled  
; Disable main window.
suspend
Gui, 4:-sysmenu
AllDrives=C ; or just the drives to search.
FileList =  ; Initialize to be blank.
Loop, Parse, AllDrives
{
   Drive := A_LoopField
   Loop, %Drive%:\*.mp3,0,1
   {
      FileList = %FileList%%A_LoopFileName%|
      FileListWithPath = %FileListWithPath%%A_LoopFileFullPath%|
      }
   }
StringTrimRight, FileList, FileList, 1
StringTrimRight, FileListWithPath, FileListWithPath, 1
StringSplit, SongNum, FileListWithPath, |
Gui, 4:Add, ListBox, vselectedsong altsubmit, %FileList%
Gui 4:Add, Button, w100 vbut xm+50 gPlay Default, hrej
Gui, 4:Show,, My MP3's
Return
play:
Gui, 4:Submit, Nohide
SoundPlay, % (SongNum%SelectedSong%)
return
4GuiClose:
4GuiEscape:
Gui, 1:-Disabled  
Gui Destroy  
soundplay, nonexistent
suspend
Return
}
IfMsgbox, no
Return
return

sndwin:
gui, 6:-sysmenu
suspend
Loop %windir%\media\*.wav
{
   mp3List .= A_LoopFileName . "|"
}
StringTrimRight mp3List, mp3List, 1
Gui 6:Add, ListBox, w200 h300 vmp3Choice, %mp3List%
Gui 6:Add, Button, w100 xm+50 g6Go Default, hrej
Gui 6:Show,, Zvuky ve windows
Return
6Go:
Gui 6:Submit, NoHide
   If (mp3Choice = "")
   {
MsgBox 16, chyba, Není vybrán zvuk.
   }
   Else
   {
soundplay, %A_Windir%\media\%mp3Choice%
   }
Return
6GuiClose:
6GuiEscape:
Gui, 1:-Disabled  
Gui Destroy  
soundplay, nonexistent
suspend
Return
min:
$#h::
ifwinnotactive, %scriptname%
{
send, #h
return
}
gui, hide
Return
plnh:
soundset, 100
soundset, 100
Return
$^up::
ifwinnotactive, %ScriptName%
{
send, {^up}
return
}
SoundSet +10  
return
$^down::
ifwinnotactive, %ScriptName%
{
send, {^down}
return
}

SoundSet -10  
Return
mh3:
Msgbox, 64, O programu., Free blind Player verze %version%. Vytvořil Vlček Pavel.
Return

keys:
$^k::
ifwinnotactive, %scriptname%
{
send, ^k
return
}
run, %A_ScriptDir%\keys.txt
Return
Problem is in mh3 and space.
Thanks.

Washboard
  • Guests
  • Last active:
  • Joined: --
Why in Sound_Open, success = 0, and in the others functions success = 1 (= True, too...)
Wouldn't be better to have 1 = success, -1 = open failed ans -2 File doesn't exist ?