Jump to content
Sign in to follow this  
gil900

Need help with PlaySound API

Recommended Posts

gil900

Hello,

I am using the function _ResourcePlaySound (from Resources UDF) to play sound file from the exe.

 

in my case i start to play some sound and my code(loop) must to keep running.

so i can't use _ResourcePlaySound with the wait parameter to know when the sound stopped because the entire script will sleep.

I need a way how to know if a sound still playing.

I asked Zedna (The author of Resources UDF) and he said he don't know how to do it and it is related to PlaySound API.

So how can i use PlaySound API to know if sound still playing?

 

Thanks for helpers!

Share this post


Link to post
Share on other sites
Melba23

gil900,

 

in my case i start to play some sound and my code(loop) must to keep running

You need to set the $Flag parameter of _ResourcePlaySound to $SND_ASYNC (0x1) to let the script continue. I see no way of detecting when the sound has stopped - so I suggest you do one of 2 things:

 

- 1. Keep the sound looping until you want to end it by also setting the $Flag parameter of _ResourcePlaySound to $SND_LOOP (0x8). Now the sound file will repeat indefinitely until the script ends or you stop the resource from playing by using:

DllCall("winmm.dll", "int", "PlaySound", "str", NULL, "hwnd", 0, "int", 0)

- 2. Use some form of timer to tell you whether the resource has finished playing - you know the resource you are playing so you also know its length.

 

I hope that helps. :)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites
gil900

You know the length of the sound, you know when it's started.

if start + time passed is less than start + length then sound is still playing.

I thought about this method.

But what if the sound had not started at the right time (because the computer is screwed up or something like that)?

You do not know what happened about that ..

Also, I do not know what the length of the sound. I'll have to put that data to the array ..

I prefer a more stable method.

Share this post


Link to post
Share on other sites
Melba23

gil900,

Did you read my post above? :huh:

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites
gil900

gil900,

Did you read my post above? :huh:

M23

I read it.

in 2 you suggests the same method that JohnOne suggested.

in 1 you suggests something that i don't want - that the sound will play from the beginning every time until the script is closed.

 
I understand you wrong?

Share this post


Link to post
Share on other sites
Melba23

gil900,

I understand you wrong?

Yes, you can stop the playing sound at any time by running the code I posted. Try it and see. :)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites
gil900

gil900,

Yes, you can stop the playing sound at any time by running the code I posted. Try it and see. :)

M23

yes, you also worte:

..or you stop the resource from playing by using

 

EDIT:

But I do not see how it helps me.

I need to know if the sound is playing or not ..

the sound will stop anyway after it ends so i don't need to stop the sound..

i only need to know if the sound still playing or not.

EDIT2:

JohnOne, Suggested that:

 

You know the length of the sound, you know when it's started.

if start + time passed is less than start + length then sound is still playing.

 

The problem is that it does not work properly if the sound had not started at the right time (because of slow computer)

So my question is what are the chances of that happening - the case that the computer is slow and the sound starts after a delay of a few seconds..

If this case have very low probability then that method is fine

Edited by gil900

Share this post


Link to post
Share on other sites
Melba23

gil900,

The earlier post you made in the Resources thread implied that you wanted to end the sound playing when your "other code" terminated, which is what I was trying to solve for you. However, if all you want is for the sound to play fully just the once then I see no alternative to the timer idea that both JohnOne and I suggested independently. :(

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites
gil900

gil900,

The earlier post you made in the Resources thread implied that you wanted to end the sound playing when your "other code" terminated, which is what I was trying to solve for you. However, if all you want is for the sound to play fully just the once then I see no alternative to the timer idea that both JohnOne and I suggested independently. :(

M23

ok,

Please reread the previous post after "EDIT2".

I asked a new question there

Share this post


Link to post
Share on other sites
Melba23

gil900,

I think I have found the answer. You need to attempt to replay the same sound using the $SND_NOSTOP parameter - this will not play if the current sound is still playing. :)

This works for me: :thumbsup:

#AutoIt3Wrapper_Res_File_Add=sound.wav, sound, SoundTest

#include <Resources.au3>

; Play the sound 
_ResourcePlaySound("SoundTest", $SND_ASYNC)
; Prove we have returned
MsgBox(0, "Hi", "Sound should be playing")
; Now loop
While 1
    ; Try to replay the sound
    $vRet = _ResourcePlaySound("SoundTest", BitOr($SND_RESOURCE, $SND_ASYNC, $SND_NOSTOP))
    ; If there is a return, the sound is no longer playing
    If $vRet Then
        ; Stop the sound from playing again
        DllCall("winmm.dll", "int", "PlaySound", "str", NULL, "hwnd", 0, "int", 0)
        ; Announce the end
        MsgBox(0, "Hi", "Stopped")
        ExitLoop
    EndIf
WEnd
M23

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites
gil900

gil900,

I think I have found the answer. You need to attempt to replay the same sound using the $SND_NOSTOP parameter - this will not play if the current sound is still playing. :)

This works for me: :thumbsup:

#AutoIt3Wrapper_Res_File_Add=sound.wav, sound, SoundTest

#include <Resources.au3>

; Play the sound 
_ResourcePlaySound("SoundTest", $SND_ASYNC)
; Prove we have returned
MsgBox(0, "Hi", "Sound should be playing")
; Now loop
While 1
    ; Try to replay the sound
    $vRet = _ResourcePlaySound("SoundTest", BitOr($SND_RESOURCE, $SND_ASYNC, $SND_NOSTOP))
    ; If there is a return, the sound is no longer playing
    If $vRet Then
        ; Stop the sound from playing again
        DllCall("winmm.dll", "int", "PlaySound", "str", NULL, "hwnd", 0, "int", 0)
        ; Announce the end
        MsgBox(0, "Hi", "Stopped")
        ExitLoop
    EndIf
WEnd
M23

 

Thank you.

I'll check it later.

If I see that it works then I'll write a function for it which based on this method and I will share the function with you.

Share this post


Link to post
Share on other sites
gil900

gil900,

I think I have found the answer. You need to attempt to replay the same sound using the $SND_NOSTOP parameter - this will not play if the current sound is still playing. :)

This works for me: :thumbsup:

#AutoIt3Wrapper_Res_File_Add=sound.wav, sound, SoundTest

#include <Resources.au3>

; Play the sound 
_ResourcePlaySound("SoundTest", $SND_ASYNC)
; Prove we have returned
MsgBox(0, "Hi", "Sound should be playing")
; Now loop
While 1
    ; Try to replay the sound
    $vRet = _ResourcePlaySound("SoundTest", BitOr($SND_RESOURCE, $SND_ASYNC, $SND_NOSTOP))
    ; If there is a return, the sound is no longer playing
    If $vRet Then
        ; Stop the sound from playing again
        DllCall("winmm.dll", "int", "PlaySound", "str", NULL, "hwnd", 0, "int", 0)
        ; Announce the end
        MsgBox(0, "Hi", "Stopped")
        ExitLoop
    EndIf
WEnd
M23

 

Thank you!

I checked it and it works!

I created new wav named "Silent.wav" and with a length of 0.001 seconds and weighs 130 bytes.

so that way i don't need to stop the sound !

I do not really like this way. But at the end what matters is that it works and I'd rather not to waste time for finding more normal way.

Share this post


Link to post
Share on other sites
Melba23

gil900,

Great news! :D

 

I created new wav named "Silent.wav" and with a length of 0.001 seconds and weighs 130 bytes. so that way i don't need to stop the sound !

I did the same thing as I found you could get a short "blip" of the restarted wav before it was stopped. I was going to suggest it if you found the same problem. ;)

 

I do not really like this way. But at the end what matters is that it works and I'd rather not to waste time for finding more normal way

I do not think there is a "more normal" way. I searched for quite a long time to find this solution and I do not believe there is another one when you want to play the file in its entirety from the resource table. :(

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites
gil900

I do not think there is a "more normal" way. I searched for quite a long time to find this solution and I do not believe there is another one when you want to play the file in its entirety from the resource table. :(

Now I'm starting to remember that I thought in the past about "more normal" way.

 
The method I thought about is to call _ResourcePlaySound with $Flag = 0 inside a separate "thread"(but the same exe and process).
 
This way you can wait for the sound to stop playing without stopping script.
if you can define variables in the separate "thread" and access them from the main thread then the idea is:
 
In the separate "thread":
$IsPlaying = True
_ResourcePlaySound("Sound")
$IsPlaying = False
 
In the main "thread"

While 1
    If $IsPlaying = True Then
        ToolTip("Sound playing")
    Else
        ToolTip("Sound NOT playing")
    EndIf
    Sleep(10)
WEnd

 

I know that autoit not supports multithread.
but i sow that people were able to do something very similar. So if this is true then it is probably enough to be used as "thread"
 

I do not know how to implement this method. I'm not sure it's possible.

I gave a new direction .. If you think it's possible so please try to exercise it or give me a start how to create that separate "thread"

Edited by gil900

Share this post


Link to post
Share on other sites
Melba23

gil900,

 

I gave a new direction

Which I for one am not following. About time you did some research and coding, methinks. ;)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites
gil900

gil900,

 

Which I for one am not following. About time you did some research and coding, methinks. ;)

M23

If I understood what you said, you probably do not know how much research and coding i did..

I just saw that some people were able to create separate "thread". But I have not tried all solutions.

 

EDIT

According to the research I've done (which is not much .. I read people's comments), I realized that this direction is not worth it because it may cause the script to crash.

But I also want to hear your research and what you say about it.

EndEdit

I need you to also answer the question:

Is it possible to create a separate "thread" with the following requirements:

* Ability to define and re-define the variables in the separate "thread"

* Ability to access the variables in the separate "thread"

* That separate "thread" will work on the same exe and process

If you know someone did it, so please give me a link to it.

I also want to use it for InetRead (run InetRead in separate "thread")

 
Thank you
Edited by gil900

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×