Jump to content
Sign in to follow this  
Gif

Sound Help

Recommended Posts

Gif

Case $msg = $btn_play
            $lbl_showsongfile = GUICtrlCreateLabel(GUICtrlRead($input_open), 160, 110, 270, 40)
            $sound = _SoundOpen(GUICtrlRead($input_open))
                 If @error = 2 Then
                    MsgBox(0, "Error", "The file does not exist")
                     ElseIf @error = 3 Then
                        MsgBox(0, "Error", "The alias was invalid")
                          ElseIf @extended <> 0 Then
                                    $extended = @extended
                                      $errorstring = DllCall("winmm.dll","int","mciGetErrorStringA","str",$extended,"str","","int",65534,"hwnd",0)
                                           MsgBox(0, "Error", "The open failed." & @CRLF & "Error Number: " & $extended & @CRLF & "Error Description: " & $errorstring[2] & @CRLF & "Please Note: The sound may still play correctly.")
                                          Else
                                            MsgBox(0, "Success", "The file opened successfully")
                                        EndIf
                                        If _SoundClose($sound) = 1 Then
                                            
                                                    MsgBox(0, "Success", "The sound was closed successfully")
                                                     Else
                                                  MsgBox(0, "Error", "The file was not closed successfully")
                                              EndIf
                                            _SoundPlay($sound, 1)

I use this script and it does not work... WHY? where is the mistake

If I use only sound play instead of _soundopen and _soundplay (..) the sound works fine but i can't use othr functions like pause and stop, or maybe i can?

can you assist me i am a bit confused, how do i combine _soundplay with other functions?

Excuse me for my bad english but i am a foreigner

Edited by c4nm7

Share this post


Link to post
Share on other sites
BrettF

I'm not really seeing where your problem is. Just to make sure, you have to use the parameters in the functions (_SoundPlay ($sound) .etc....) Where are you having trouble with it??

Share this post


Link to post
Share on other sites
Gif

I'm not really seeing where your problem is. Just to make sure, you have to use the parameters in the functions (_SoundPlay ($sound) .etc....) Where are you having trouble with it??

When the music statrts the programm somehow does not function (BASICLY IT Shows me the error Not Responding) and i do not have the ability to press other buttons or do anyhing in the program

Share this post


Link to post
Share on other sites
birdofprey

To my knowledge there's no such thing as

If _SoundClose($sound) = 1 Then
, what are you trying to do there ?

Yes, _SoundClose returns 1 if it closes the sound successfully but, I don't think that the above line would actually close the file... :) And I don't think you actually want to do that, cause you can't play, pause or resume a closed file... Keep that $sound handle alive, and try SoundPause($sound), _SoundResume($sound) -it, while that. When you want to play another file, point $sound to that...

If you really need to know if the file is closed try:

$error = _SoundClose($sound)

If $error = 1 Then...

Correct me if I'm wrong...

Edited by birdofprey

Share this post


Link to post
Share on other sites
Gif

To my knowledge there's no such thing as

If _SoundClose($sound) = 1 Then
, what are you trying to do there ?

Yes, _SoundClose returns 1 if it closes the sound successfully but, I don't think that the above line would actually close the file... :) And I don't think you actually want to do that, cause you can't play, pause or resume a closed file... Keep that $sound handle alive, and try SoundPause($sound), _SoundResume($sound) -it, while that. When you want to play another file, point $sound to that...

If you really need to know if the file is closed try:

$error = _SoundClose($sound)

If $error = 1 Then...

Correct me if I'm wrong...

sorry but i am a bit confused i don't know what to do , shall i use this order?

_SoundOpen

_SoundClose

_soundplay

What is the purpose of soundclose, can't understand y when the music starts there is no respnse from my programm ...

Share this post


Link to post
Share on other sites
birdofprey

sorry but i am a bit confused i don't know what to do , shall i use this order?

_SoundOpen

_SoundClose

_soundplay

What is the purpose of soundclose, can't understand y when the music starts there is no respnse from my programm ...

I don't know why your program stops responding. But I can answer your other questions.

_SoundOpen returns a so called handle. That's the variable you work with.

$sound = _SoundOpen($file)oÝ÷ ØÚ0¶­¶´ß«(ºwoj¸nW¢±§-¹©eÊ"اÊ&{^­ì¨¹Æ§vax,Â+aÖ®¶­seõ6÷VæEÆb33c·6÷VæB·Æ6÷Væ@¥6ÆVW#·vVâFRGvò6V6öæG2&R÷fW ¥õ6÷VæEW6Rb33c·6÷VæB²W6RFR6÷Væ@¥6ÆVW#²¶VWBW6VBf÷"æ÷FW""6V6öæG0¥õ6÷VæE&W7VÖRb33c·6÷VæB·&W7VÖRFR6÷Væ@ £¶æBb÷RƶS ¥õ6÷VæE7F÷b33c·6÷VæB·7F÷FB6÷VæBoÝ÷ ØÚ0¶­Ê¬¶iyÛayø¥y©Ý÷hmÁ©í¶ek&«¢éÝÂ+a¶­²§u©òÞjz-êì¢éÝ{h¶´ß«(ºwoj¸nWÉ«­¢+ØÀÌØíͽչô}M½Õ¹=Á¸ ÀÌØí¹½Ñ¡É¥±¤°(í¹Á±äÁÕÍÉÍÕµÍѽÀÑ¡¥ÌÀÌØí¹½Ñ¡È¥±¸¸¸(í½È©ÕÍÐ)}M½Õ¹
±½Í ÀÌØíͽչ¤¸(
Edited by birdofprey

Share this post


Link to post
Share on other sites
birdofprey

_SoundOpen

_SoundClose

_soundplay

Also notice that these are actually:

_SoundOpen($something)
_SoundClose($something)
_soundplay($something)

and while for _SoundOpen() that something is a file, for _SoundPlay that is the $sound ( as returned by _SoundOpen() )

Edited by birdofprey

Share this post


Link to post
Share on other sites
jennico

hi, i would like to take part in this discussion.

i found out that _soundplay() and _soundstop() work very fine without _soundopen() and _soundclose().

you can just use the filename instead of the handle, in any case it works. it also works with .mid files. you only have to take care that you use filegetshortname() because the function cannot handle whitespaces.

but i would like to know: is it better to open and close a sound before playing ? is it necessary ? what is the difference ? i ask because i now encounter problems with my soundcard (does not react from time to time, though showing no error ). but maybe this has nothing to do with the sound functions, i hope ?

cheers j.

edit: another thing i found out: the _soundstop() udf consists of two functions: the first one is _soundseek() and the second one is _soundstop(). i altered the _soundstop() udf so it works without the _soundseek() call and it seems not to be necessary. _soundstop() with _soundseek() stops the sound perfectly. so what is the sense of _soundseek() ?

the sound.au3 and i marked the _soundseek() within _soundstop()

;===============================================================================
;
; Function Name:   _SoundOpen
; Description::    Opens a sound file for use with other _Sound functions
; Parameter(s):    $hFile - The sound file, $sAlias[optianal] - a name such as sound1,
;                  if you do not specify one it is randomly generated
; Requirement(s):  AutoIt 3.2 ++
; Return Value(s): string(the sound id) - Success, 0 - Failure
;                  @extended <> 0 - open failed, @error = 2 - File doesn't exist,
;                  @error = 3 - alias contains whitespace
; Author(s):       RazerM
;
;===============================================================================
;
Func _SoundOpen($hFile, $sAlias = "")
    ;Declare variables
    Local $sSnd_id, $iCurrentPos, $iRet, $asAlias
    ;check for file
    If Not FileExists($hFile) Then Return SetError(2, 0, 0)
    ;search for whitespace by character
    $asAlias = StringSplit($sAlias, "")
    For $iCurrentPos = 1 To $asAlias[0]
        If StringIsSpace($asAlias[$iCurrentPos]) Then Return SetError(3, 0, 0)
    Next
    ;create random alias if one is not supplied
    If $sAlias = "" Then
        $sSnd_id = RandomStr(10)
    Else
        $sSnd_id = $sAlias
    EndIf
    ;open file
    $iRet = mciSendString("open " & FileGetShortName($hFile) & " alias " & FileGetShortName($sSnd_id))
    Return SetError(0, $iRet, $sSnd_id)
EndFunc   ;==>_SoundOpen

;===============================================================================
;
; Function Name:   _SoundClose
; Description::    Closes a sound
; Parameter(s):    $sSnd_id - Sound ID returned by _SoundOpen
; Requirement(s):  AutoIt 3.2 ++
; Return Value(s): 1 - Success, 0 and @error = 1 - Failure
; Author(s):       RazerM
;
;===============================================================================
;
Func _SoundClose($sSnd_id)
    If mciSendString("close " & FileGetShortName($sSnd_id)) = 0 Then
        Return 1
    Else
        Return SetError(1, 0, 0)
    EndIf
EndFunc   ;==>_SoundClose

;===============================================================================
;
; Function Name:   _SoundPlay
; Description::    Plays a sound from the current position (beginning is the default)
; Parameter(s):    $sSnd_id - Sound ID returned by _SoundOpen or sound file
;                  $fWait - 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
; Requirement(s):  AutoIt 3.2 ++
; Return Value(s): 1 - Success, 0 - Failure
;                  @error = 2 - $fWait is invalid, @error = 1 - play failed
; Author(s):       RazerM
;
;===============================================================================
;
Func _SoundPlay($sSnd_id, $fWait = 0)
    ;Declare variables
    Local $iRet
    ;validate $fWait
    If $fWait <> 0 And $fWait <> 1 Then Return SetError(2, 0, 0)
    ;if sound has finished, seek to start
    If _SoundPos($sSnd_id, 2) = _SoundLength($sSnd_id, 2) Then mciSendString("seek " & FileGetShortName($sSnd_id) & " to start")
    ;If $fWait = 1 then pass wait to mci
    If $fWait = 1 Then
        $iRet = mciSendString("play " & FileGetShortName($sSnd_id) & " wait")
    Else
        $iRet = mciSendString("play " & FileGetShortName($sSnd_id))
    EndIf
    ;return
    If $iRet = 0 Then
        Return 1
    Else
        Return SetError(1, 0, 0)
    EndIf
EndFunc   ;==>_SoundPlay

;===============================================================================
;
; Function Name: _SoundStop
; Description::    Stops the sound
; Parameter(s):    $sSnd_id - Sound ID returned by _SoundOpen or sound file
; Requirement(s):  AutoIt 3.2 ++
; Return Value(s): 1 - Success, 0 and @error = 1 - Failure,
; Author(s):       RazerM
;
;===============================================================================
;
Func _SoundStop($sSnd_id)
    ;Declare variables
    Local $iRet, $iRet2
    ;seek to start
    [b][u]$iRet = mciSendString("seek " & FileGetShortName($sSnd_id) & " to start")[/u][/b]
    ;stop
    $iRet2 = mciSendString("stop " & FileGetShortName($sSnd_id))
    ;return
    If $iRet = 0 And $iRet2 = 0 Then
        Return 1
    Else
        Return SetError(1, 0, 0)
    EndIf
EndFunc   ;==>_SoundStop

;===============================================================================
;
; Function Name:   _SoundPause
; Description::    Pauses the sound
; Parameter(s):    $sSnd_id - Sound ID returned by _SoundOpen or sound file
; Requirement(s):  AutoIt 3.2 ++
; Return Value(s): 1 - Success, 0 and @error = 1 - Failure,
; Author(s):       RazerM
;
;===============================================================================
;
Func _SoundPause($sSnd_id)
    ;Declare variables
    Local $iRet
    ;pause sound
    $iRet = mciSendString("pause " & FileGetShortName($sSnd_id))
    ;return
    If $iRet = 0 Then
        Return 1
    Else
        Return SetError(1, 0, 0)
    EndIf
EndFunc   ;==>_SoundPause

;===============================================================================
;
; Function Name:   _SoundResume
; Description::    Resumes the sound after being paused
; Parameter(s):    $sSnd_id - Sound ID returned by _SoundOpen or sound file
; Requirement(s):  AutoIt 3.2 ++
; Return Value(s): 1 - Success, 0 and @error = 1 - Failure,
; Author(s):       RazerM
;
;===============================================================================
;
Func _SoundResume($sSnd_id)
    ;Declare variables
    Local $iRet
    ;resume sound
    $iRet = mciSendString("resume " & FileGetShortName($sSnd_id))
    ;return
    If $iRet = 0 Then
        Return 1
    Else
        Return SetError(1, 0, 0)
    EndIf
EndFunc   ;==>_SoundResume

;===============================================================================
;
; Function Name:   _SoundLength
; Description::    Returns the length of the sound in the format hh:mm:ss
; Parameter(s):    $sSnd_id - Sound ID returned by _SoundOpen or sound file,
;                  $iMode = 1 - hh:mm:ss, $iMode = 2 - milliseconds
; Requirement(s):  AutoIt 3.2 ++
; Return Value(s): Length of the sound - Success, 0 and @error = 1 - $iMode is invalid
; Author(s):       RazerM
;
;===============================================================================
;
Func _SoundLength($sSnd_id, $iMode = 1)
    ;Declare variables
    Local $iSnd_len_ms, $iSnd_len_min, $iSnd_len_hour, $iSnd_len_sec, $sSnd_len_format
    ;validate $iMode
    If $iMode <> 1 And $iMode <> 2 Then Return SetError(1, 0, 0)
    ;tell mci to use time in milliseconds
    mciSendString("set " & FileGetShortName($sSnd_id) & " time format miliseconds")
    ;recieve length of sound
    $iSnd_len_ms = mciSendString("status " & FileGetShortName($sSnd_id) & " length")
    ;assign modified data to variables
    $iSnd_len_min = Int($iSnd_len_ms / 60000)
    $iSnd_len_hour = Int($iSnd_len_min / 60)
    $iSnd_len_sec = Int(Int($iSnd_len_ms / 1000) - ($iSnd_len_min * 60))
    ;assign formatted data to $sSnd_len_format
    $sSnd_len_format = StringFormat("%02i:%02i:%02i", $iSnd_len_hour, $iSnd_len_min, $iSnd_len_sec)
    ;return correct variable
    If $iMode = 1 Then Return $sSnd_len_format
    If $iMode = 2 Then Return $iSnd_len_ms
EndFunc   ;==>_SoundLength

;===============================================================================
;
; Function Name:   _SoundSeek
; Description::    Seeks the sound to a specified time
; Parameter(s):    $sSnd_id - Sound ID returned by _SoundOpen (must NOT be a file), $iHour, $iMin, $iSec
; Requirement(s):  AutoIt 3.2 ++
; Return Value(s): 1 - Success, 0 and @error = 1 - Failure,
; Author(s):       RazerM
;
;===============================================================================
;
Func _SoundSeek($sSnd_id, $iHour, $iMin, $iSec)
    ;Declare variables
    Local $iMs = 0
    Local $iRet
    ;prepare mci to recieve time in milliseconds
    mciSendString("set " & FileGetShortName($sSnd_id) & " time format miliseconds")
    ;modify the $iHour, $iMin and $iSec parameters to be in milliseconds
    ;and add to $iMs
    $iMs += $iSec * 1000
    $iMs += $iMin * 60 * 1000
    $iMs += $iHour * 60 * 60 * 1000
    ; seek sound to time ($iMs)
    $iRet = mciSendString("seek " & FileGetShortName($sSnd_id) & " to " & $iMs)
    ;return
    If $iRet = 0 Then
        Return 1
    Else
        Return SetError(1, 0, 0)
    EndIf
EndFunc   ;==>_SoundSeek

;===============================================================================
;
; Function Name:   _SoundStatus
; 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):    $sSnd_id - Sound ID returned by _SoundOpen or sound file
; Requirement(s):  AutoIt 3.2 ++
; Return Value(s): Sound Status
; Author(s):       RazerM
;
;===============================================================================
;
Func _SoundStatus($sSnd_id)
    ;return status
    Return mciSendString("status " & FileGetShortName($sSnd_id) & " mode")
EndFunc   ;==>_SoundStatus

;===============================================================================
;
; Function Name:   _SoundPos
; Description::    Returns the current position of the song
; Parameter(s):    $sSnd_id - Sound ID returned by _SoundOpen or sound file,
;                  $iMode = 1 - hh:mm:ss, $iMode = 2 - milliseconds
; Requirement(s):  AutoIt 3.2 ++
; Return Value(s): Current Position - Success, @error = 1 - $iMode is invalid
; Author(s):       RazerM
;
;===============================================================================
;
Func _SoundPos($sSnd_id, $iMode = 1)
    ;Declare variables
    Local $iSnd_pos_ms, $iSnd_pos_min, $iSnd_pos_hour, $iSnd_pos_sec, $sSnd_pos_format
    ;validate $iMode
    If $iMode <> 1 And $iMode <> 2 Then Return SetError(1, 0, 0)
    ;tell mci to use time in milliseconds
    mciSendString("set " & FileGetShortName($sSnd_id) & " time format miliseconds")
    ;receive position of sound
    $iSnd_pos_ms = mciSendString("status " & FileGetShortName($sSnd_id) & " position")
    ;modify data and assign to variables
    $iSnd_pos_min = Int($iSnd_pos_ms / 60000)
    $iSnd_pos_hour = Int($iSnd_pos_min / 60)
    $iSnd_pos_sec = Int(Int($iSnd_pos_ms / 1000) - ($iSnd_pos_min * 60))
    ;assign formatted data to $sSnd_pos_format
    $sSnd_pos_format = StringFormat("%02i:%02i:%02i", $iSnd_pos_hour, $iSnd_pos_min, $iSnd_pos_sec)
    ;return correct variable
    If $iMode = 1 Then Return $sSnd_pos_format
    If $iMode = 2 Then Return $iSnd_pos_ms
EndFunc   ;==>_SoundPos

;internal functions
Func mciSendString($string)
    Local $iRet
    $iRet = DllCall("winmm.dll", "int", "mciSendStringA", "str", $string, "str", "", "int", 65534, "hwnd", 0)
    If Not @error Then Return $iRet[2]
EndFunc   ;==>mciSendString

Func RandomStr($len)
    Local $string
    For $iCurrentPos = 1 To $len
        $string &= Chr(Random(97, 122, 1))
    Next
    Return $string
EndFunc   ;==>RandomStr
Edited by jennico

Spoiler

I actively support Wikileaks | Freedom for Julian Assange ! | Defend freedom of speech ! | Fight censorship ! | I will not silence.OixB7.jpgDon't forget this IP: 213.251.145.96

 

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  

×