AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Sound.ahk - Sound functions for use with AutoHotkey
Goto page 1, 2  Next
 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions
View previous topic :: View next topic  
Author Message
fincs



Joined: 05 May 2007
Posts: 82
Location: Seville, Spain

PostPosted: Sun Jul 01, 2007 10:54 am    Post subject: Sound.ahk - Sound functions for use with AutoHotkey Reply with quote

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:

Code:
;
; AutoHotkey Version: 1.0.47.00
; Language:       Anyone
; Author:         Fincs <fernandoincs@hotmail.com>
;
; 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:

Code:
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!
_________________
Sorry my poor English
Fincs

MsgBox % RegExReplace("Fernando Incs.", "^(\w).*\s+I(\w{3})\w*\.$", "$1i$2")
Back to top
View user's profile Send private message
fincs



Joined: 05 May 2007
Posts: 82
Location: Seville, Spain

PostPosted: Sun Jul 01, 2007 4:01 pm    Post subject: Reply with quote

28 views and no replies?
_________________
Sorry my poor English
Fincs

MsgBox % RegExReplace("Fernando Incs.", "^(\w).*\s+I(\w{3})\w*\.$", "$1i$2")
Back to top
View user's profile Send private message
ManaUser



Joined: 24 May 2007
Posts: 900

PostPosted: Sun Jul 01, 2007 5:09 pm    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message
jballi



Joined: 01 Oct 2005
Posts: 319
Location: Texas, USA

PostPosted: Mon Jul 02, 2007 12:54 am    Post subject: Reply with quote

ManaUser wrote:
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. Smile
Back to top
View user's profile Send private message
Sean



Joined: 12 Feb 2007
Posts: 1281

PostPosted: Mon Jul 02, 2007 2:21 am    Post subject: Reply with quote

I think you may utilize MCI's Callback via OnMessage. It's just a small suggestion.

Code:
#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 type mpegvideo style overlapped shareable", "Uint", 0, "Uint", 0, "Uint", 0)
DllCall("winmm\mciSendStringA", "str", "play mmedia notify", "Uint", 0, "Uint", 0, "Uint", hAHK)

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
}
Back to top
View user's profile Send private message
jballi



Joined: 01 Oct 2005
Posts: 319
Location: Texas, USA

PostPosted: Tue Jul 03, 2007 8:00 am    Post subject: Reply with quote

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.

  • 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.

  • 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.

  • 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:
Code:
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...
Back to top
View user's profile Send private message
fincs



Joined: 05 May 2007
Posts: 82
Location: Seville, Spain

PostPosted: Tue Jul 03, 2007 11:44 am    Post subject: Reply with quote

jballi wrote:
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_*".
jballi wrote:
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.
jballi wrote:
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:
Code:
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)

jballi wrote:
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".
jballi wrote:
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:
Code:
Sound_Play(SoundHandle [, Wait=0, OnStopFunction=""]) ; When the file is at EOF OnStopFunction is called.

_________________
Sorry my poor English
Fincs

MsgBox % RegExReplace("Fernando Incs.", "^(\w).*\s+I(\w{3})\w*\.$", "$1i$2")
Back to top
View user's profile Send private message
Trubbleguy



Joined: 20 Jan 2007
Posts: 73
Location: Melbourne

PostPosted: Thu Sep 27, 2007 3:25 am    Post subject: found some stuff Reply with quote

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.
Code:
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
Back to top
View user's profile Send private message Send e-mail Visit poster's website MSN Messenger
vlcek



Joined: 19 Feb 2007
Posts: 321
Location: Czech Republic

PostPosted: Sat Sep 29, 2007 7:53 am    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message
jojo
Guest





PostPosted: Sat Sep 29, 2007 10:21 am    Post subject: Reply with quote

fincs wrote:

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.....
Back to top
jojo
Guest





PostPosted: Sat Sep 29, 2007 10:24 am    Post subject: Reply with quote

@vlek:
you have to include the main file in the player, or copy both into one file
Back to top
playerA
Guest





PostPosted: Fri Oct 05, 2007 10:58 am    Post subject: How to jump for example at 1'33" ? Reply with quote

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:
Code:
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.
Smile
Back to top
playerA
Guest





PostPosted: Fri Oct 05, 2007 4:51 pm    Post subject: Apologize Reply with quote

My apologize to Fincs
Quote:
Hi Jojo, thank you 4 the script

(I mean Fincs)
Embarassed Embarassed Embarassed

Thanks Jojo however
Quote:
you have to include the main file in the player, or copy both into one file

this resolve my first prob (same of vlcek) Very Happy
But I'm looking 4:
how to jump for example at 1'33" of a mp3?
I try:
Code:

;
; AutoHotkey Version: 1.0.47.00
; Language:       Anyone
; Author:         Fincs <fernandoincs@hotmail.com>
;
; 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
Embarassed
Back to top
vlcek



Joined: 19 Feb 2007
Posts: 321
Location: Czech Republic

PostPosted: Fri Oct 12, 2007 4:03 pm    Post subject: Reply with quote

Helo. I have problems with resume sound

Code:

;
; AutoHotkey Version: 1.0.47.00
; Language:       Anyone
; Author:         Fincs <fernandoincs@hotmail.com>
;
; 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.
Back to top
View user's profile Send private message
Washboard
Guest





PostPosted: Fri Oct 12, 2007 4:48 pm    Post subject: Reply with quote

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 ?
Back to top
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group