ajhhanekamp Posted July 24, 2009 Share Posted July 24, 2009 I want to make an MP3 file repeat, now i have a file played by using 'SoundPlay'. I can make it play but not make it repeat, i would like to have it play unlimited. I hope someone can help me out. Link to comment Share on other sites More sharing options...
Developers Jos Posted July 24, 2009 Developers Share Posted July 24, 2009 I want to make an MP3 file repeat, now i have a file played by using 'SoundPlay'. I can make it play but not make it repeat, i would like to have it play unlimited. I hope someone can help me out.You can start a MP3 with _SoundPlay() and check the Length with _SoundLength().Then use an Adlib() func to check the _SoundPos() and compare it with the saved length and start the MP3 again when the end is reached. SciTE4AutoIt3 Full installer Download page - Beta files Read before posting How to post scriptsource Forum etiquette Forum Rules Live for the present, Dream of the future, Learn from the past. Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted July 24, 2009 Moderators Share Posted July 24, 2009 ajhhanekamp,The easiest way to see if teh sound file has finished is to use the _SoundStatus function:#include <Sound.au3> HotKeySet("{ESCAPE}", "_Exit") $vSndID = _SoundOpen("your file path") While 1 If _SoundStatus($vSndID) = "stopped" Then _SoundPlay($vSndID) Sleep(10) WEnd Func _Exit() _SoundStop($vSndID) Exit EndFuncM23 Xandy 1 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
ajhhanekamp Posted July 24, 2009 Author Share Posted July 24, 2009 It's giving me some kind of error: C:\Program Files\AutoIt3\Include\Sound.au3 (442) : ==> Array variable has incorrect number of subscripts or subscript dimension range exceeded.: If $aSndID[1] <> 0 Then If ^ ERROR >Exit code: 1 Time: 0.419 ajhhanekamp, The easiest way to see if teh sound file has finished is to use the _SoundStatus function: #include <Sound.au3> HotKeySet("{ESCAPE}", "_Exit") $vSndID = _SoundOpen("your file path") While 1 If _SoundStatus($vSndID) = "stopped" Then _SoundPlay($vSndID) Sleep(10) WEnd Func _Exit() _SoundStop($vSndID) Exit EndFunc M23 Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted July 25, 2009 Moderators Share Posted July 25, 2009 ajhhanekamp,Sorry about that - what version of Autoit are you running? We have just rewritten the Sound UDF - the latest version is in the Beta install.If you do not want to install the Beta, just save the following code into the same folder as your script under the name Sound_Beta.au3 and then change the include line to read #include "Sound_Beta.au3". All should then work. expandcollapse popup#include-once #include <File.au3> ; Using: _PathSplit ; #INDEX# ======================================================================================================================= ; Title .........: Sound ; AutoIt Version : 3.2 ++ ; Language ......: English ; Description ...: Functions that assist with Sound management. ; Author(s) .....: RazerM, Melba23, Simucal, PsaltyDS ; Dll ...........: winmm.dll ; =============================================================================================================================== ; #CONSTANTS# =================================================================================================================== Global Const $__SOUNDCONSTANT_SNDID_MARKER = 0x49442d2d ; =============================================================================================================================== ; #CURRENT# ===================================================================================================================== ;_SoundOpen ;_SoundClose ;_SoundPlay ;_SoundStop ;_SoundPause ;_SoundResume ;_SoundLength ;_SoundSeek ;_SoundStatus ;_SoundPos ; =============================================================================================================================== ; #INTERNAL_USE_ONLY#============================================================================================================ ;__SoundChkSndID ;__SoundMciSendString ;__SoundReadTLENFromMP3 ;__SoundReadXingFromMP3 ;__SoundHexToString ;__SoundTicksToTime ;__SoundTimeToTicks ; =============================================================================================================================== ; #FUNCTION# ==================================================================================================================== ; Name...........: _SoundOpen ; Description ...: Opens a sound file for use with other _Sound functions ; Syntax.........: _SoundOpen($sFile) ; Parameters ....: $sFile - The sound file ; Return values..: Success - 3-element array (used as Sound ID) ; Failure - 0 and Sets @error to: ; @error 1 - Open failed - @extended holds MCI error code ; 2 - File does not exist ; Author ........: RazerM, Melba23, some code by Simucal, PsaltyDS ; Modified.......: ; Remarks .......: ; Related .......: _SoundClose, _SoundLength, _SoundPause, _SoundPlay, , _SoundResume, _SoundStatus, _SoundStop ; Link ..........: ; Example .......: Yes ; =============================================================================================================================== Func _SoundOpen($sFile) ;Declare variables Local $aSndID[4], $iRet, $fTryNextMethod = False, $hFile Local $szDrive, $szDir, $szFName, $szExt, $iSndLenMs, $iSndLenMin, $iSndLenHour, $iSndLenSecs Local $sSndDirName, $sSndFileName, $sSndDirShortName, $oShell, $oShellDir, $oShellDirFile, $sRaw, $aInfo Local $sTrackLength, $iSoundTicks, $iActualTicks, $iVBRRatio, $aiTime, $sTag ;check for file If Not FileExists($sFile) Then Return SetError(2, 0, 0) ;create random string for file ID For $i = 1 To 10 $aSndID[0] &= Chr(Random(97, 122, 1)) Next _PathSplit($sFile, $szDrive, $szDir, $szFName, $szExt) If $szDrive = "" Then $sSndDirName = @WorkingDir & "\" Else $sSndDirName = $szDrive & $szDir EndIf $sSndFileName = $szFName & $szExt $sSndDirShortName = FileGetShortName($sSndDirName, 1) ;open file $iRet = __SoundMciSendString("open " & FileGetShortName($sFile) & " alias " & $aSndID[0]) If $iRet <> 0 Then Return SetError(1, $iRet, 0) ; open failed $oShell = ObjCreate("shell.application") If IsObj($oShell) Then $oShellDir = $oShell.NameSpace($sSndDirShortName) If IsObj($oShellDir) Then $oShellDirFile = $oShellDir.Parsename($sSndFileName) If IsObj($oShellDirFile) Then $sRaw = $oShellDir.GetDetailsOf($oShellDirFile, -1) $aInfo = StringRegExp($sRaw, ": ([0-9]{2}:[0-9]{2}:[0-9]{2})", 3) If Not IsArray($aInfo) Then $fTryNextMethod = True Else $sTrackLength = $aInfo[0] EndIf Else $fTryNextMethod = True EndIf Else $fTryNextMethod = True EndIf Else $fTryNextMethod = True EndIf If $fTryNextMethod Then $fTryNextMethod = False If $szExt = ".mp3" Then $hFile = FileOpen(FileGetShortName($sSndDirName & $sSndFileName), 4) $sTag = FileRead($hFile, 5156) FileClose($hFile) $sTrackLength = __SoundReadXingFromMP3($sTag) If @error Then $fTryNextMethod = True Else $fTryNextMethod = True EndIf EndIf If $fTryNextMethod Then $fTryNextMethod = False If $szExt = ".mp3" Then $sTrackLength = __SoundReadTLENFromMP3($sTag) If @error Then $fTryNextMethod = True Else $fTryNextMethod = True EndIf EndIf FileClose($hFile) If $fTryNextMethod Then $fTryNextMethod = False ;tell mci to use time in milliseconds __SoundMciSendString("set " & $aSndID[0] & " time format miliseconds") ;receive length of sound $iSndLenMs = __SoundMciSendString("status " & $aSndID[0] & " length", 255) ;assign modified data to variables __SoundTicksToTime($iSndLenMs, $iSndLenHour, $iSndLenMin, $iSndLenSecs) ;assign formatted data to $sSndLenFormat $sTrackLength = StringFormat("%02i:%02i:%02i", $iSndLenHour, $iSndLenMin, $iSndLenSecs) EndIf ; Convert Track_Length to mSec $aiTime = StringSplit($sTrackLength, ":") $iActualTicks = __SoundTimeToTicks($aiTime[1], $aiTime[2], $aiTime[3]) ;tell mci to use time in milliseconds __SoundMciSendString("set " & $aSndID[0] & " time format miliseconds") ;;Get estimated length $iSoundTicks = __SoundMciSendString("status " & $aSndID[0] & " length", 255) ;Compare to actual length If Abs($iSoundTicks - $iActualTicks) < 1000 Then ;Assume CBR, as our track length from shell.application is only accurate within 1000ms $iVBRRatio = 0 Else ;Set correction ratio for VBR operations $iVBRRatio = $iSoundTicks / $iActualTicks EndIf $aSndID[1] = $iVBRRatio $aSndID[2] = 0 $aSndID[3] = $__SOUNDCONSTANT_SNDID_MARKER Return SetError(0, $iRet, $aSndID) EndFunc ;==>_SoundOpen ; #FUNCTION# ==================================================================================================================== ; Name...........: _SoundClose ; Description ...: Closes a sound ; Syntax.........: _SoundClose($aSndID) ; Parameters ....: $aSndID - Sound ID returned by _SoundOpen() ; Return values..: Success - 1 ; Failure - 0 and set @error ; @error 1 - Close failed ; 3 - Invalid Sound ID ; Author ........: RazerM, Melba23 ; Modified.......: ; Remarks .......: ; Related .......: ; Link ..........: ; Example .......: Yes ; =============================================================================================================================== Func _SoundClose($aSndID) If Not IsArray($aSndID) Or Not __SoundChkSndID($aSndID) Then Return SetError(3, 0, 0) ; invalid sound ID If __SoundMciSendString("close " & $aSndID[0]) = 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) ; Syntax.........:_SoundPlay($aSndID[, $iWait = 0]) ; Parameters ....: $aSndID - Sound ID returned by _SoundOpen() or sound file ; $iWait - If set to 1 the script will wait for the sound to finish before continuing ; Return values..: Success - 1 ; Failure - 0 and set @error ; @error 1 - Play failed ; 2 - Invalid $iWait parameter ; 3 - Invalid Sound ID or file name ; Author ........: RazerM, Melba23 ; Modified.......: ; Remarks .......: ; Related .......: _SoundPause, _SoundStop, _SoundSeek, _SoundOpen, _SoundResume ; Link ..........: ; Example .......: Yes ; =============================================================================================================================== Func _SoundPlay($aSndID, $iWait = 0) ;Declare variables Local $iRet ;validate $fWait If $iWait <> 0 And $iWait <> 1 Then Return SetError(2, 0, 0) ; invalid $iWait parameter If Not __SoundChkSndID($aSndID) Then Return SetError(3, 0, 0) ; invalid Sound ID or file name ;if sound has finished, seek to start If _SoundPos($aSndID, 2) = _SoundLength($aSndID, 2) Then __SoundMciSendString("seek " & $aSndID[0] & " to start") ;If $fWait = 1 then pass wait to mci If $iWait = 1 Then $iRet = __SoundMciSendString("play " & $aSndID[0] & " wait") Else $iRet = __SoundMciSendString("play " & $aSndID[0]) EndIf ;return If $iRet = 0 Then Return 1 Else Return SetError(1, 0, 0) EndIf EndFunc ;==>_SoundPlay ; #FUNCTION# ==================================================================================================================== ; Name...........: _SoundStop ; Description ...: Stops the sound ; Syntax.........: _SoundStop(ByRef $aSndID) ; Parameters ....: $aSndID - Sound ID returned by _SoundOpen() or sound file (must be a variable) ; Return values..: Success - 1 ; Failure - 0 and set @error ; @error 1 - Stop failed ; 3 - Invalid Sound ID or file name ; Author ........: RazerM, Melba23 ; Modified.......: ; Remarks .......: ; Related .......: ; Link ..........: ; Example .......: Yes ; =============================================================================================================================== Func _SoundStop(ByRef $aSndID) ;Declare variables Local $iRet, $iRet2, $vTemp ; create temp variable so file name variable is not changed ByRef $vTemp = $aSndID If Not __SoundChkSndID($vTemp) Then Return SetError(3, 0, 0) ; invalid Sound ID or file name ;seek to start $iRet = __SoundMciSendString("seek " & $vTemp[0] & " to start") ;stop $iRet2 = __SoundMciSendString("stop " & $vTemp[0]) ;reset VBR factor if used If IsArray($aSndID) Then $aSndID[2] = 0 ;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 ; Syntax.........: _SoundPause($aSndID) ; Parameters ....: $aSndID - Sound ID returned by _SoundOpen() or sound file ; Return values..: Success - 1 ; Failure - 0 and sets @error ; @error 1 - Pause failed ; 3 - Invalid Sound ID or file name ; Author ........: RazerM, Melba23 ; Modified.......: ; Remarks .......: ; Related .......: _SoundResume, _SoundOpen, _SoundPlay ; Link ..........: ; Example .......: Yes ; =============================================================================================================================== Func _SoundPause($aSndID) ;Declare variables Local $iRet If Not __SoundChkSndID($aSndID) Then Return SetError(3, 0, 0) ; invalid Sound ID or file name ;pause sound $iRet = __SoundMciSendString("pause " & $aSndID[0]) ;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 ; Syntax.........: _SoundResume($aSndID) ; Parameters ....: $aSndID - Sound ID returned by _SoundOpen() or sound file ; Return values..: Success - 1 ; Failure - 0 and set @error ; @error 1 - Resume failed ; 3 - Invalid Sound ID or file name ; Author ........: RazerM, Melba23 ; Modified.......: ; Remarks .......: ; Related .......: ; Link ..........: ; Example .......: Yes ; =============================================================================================================================== Func _SoundResume($aSndID) ;Declare variables Local $iRet If Not __SoundChkSndID($aSndID) Then Return SetError(3, 0, 0) ; invalid Sound ID or file name ;resume sound $iRet = __SoundMciSendString("resume " & $aSndID[0]) ;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 ; Syntax.........: _SoundLength($aSndID[, $iMode = 1]) ; Parameters ....: $aSndID - Sound ID returned by _SoundOpen() or sound file, ; $iMode = 1 - hh:mm:ss, $iMode = 2 - milliseconds ; Return values .: Success - Length of the sound ; Failure - 0 and set @error ; @error 1 - Invalid $iMode parameter ; 3 - Invalid Sound ID or file name ; Author ........: RazerM, Melba23 ; Modified.......: jpm ; Remarks .......: ; Related .......: ; Link ..........: ; Example .......: Yes ; =============================================================================================================================== Func _SoundLength($aSndID, $iMode = 1) ;Declare variables Local $iSndLenMs, $bFile = False ;validate $iMode If $iMode <> 1 And $iMode <> 2 Then Return SetError(1, 0, 0) If Not IsArray($aSndID) Then If Not FileExists($aSndID) Then Return SetError(3, 0, 0) ; invalid file name $bFile = True $aSndID = _SoundOpen($aSndID) Else If Not __SoundChkSndID($aSndID) Then Return SetError(3, 0, 0) ; invalid Sound ID EndIf ;tell mci to use time in milliseconds __SoundMciSendString("set " & $aSndID[0] & " time format miliseconds") ;receive length of sound $iSndLenMs = Number(__SoundMciSendString("status " & $aSndID[0] & " length", 255)) If $aSndID[1] <> 0 Then $iSndLenMs = Round($iSndLenMs / $aSndID[1]) If $bFile Then _SoundClose($aSndID) ;if user called _SoundLength with a filename If $iMode = 2 Then Return $iSndLenMs ; $iMode = 1 (hh:mm:ss) Local $iSndLenMin, $iSndLenHour, $iSndLenSecs ;assign modified data to variables __SoundTicksToTime($iSndLenMs, $iSndLenHour, $iSndLenMin, $iSndLenSecs) ;assign formatted data to $sSndLenFormat Local $sSndLenFormat = StringFormat("%02i:%02i:%02i", $iSndLenHour, $iSndLenMin, $iSndLenSecs) ;return correct variable Return $sSndLenFormat EndFunc ;==>_SoundLength ; #FUNCTION# ==================================================================================================================== ; Name...........: _SoundSeek ; Description ...: Seeks the sound to a specified time ; Syntax.........: _SoundSeek(ByRef $aSndID, $iHour, $iMin, $iSec) ; Parameters ....: $aSndID - Sound ID returned by _SoundOpen() (must NOT be a file), $iHour, $iMin, $iSec ; Return values..: Success - 1 ; Failure - 0 and set @error ; @error 1 - Seek failed ; 3 - Invalid Sound ID ; Author ........: RazerM, Melba23 ; Modified.......: ; Remarks .......: ; Related .......: ; Link ..........: ; Example .......: Yes ; =============================================================================================================================== Func _SoundSeek(ByRef $aSndID, $iHour, $iMin, $iSec) ;Declare variables Local $iMs = 0, $iRet If Not IsArray($aSndID) Or Not __SoundChkSndID($aSndID) Then Return SetError(3, 0, 0) ; invalid Sound ID ;prepare mci to receive time in milliseconds __SoundMciSendString("set " & $aSndID[0] & " 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 If $aSndID[1] <> 0 Then $aSndID[2] = Round($iMs * $aSndID[1]) - $iMs $iMs = Round($iMs * $aSndID[1]) EndIf ; seek sound to time ($iMs) $iRet = __SoundMciSendString("seek " & $aSndID[0] & " to " & $iMs) If _SoundPos($aSndID, 2) < 0 Then $aSndID[2] = 0 ;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. ; Syntax.........: _SoundStatus($aSndID) ; Parameters ....: $aSndID - Sound ID returned by _SoundOpen() or sound file ; Return values..: Success - Sound status ; Failure - 0 and set @error ; @error 3 - Invalid Sound ID or file name ; Author ........: RazerM, Melba23 ; Modified.......: ; Remarks .......: Some devices can return the additional "open", "parked", "recording", and "seeking" values.(MSDN) ; Related .......: ; Link ..........: ; Example .......: Yes ; =============================================================================================================================== Func _SoundStatus($aSndID) If Not __SoundChkSndID($aSndID) Then Return SetError(3, 0, 0) ; invalid Sound ID or file name ;return status Return __SoundMciSendString("status " & $aSndID[0] & " mode", 255) EndFunc ;==>_SoundStatus ; #FUNCTION# ==================================================================================================================== ; Name...........: _SoundPos ; Description ...: Returns the current position of the song ; Syntax.........: _SoundPos($aSndID[, $iMode = 1]) ; Parameters ....: $aSndID - Sound ID returned by _SoundOpen() or sound file, ; $iMode = 1 - hh:mm:ss, $iMode = 2 - milliseconds ; Return values..: Success - Current position ; Failure - 0 and set @error ; @error 1 - Invalid $iMode ; |3 - Invalid Sound ID or file name ; Author ........: RazerM, Melba23 ; Modified.......: ; Remarks .......: ; Related .......: ; Link ..........: ; Example .......: Yes ; =============================================================================================================================== Func _SoundPos($aSndID, $iMode = 1) ;Declare variables Local $iSndPosMs ;validate $iMode If $iMode <> 1 And $iMode <> 2 Then Return SetError(1, 0, 0) If Not __SoundChkSndID($aSndID) Then Return SetError(3, 0, 0) ; invalid Sound ID or file name ;tell mci to use time in milliseconds __SoundMciSendString("set " & $aSndID[0] & " time format miliseconds") ;receive position of sound $iSndPosMs = Number(__SoundMciSendString("status " & $aSndID[0] & " position", 255)) If $aSndID[1] <> 0 Then $iSndPosMs -= $aSndID[2] EndIf If $iMode = 2 Then Return $iSndPosMs ;$iMode = 1 (hh:mm:ss) Local $iSndPosMin, $iSndPosHour, $iSndPosSecs ;modify data and assign to variables __SoundTicksToTime($iSndPosMs, $iSndPosHour, $iSndPosMin, $iSndPosSecs) ;assign formatted data to $sSndPosFormat Local $sSndPosHMS = StringFormat("%02i:%02i:%02i", $iSndPosHour, $iSndPosMin, $iSndPosSecs) ;return correct variable Return $sSndPosHMS EndFunc ;==>_SoundPos ; #INTERNAL_USE_ONLY# =========================================================================================================== ; Name...........: __SoundChkSndID ; Description ...: Used internally within this file, not for general use ; Syntax.........: __SoundChkSndID(ByRef $aSndID, $bInit=False, $iPos=Default) ; Author ........: jpm ; Modified.......: Melba23 ; Remarks .......: ; Related .......: ; Link ..........: ; Example .......: ; =============================================================================================================================== Func __SoundChkSndID(ByRef $aSndID) Local $vTemp If Not IsArray($aSndID) Then If Not FileExists($aSndID) Then Return 0 ; invalid Sound file $vTemp = FileGetShortName($aSndID) Dim $aSndID[4] = [$vTemp, 0, 0, $__SOUNDCONSTANT_SNDID_MARKER] ; create valid Sound ID array for use in UDF Else If UBound($aSndID) <> 4 And $aSndID[3] <> $__SOUNDCONSTANT_SNDID_MARKER Then Return 0 ; invalid Sound ID EndIf Return 1 EndFunc ;==>__SoundChkSndID ; #INTERNAL_USE_ONLY# =========================================================================================================== ; Name...........: __SoundMciSendString ; Description ...: Used internally within this file, not for general use ; Syntax.........: __SoundMciSendString($string[, $iLen = 0]) ; Author ........: RazerM, Melba23 ; Modified.......: ; Related .......: ; Link ..........: ; Example .......: ; =============================================================================================================================== Func __SoundMciSendString($string, $iLen = 0) Local $iRet $iRet = DllCall("winmm.dll", "int", "mciSendStringA", "str", $string, "str", "", "long", $iLen, "long", 0) If Not @error Then Return $iRet[2] EndFunc ;==>__SoundMciSendString ; #INTERNAL_USE_ONLY# =========================================================================================================== ; Name...........: __SoundReadTLENFromMP3 ; Description ...: Used internally within this file, not for general use ; Syntax.........: __SoundReadTLENFromMP3($sTag) ; Parameters ....: $sTag - >= 1024 bytes from 'read raw' mode. ; Return values .: Success - Sound length (hh:mm:ss) ; Failure - 0 and @error = 1 ; Author ........: Melba23 ; Modified.......: RazerM ; Remarks .......: File must be an mp3 AFAIK ; Related .......: ; Link ..........: ; Example .......: ; =============================================================================================================================== Func __SoundReadTLENFromMP3($sTag) Local $iTemp, $sTemp, $iLengthMs, $iLengthHour, $iLengthMin, $iLengthSecs ; Check that an ID3v2.3 tag is present If StringLeft($sTag, 10) <> "0x49443303" Then Return SetError(1, 0, 0) $iTemp = StringInStr($sTag, "544C454E") + 21 $sTag = StringTrimLeft($sTag, $iTemp) $sTemp = "" For $i = 1 To 32 Step 2 If StringMid($sTag, $i, 2) = "00" Then ExitLoop Else $sTemp &= StringMid($sTag, $i, 2) EndIf Next $iLengthMs = Number(__SoundHexToString($sTemp)) If $iLengthMs > 0 Then __SoundTicksToTime($iLengthMs, $iLengthHour, $iLengthMin, $iLengthSecs) ;Convert to hh:mm:ss and return Return StringFormat("%02i:%02i:%02i", $iLengthHour, $iLengthMin, $iLengthSecs) Else Return SetError(1, 0, 0) EndIf EndFunc ;==>__SoundReadTLENFromMP3 ; #INTERNAL_USE_ONLY# =========================================================================================================== ; Name...........: __SoundReadXingFromMP3 ; Syntax.........: __SoundReadXingFromMP3($sTag) ; Parameters ....: $sTag - first 5156 bytes from 'read raw' mode. ; Return values .: Success - Sound length (hh:mm:ss) ; Failure - 0 and @error = 1 ; Author ........: Melba23 ; Modified.......: RazerM ; Remarks .......: File must be an mp3 AFAIK ; Related .......: ; Link ..........: ; Example .......: ; =============================================================================================================================== Func __SoundReadXingFromMP3($sTag) Local $iXingPos, $iFlags, $iFrames, $sHeader, $iMPEGByte, $iFreqByte, $iMPEGVer, $iLayerNum, $iSamples, $iFreqNum, $iFrequency, $iLengthMs, $iLengthHours, $iLengthMins, $iLengthSecs $iXingPos = StringInStr($sTag, "58696E67") If $iXingPos = 0 Then Return SetError(1, 0, 0) ; Read fields flag $iFlags = Number("0x" & StringMid($sTag, $iXingPos + 14, 2)) If BitAND($iFlags, 1) = 1 Then $iFrames = Number("0x" & StringMid($sTag, $iXingPos + 16, 8)) Else Return SetError(1, 0, 0); No frames field EndIf ; Now to find Samples per frame & Sampling rate ; Go back to the frame header start $sHeader = StringMid($sTag, $iXingPos - 72, 8) ; Read the relevant bytes $iMPEGByte = Number("0x" & StringMid($sHeader, 4, 1)) $iFreqByte = Number("0x" & StringMid($sHeader, 6, 1)) ; Decode them ; 8 = MPEG-1, 0 = MPEG-2 $iMPEGVer = BitAND($iMPEGByte, 8) ; 2 = Layer III, 4 = Layer II, 6 = Layer I $iLayerNum = BitAND($iMPEGByte, 6) Switch $iLayerNum Case 6 $iSamples = 384 Case 4 $iSamples = 1152 Case 2 Switch $iMPEGVer Case 8 $iSamples = 1152 Case 0 $iSamples = 576 Case Else $iSamples = 0 EndSwitch Case Else $iSamples = 0 EndSwitch ; If not valid return If $iSamples = 0 Then Return SetError(1, 0, 0) ; 0 = bit 00, 4 = Bit 01, 8 = Bit 10 $iFreqNum = BitAND($iFreqByte, 12) Switch $iFreqNum Case 0 $iFrequency = 44100 Case 4 $iFrequency = 48000 Case 8 $iFrequency = 32000 Case Else $iFrequency = 0 EndSwitch ; If not valid return If $iFrequency = 0 Then Return SetError(1, 0, 0) ; MPEG-2 halves the value If $iMPEGVer = 0 Then $iFrequency = $iFrequency / 2 ; Duration in secs = No of frames * Samples per frame / Sampling freq $iLengthMs = Int(($iFrames * $iSamples / $iFrequency) * 1000) ; Convert to hh:mm:ss and return __SoundTicksToTime($iLengthMs, $iLengthHours, $iLengthMins, $iLengthSecs) Return StringFormat("%02i:%02i:%02i", $iLengthHours, $iLengthMins, $iLengthSecs) EndFunc ;==>__SoundReadXingFromMP3 ; #INTERNAL_USE_ONLY# =========================================================================================================== ; Name...........: _HexToString ; Description ...: Convert a hex string to a string. ; Syntax.........: _HexToString($strHex) ; Parameters ....: $strHex - an hexadecimal string ; Return values .: Success - Returns a string. ; Failure - Returns -1 and sets @error to 1. ; Author ........: Jarvis Stubblefield ; Modified.......: SmOke_N - (Re-write using BinaryToString for speed) ; Remarks .......: ; Related .......: _StringToHex ; Link ..........: ; Example .......: Yes ; =============================================================================================================================== Func __SoundHexToString($strHex) If StringLeft($strHex, 2) = "0x" Then Return BinaryToString($strHex) Return BinaryToString("0x" & $strHex) EndFunc ;==>__SoundHexToString ; #INTERNAL_USE_ONLY# =========================================================================================================== ; Name...........: _TicksToTime ; Description ...: Converts the specified tick amount to hours, minutes and seconds. ; Syntax.........: _TicksToTime($iTicks, ByRef $iHours, ByRef $iMins, ByRef $iSecs) ; Parameters ....: $iTicks - Tick amount. ; $iHours - Variable to store the hours. ; $iMins - Variable to store the minutes. ; $iSecs - Variable to store the seconds. ; Return values .: Success - 1 ; Failure - 0 ; @Error - 0 - No error. ; |1 - $iTicks isn't an integer. ; Author ........: Marc <mrd at gmx de> ; Modified.......: ; Remarks .......: ; Related .......: __SoundTimeToTicks ; Link ..........: ; Example .......: Yes ; =============================================================================================================================== Func __SoundTicksToTime($iTicks, ByRef $iHours, ByRef $iMins, ByRef $iSecs) If Number($iTicks) > 0 Then $iTicks = Round($iTicks / 1000) $iHours = Int($iTicks / 3600) $iTicks = Mod($iTicks, 3600) $iMins = Int($iTicks / 60) $iSecs = Round(Mod($iTicks, 60)) ; If $iHours = 0 then $iHours = 24 Return 1 ElseIf Number($iTicks) = 0 Then $iHours = 0 $iTicks = 0 $iMins = 0 $iSecs = 0 Return 1 Else Return SetError(1,0,0) EndIf EndFunc ;==>__SoundTicksToTime ; #INTERNAL_USE_ONLY# =========================================================================================================== ; Name...........: _TimeToTicks ; Description ...: Converts the specified hours, minutes, and seconds to ticks. ; Syntax.........: _TimeToTicks([$iHours = @HOUR[, $iMins = @MIN[, $iSecs = @SEC]]]) ; Parameters ....: $iHours - The hours. ; $iMins - The minutes. ; $iSecs - The seconds. ; Return values .: Success - Returns the number of ticks. ; Failure - 0 ; @Error - 0 - No error. ; |1 - The specified hours, minutes, or seconds are not valid. ; Author ........: Marc <mrd at gmx de> ; Modified.......: SlimShady: added the default time and made parameters optional ; Remarks .......: ; Related .......: _TicksToTime ; Link ..........: ; Example .......: Yes ; =============================================================================================================================== Func __SoundTimeToTicks($iHours = @HOUR, $iMins = @MIN, $iSecs = @SEC) ;============================================== ; Local Constant/Variable Declaration Section ;============================================== Local $iTicks If StringIsInt($iHours) And StringIsInt($iMins) And StringIsInt($iSecs) Then $iTicks = 1000 * ((3600 * $iHours) + (60 * $iMins) + $iSecs) Return $iTicks Else Return SetError(1,0,0) EndIf EndFunc ;==>__SoundTimeToTicksWhen the new stable version of Autoit is released, the standard Sound.au3 include will be updated to this version - at least that is the plan!M23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
ajhhanekamp Posted July 25, 2009 Author Share Posted July 25, 2009 I am using version 1.77. Somehow its still giving me this error: >"C:\Program Files\AutoIt3\SciTE\..\autoit3.exe" /ErrorStdOut "C:\Documents and Settings\Arjan Hanekamp\Bureaublad\autoit\progs.au3" C:\Documents and Settings\Arjan Hanekamp\Bureaublad\autoit\progs.au3 (27) : ==> "Func" statement has no matching "EndFunc".: Func _Exit() >Exit code: 1 Time: 0.307 I will just send you my complete code: expandcollapse popup#include <GUIConstantsEx.au3> #include <ButtonConstants.au3> #include <Sound_Beta.au3> #include <Sound.au3> Opt('MustDeclareVars', 1) Example() Func Example() Local $Button_1, $Button_2, $Button_3, $Button_4, $msg, $n Local $GUI_DISABLE GUICreate("Games & Programs Launcher", 800, 600) HotKeySet("{ESCAPE}", "_Exit") $vSndID = _SoundOpen("C:\Pangya - caramelldansen.mp3") While 1 If _SoundStatus($vSndID) = "stopped" Then _SoundPlay($vSndID) Sleep(10) WEnd Func _Exit() _SoundStop($vSndID) Exit EndFunc GuiCtrlCreatePic('C:\Fire.jpg', 0, 0, @Desktopwidth, @DesktopHeight) GuiCtrlSetState(1,$GUI_DISABLE) $n = GUICtrlCreatePic("C:\games.jpg", 380, 10, 120, 120) Opt("GUICoordMode", 2) $Button_1 = GUICtrlCreateButton("Command and Conquer Generals", -480, 15, 170, 50) $Button_2 = GUICtrlCreateButton("Command and Conquer Generals Zero: Hour", 140, -50, 220, 50 ) $Button_3 = GUICtrlCreateButton("NTreev USA Pangya", 110, -50, 120 , 50) $n = GUICtrlCreatePic("C:\programs.jpg", -490, 30, 300, 100) $Button_4 = GUICtrlCreateButton("Microsoft Office Word 2003", -570, 15, 170, 50) GUISetState() While 1 $msg = GUIGetMsg() Select Case $msg = $GUI_EVENT_CLOSE Exitloop Case $msg = $Button_1 Run ( "E:\Command and Conquer Generals\generals.exe") Case $msg = $Button_2 Run ( "E:\Command & Conquer Generals Zero Hour\generals.exe") Case $msg = $Button_3 Run ( "E:\Ntreev USA\Pangya\update.exe") Case $msg = $Button_4 Run ( "C:\Program Files\Microsoft Office\OFFICE11\WINWORD.EXE") EndSelect WEnd EndFunc MsgBox(64, 'Notification', 'Games & Programs Launcher will close now') Exit ajhhanekamp, Sorry about that - what version of Autoit are you running? We have just rewritten the Sound UDF - the latest version is in the Beta install. If you do not want to install the Beta, just save the following code into the same folder as your script under the name Sound_Beta.au3 and then change the include line to read #include "Sound_Beta.au3". All should then work. expandcollapse popup#include-once #include <File.au3> ; Using: _PathSplit ; #INDEX# ======================================================================================================================= ; Title .........: Sound ; AutoIt Version : 3.2 ++ ; Language ......: English ; Description ...: Functions that assist with Sound management. ; Author(s) .....: RazerM, Melba23, Simucal, PsaltyDS ; Dll ...........: winmm.dll ; =============================================================================================================================== ; #CONSTANTS# =================================================================================================================== Global Const $__SOUNDCONSTANT_SNDID_MARKER = 0x49442d2d ; =============================================================================================================================== ; #CURRENT# ===================================================================================================================== ;_SoundOpen ;_SoundClose ;_SoundPlay ;_SoundStop ;_SoundPause ;_SoundResume ;_SoundLength ;_SoundSeek ;_SoundStatus ;_SoundPos ; =============================================================================================================================== ; #INTERNAL_USE_ONLY#============================================================================================================ ;__SoundChkSndID ;__SoundMciSendString ;__SoundReadTLENFromMP3 ;__SoundReadXingFromMP3 ;__SoundHexToString ;__SoundTicksToTime ;__SoundTimeToTicks ; =============================================================================================================================== ; #FUNCTION# ==================================================================================================================== ; Name...........: _SoundOpen ; Description ...: Opens a sound file for use with other _Sound functions ; Syntax.........: _SoundOpen($sFile) ; Parameters ....: $sFile - The sound file ; Return values..: Success - 3-element array (used as Sound ID) ; Failure - 0 and Sets @error to: ; @error 1 - Open failed - @extended holds MCI error code ; 2 - File does not exist ; Author ........: RazerM, Melba23, some code by Simucal, PsaltyDS ; Modified.......: ; Remarks .......: ; Related .......: _SoundClose, _SoundLength, _SoundPause, _SoundPlay, , _SoundResume, _SoundStatus, _SoundStop ; Link ..........: ; Example .......: Yes ; =============================================================================================================================== Func _SoundOpen($sFile) ;Declare variables Local $aSndID[4], $iRet, $fTryNextMethod = False, $hFile Local $szDrive, $szDir, $szFName, $szExt, $iSndLenMs, $iSndLenMin, $iSndLenHour, $iSndLenSecs Local $sSndDirName, $sSndFileName, $sSndDirShortName, $oShell, $oShellDir, $oShellDirFile, $sRaw, $aInfo Local $sTrackLength, $iSoundTicks, $iActualTicks, $iVBRRatio, $aiTime, $sTag ;check for file If Not FileExists($sFile) Then Return SetError(2, 0, 0) ;create random string for file ID For $i = 1 To 10 $aSndID[0] &= Chr(Random(97, 122, 1)) Next _PathSplit($sFile, $szDrive, $szDir, $szFName, $szExt) If $szDrive = "" Then $sSndDirName = @WorkingDir & "\" Else $sSndDirName = $szDrive & $szDir EndIf $sSndFileName = $szFName & $szExt $sSndDirShortName = FileGetShortName($sSndDirName, 1) ;open file $iRet = __SoundMciSendString("open " & FileGetShortName($sFile) & " alias " & $aSndID[0]) If $iRet <> 0 Then Return SetError(1, $iRet, 0) ; open failed $oShell = ObjCreate("shell.application") If IsObj($oShell) Then $oShellDir = $oShell.NameSpace($sSndDirShortName) If IsObj($oShellDir) Then $oShellDirFile = $oShellDir.Parsename($sSndFileName) If IsObj($oShellDirFile) Then $sRaw = $oShellDir.GetDetailsOf($oShellDirFile, -1) $aInfo = StringRegExp($sRaw, ": ([0-9]{2}:[0-9]{2}:[0-9]{2})", 3) If Not IsArray($aInfo) Then $fTryNextMethod = True Else $sTrackLength = $aInfo[0] EndIf Else $fTryNextMethod = True EndIf Else $fTryNextMethod = True EndIf Else $fTryNextMethod = True EndIf If $fTryNextMethod Then $fTryNextMethod = False If $szExt = ".mp3" Then $hFile = FileOpen(FileGetShortName($sSndDirName & $sSndFileName), 4) $sTag = FileRead($hFile, 5156) FileClose($hFile) $sTrackLength = __SoundReadXingFromMP3($sTag) If @error Then $fTryNextMethod = True Else $fTryNextMethod = True EndIf EndIf If $fTryNextMethod Then $fTryNextMethod = False If $szExt = ".mp3" Then $sTrackLength = __SoundReadTLENFromMP3($sTag) If @error Then $fTryNextMethod = True Else $fTryNextMethod = True EndIf EndIf FileClose($hFile) If $fTryNextMethod Then $fTryNextMethod = False ;tell mci to use time in milliseconds __SoundMciSendString("set " & $aSndID[0] & " time format miliseconds") ;receive length of sound $iSndLenMs = __SoundMciSendString("status " & $aSndID[0] & " length", 255) ;assign modified data to variables __SoundTicksToTime($iSndLenMs, $iSndLenHour, $iSndLenMin, $iSndLenSecs) ;assign formatted data to $sSndLenFormat $sTrackLength = StringFormat("%02i:%02i:%02i", $iSndLenHour, $iSndLenMin, $iSndLenSecs) EndIf ; Convert Track_Length to mSec $aiTime = StringSplit($sTrackLength, ":") $iActualTicks = __SoundTimeToTicks($aiTime[1], $aiTime[2], $aiTime[3]) ;tell mci to use time in milliseconds __SoundMciSendString("set " & $aSndID[0] & " time format miliseconds") ;;Get estimated length $iSoundTicks = __SoundMciSendString("status " & $aSndID[0] & " length", 255) ;Compare to actual length If Abs($iSoundTicks - $iActualTicks) < 1000 Then ;Assume CBR, as our track length from shell.application is only accurate within 1000ms $iVBRRatio = 0 Else ;Set correction ratio for VBR operations $iVBRRatio = $iSoundTicks / $iActualTicks EndIf $aSndID[1] = $iVBRRatio $aSndID[2] = 0 $aSndID[3] = $__SOUNDCONSTANT_SNDID_MARKER Return SetError(0, $iRet, $aSndID) EndFunc ;==>_SoundOpen ; #FUNCTION# ==================================================================================================================== ; Name...........: _SoundClose ; Description ...: Closes a sound ; Syntax.........: _SoundClose($aSndID) ; Parameters ....: $aSndID - Sound ID returned by _SoundOpen() ; Return values..: Success - 1 ; Failure - 0 and set @error ; @error 1 - Close failed ; 3 - Invalid Sound ID ; Author ........: RazerM, Melba23 ; Modified.......: ; Remarks .......: ; Related .......: ; Link ..........: ; Example .......: Yes ; =============================================================================================================================== Func _SoundClose($aSndID) If Not IsArray($aSndID) Or Not __SoundChkSndID($aSndID) Then Return SetError(3, 0, 0) ; invalid sound ID If __SoundMciSendString("close " & $aSndID[0]) = 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) ; Syntax.........:_SoundPlay($aSndID[, $iWait = 0]) ; Parameters ....: $aSndID - Sound ID returned by _SoundOpen() or sound file ; $iWait - If set to 1 the script will wait for the sound to finish before continuing ; Return values..: Success - 1 ; Failure - 0 and set @error ; @error 1 - Play failed ; 2 - Invalid $iWait parameter ; 3 - Invalid Sound ID or file name ; Author ........: RazerM, Melba23 ; Modified.......: ; Remarks .......: ; Related .......: _SoundPause, _SoundStop, _SoundSeek, _SoundOpen, _SoundResume ; Link ..........: ; Example .......: Yes ; =============================================================================================================================== Func _SoundPlay($aSndID, $iWait = 0) ;Declare variables Local $iRet ;validate $fWait If $iWait <> 0 And $iWait <> 1 Then Return SetError(2, 0, 0) ; invalid $iWait parameter If Not __SoundChkSndID($aSndID) Then Return SetError(3, 0, 0) ; invalid Sound ID or file name ;if sound has finished, seek to start If _SoundPos($aSndID, 2) = _SoundLength($aSndID, 2) Then __SoundMciSendString("seek " & $aSndID[0] & " to start") ;If $fWait = 1 then pass wait to mci If $iWait = 1 Then $iRet = __SoundMciSendString("play " & $aSndID[0] & " wait") Else $iRet = __SoundMciSendString("play " & $aSndID[0]) EndIf ;return If $iRet = 0 Then Return 1 Else Return SetError(1, 0, 0) EndIf EndFunc ;==>_SoundPlay ; #FUNCTION# ==================================================================================================================== ; Name...........: _SoundStop ; Description ...: Stops the sound ; Syntax.........: _SoundStop(ByRef $aSndID) ; Parameters ....: $aSndID - Sound ID returned by _SoundOpen() or sound file (must be a variable) ; Return values..: Success - 1 ; Failure - 0 and set @error ; @error 1 - Stop failed ; 3 - Invalid Sound ID or file name ; Author ........: RazerM, Melba23 ; Modified.......: ; Remarks .......: ; Related .......: ; Link ..........: ; Example .......: Yes ; =============================================================================================================================== Func _SoundStop(ByRef $aSndID) ;Declare variables Local $iRet, $iRet2, $vTemp ; create temp variable so file name variable is not changed ByRef $vTemp = $aSndID If Not __SoundChkSndID($vTemp) Then Return SetError(3, 0, 0) ; invalid Sound ID or file name ;seek to start $iRet = __SoundMciSendString("seek " & $vTemp[0] & " to start") ;stop $iRet2 = __SoundMciSendString("stop " & $vTemp[0]) ;reset VBR factor if used If IsArray($aSndID) Then $aSndID[2] = 0 ;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 ; Syntax.........: _SoundPause($aSndID) ; Parameters ....: $aSndID - Sound ID returned by _SoundOpen() or sound file ; Return values..: Success - 1 ; Failure - 0 and sets @error ; @error 1 - Pause failed ; 3 - Invalid Sound ID or file name ; Author ........: RazerM, Melba23 ; Modified.......: ; Remarks .......: ; Related .......: _SoundResume, _SoundOpen, _SoundPlay ; Link ..........: ; Example .......: Yes ; =============================================================================================================================== Func _SoundPause($aSndID) ;Declare variables Local $iRet If Not __SoundChkSndID($aSndID) Then Return SetError(3, 0, 0) ; invalid Sound ID or file name ;pause sound $iRet = __SoundMciSendString("pause " & $aSndID[0]) ;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 ; Syntax.........: _SoundResume($aSndID) ; Parameters ....: $aSndID - Sound ID returned by _SoundOpen() or sound file ; Return values..: Success - 1 ; Failure - 0 and set @error ; @error 1 - Resume failed ; 3 - Invalid Sound ID or file name ; Author ........: RazerM, Melba23 ; Modified.......: ; Remarks .......: ; Related .......: ; Link ..........: ; Example .......: Yes ; =============================================================================================================================== Func _SoundResume($aSndID) ;Declare variables Local $iRet If Not __SoundChkSndID($aSndID) Then Return SetError(3, 0, 0) ; invalid Sound ID or file name ;resume sound $iRet = __SoundMciSendString("resume " & $aSndID[0]) ;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 ; Syntax.........: _SoundLength($aSndID[, $iMode = 1]) ; Parameters ....: $aSndID - Sound ID returned by _SoundOpen() or sound file, ; $iMode = 1 - hh:mm:ss, $iMode = 2 - milliseconds ; Return values .: Success - Length of the sound ; Failure - 0 and set @error ; @error 1 - Invalid $iMode parameter ; 3 - Invalid Sound ID or file name ; Author ........: RazerM, Melba23 ; Modified.......: jpm ; Remarks .......: ; Related .......: ; Link ..........: ; Example .......: Yes ; =============================================================================================================================== Func _SoundLength($aSndID, $iMode = 1) ;Declare variables Local $iSndLenMs, $bFile = False ;validate $iMode If $iMode <> 1 And $iMode <> 2 Then Return SetError(1, 0, 0) If Not IsArray($aSndID) Then If Not FileExists($aSndID) Then Return SetError(3, 0, 0) ; invalid file name $bFile = True $aSndID = _SoundOpen($aSndID) Else If Not __SoundChkSndID($aSndID) Then Return SetError(3, 0, 0) ; invalid Sound ID EndIf ;tell mci to use time in milliseconds __SoundMciSendString("set " & $aSndID[0] & " time format miliseconds") ;receive length of sound $iSndLenMs = Number(__SoundMciSendString("status " & $aSndID[0] & " length", 255)) If $aSndID[1] <> 0 Then $iSndLenMs = Round($iSndLenMs / $aSndID[1]) If $bFile Then _SoundClose($aSndID) ;if user called _SoundLength with a filename If $iMode = 2 Then Return $iSndLenMs ; $iMode = 1 (hh:mm:ss) Local $iSndLenMin, $iSndLenHour, $iSndLenSecs ;assign modified data to variables __SoundTicksToTime($iSndLenMs, $iSndLenHour, $iSndLenMin, $iSndLenSecs) ;assign formatted data to $sSndLenFormat Local $sSndLenFormat = StringFormat("%02i:%02i:%02i", $iSndLenHour, $iSndLenMin, $iSndLenSecs) ;return correct variable Return $sSndLenFormat EndFunc ;==>_SoundLength ; #FUNCTION# ==================================================================================================================== ; Name...........: _SoundSeek ; Description ...: Seeks the sound to a specified time ; Syntax.........: _SoundSeek(ByRef $aSndID, $iHour, $iMin, $iSec) ; Parameters ....: $aSndID - Sound ID returned by _SoundOpen() (must NOT be a file), $iHour, $iMin, $iSec ; Return values..: Success - 1 ; Failure - 0 and set @error ; @error 1 - Seek failed ; 3 - Invalid Sound ID ; Author ........: RazerM, Melba23 ; Modified.......: ; Remarks .......: ; Related .......: ; Link ..........: ; Example .......: Yes ; =============================================================================================================================== Func _SoundSeek(ByRef $aSndID, $iHour, $iMin, $iSec) ;Declare variables Local $iMs = 0, $iRet If Not IsArray($aSndID) Or Not __SoundChkSndID($aSndID) Then Return SetError(3, 0, 0) ; invalid Sound ID ;prepare mci to receive time in milliseconds __SoundMciSendString("set " & $aSndID[0] & " 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 If $aSndID[1] <> 0 Then $aSndID[2] = Round($iMs * $aSndID[1]) - $iMs $iMs = Round($iMs * $aSndID[1]) EndIf ; seek sound to time ($iMs) $iRet = __SoundMciSendString("seek " & $aSndID[0] & " to " & $iMs) If _SoundPos($aSndID, 2) < 0 Then $aSndID[2] = 0 ;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. ; Syntax.........: _SoundStatus($aSndID) ; Parameters ....: $aSndID - Sound ID returned by _SoundOpen() or sound file ; Return values..: Success - Sound status ; Failure - 0 and set @error ; @error 3 - Invalid Sound ID or file name ; Author ........: RazerM, Melba23 ; Modified.......: ; Remarks .......: Some devices can return the additional "open", "parked", "recording", and "seeking" values.(MSDN) ; Related .......: ; Link ..........: ; Example .......: Yes ; =============================================================================================================================== Func _SoundStatus($aSndID) If Not __SoundChkSndID($aSndID) Then Return SetError(3, 0, 0) ; invalid Sound ID or file name ;return status Return __SoundMciSendString("status " & $aSndID[0] & " mode", 255) EndFunc ;==>_SoundStatus ; #FUNCTION# ==================================================================================================================== ; Name...........: _SoundPos ; Description ...: Returns the current position of the song ; Syntax.........: _SoundPos($aSndID[, $iMode = 1]) ; Parameters ....: $aSndID - Sound ID returned by _SoundOpen() or sound file, ; $iMode = 1 - hh:mm:ss, $iMode = 2 - milliseconds ; Return values..: Success - Current position ; Failure - 0 and set @error ; @error 1 - Invalid $iMode ; |3 - Invalid Sound ID or file name ; Author ........: RazerM, Melba23 ; Modified.......: ; Remarks .......: ; Related .......: ; Link ..........: ; Example .......: Yes ; =============================================================================================================================== Func _SoundPos($aSndID, $iMode = 1) ;Declare variables Local $iSndPosMs ;validate $iMode If $iMode <> 1 And $iMode <> 2 Then Return SetError(1, 0, 0) If Not __SoundChkSndID($aSndID) Then Return SetError(3, 0, 0) ; invalid Sound ID or file name ;tell mci to use time in milliseconds __SoundMciSendString("set " & $aSndID[0] & " time format miliseconds") ;receive position of sound $iSndPosMs = Number(__SoundMciSendString("status " & $aSndID[0] & " position", 255)) If $aSndID[1] <> 0 Then $iSndPosMs -= $aSndID[2] EndIf If $iMode = 2 Then Return $iSndPosMs ;$iMode = 1 (hh:mm:ss) Local $iSndPosMin, $iSndPosHour, $iSndPosSecs ;modify data and assign to variables __SoundTicksToTime($iSndPosMs, $iSndPosHour, $iSndPosMin, $iSndPosSecs) ;assign formatted data to $sSndPosFormat Local $sSndPosHMS = StringFormat("%02i:%02i:%02i", $iSndPosHour, $iSndPosMin, $iSndPosSecs) ;return correct variable Return $sSndPosHMS EndFunc ;==>_SoundPos ; #INTERNAL_USE_ONLY# =========================================================================================================== ; Name...........: __SoundChkSndID ; Description ...: Used internally within this file, not for general use ; Syntax.........: __SoundChkSndID(ByRef $aSndID, $bInit=False, $iPos=Default) ; Author ........: jpm ; Modified.......: Melba23 ; Remarks .......: ; Related .......: ; Link ..........: ; Example .......: ; =============================================================================================================================== Func __SoundChkSndID(ByRef $aSndID) Local $vTemp If Not IsArray($aSndID) Then If Not FileExists($aSndID) Then Return 0 ; invalid Sound file $vTemp = FileGetShortName($aSndID) Dim $aSndID[4] = [$vTemp, 0, 0, $__SOUNDCONSTANT_SNDID_MARKER] ; create valid Sound ID array for use in UDF Else If UBound($aSndID) <> 4 And $aSndID[3] <> $__SOUNDCONSTANT_SNDID_MARKER Then Return 0 ; invalid Sound ID EndIf Return 1 EndFunc ;==>__SoundChkSndID ; #INTERNAL_USE_ONLY# =========================================================================================================== ; Name...........: __SoundMciSendString ; Description ...: Used internally within this file, not for general use ; Syntax.........: __SoundMciSendString($string[, $iLen = 0]) ; Author ........: RazerM, Melba23 ; Modified.......: ; Related .......: ; Link ..........: ; Example .......: ; =============================================================================================================================== Func __SoundMciSendString($string, $iLen = 0) Local $iRet $iRet = DllCall("winmm.dll", "int", "mciSendStringA", "str", $string, "str", "", "long", $iLen, "long", 0) If Not @error Then Return $iRet[2] EndFunc ;==>__SoundMciSendString ; #INTERNAL_USE_ONLY# =========================================================================================================== ; Name...........: __SoundReadTLENFromMP3 ; Description ...: Used internally within this file, not for general use ; Syntax.........: __SoundReadTLENFromMP3($sTag) ; Parameters ....: $sTag - >= 1024 bytes from 'read raw' mode. ; Return values .: Success - Sound length (hh:mm:ss) ; Failure - 0 and @error = 1 ; Author ........: Melba23 ; Modified.......: RazerM ; Remarks .......: File must be an mp3 AFAIK ; Related .......: ; Link ..........: ; Example .......: ; =============================================================================================================================== Func __SoundReadTLENFromMP3($sTag) Local $iTemp, $sTemp, $iLengthMs, $iLengthHour, $iLengthMin, $iLengthSecs ; Check that an ID3v2.3 tag is present If StringLeft($sTag, 10) <> "0x49443303" Then Return SetError(1, 0, 0) $iTemp = StringInStr($sTag, "544C454E") + 21 $sTag = StringTrimLeft($sTag, $iTemp) $sTemp = "" For $i = 1 To 32 Step 2 If StringMid($sTag, $i, 2) = "00" Then ExitLoop Else $sTemp &= StringMid($sTag, $i, 2) EndIf Next $iLengthMs = Number(__SoundHexToString($sTemp)) If $iLengthMs > 0 Then __SoundTicksToTime($iLengthMs, $iLengthHour, $iLengthMin, $iLengthSecs) ;Convert to hh:mm:ss and return Return StringFormat("%02i:%02i:%02i", $iLengthHour, $iLengthMin, $iLengthSecs) Else Return SetError(1, 0, 0) EndIf EndFunc ;==>__SoundReadTLENFromMP3 ; #INTERNAL_USE_ONLY# =========================================================================================================== ; Name...........: __SoundReadXingFromMP3 ; Syntax.........: __SoundReadXingFromMP3($sTag) ; Parameters ....: $sTag - first 5156 bytes from 'read raw' mode. ; Return values .: Success - Sound length (hh:mm:ss) ; Failure - 0 and @error = 1 ; Author ........: Melba23 ; Modified.......: RazerM ; Remarks .......: File must be an mp3 AFAIK ; Related .......: ; Link ..........: ; Example .......: ; =============================================================================================================================== Func __SoundReadXingFromMP3($sTag) Local $iXingPos, $iFlags, $iFrames, $sHeader, $iMPEGByte, $iFreqByte, $iMPEGVer, $iLayerNum, $iSamples, $iFreqNum, $iFrequency, $iLengthMs, $iLengthHours, $iLengthMins, $iLengthSecs $iXingPos = StringInStr($sTag, "58696E67") If $iXingPos = 0 Then Return SetError(1, 0, 0) ; Read fields flag $iFlags = Number("0x" & StringMid($sTag, $iXingPos + 14, 2)) If BitAND($iFlags, 1) = 1 Then $iFrames = Number("0x" & StringMid($sTag, $iXingPos + 16, 8)) Else Return SetError(1, 0, 0); No frames field EndIf ; Now to find Samples per frame & Sampling rate ; Go back to the frame header start $sHeader = StringMid($sTag, $iXingPos - 72, 8) ; Read the relevant bytes $iMPEGByte = Number("0x" & StringMid($sHeader, 4, 1)) $iFreqByte = Number("0x" & StringMid($sHeader, 6, 1)) ; Decode them ; 8 = MPEG-1, 0 = MPEG-2 $iMPEGVer = BitAND($iMPEGByte, 8) ; 2 = Layer III, 4 = Layer II, 6 = Layer I $iLayerNum = BitAND($iMPEGByte, 6) Switch $iLayerNum Case 6 $iSamples = 384 Case 4 $iSamples = 1152 Case 2 Switch $iMPEGVer Case 8 $iSamples = 1152 Case 0 $iSamples = 576 Case Else $iSamples = 0 EndSwitch Case Else $iSamples = 0 EndSwitch ; If not valid return If $iSamples = 0 Then Return SetError(1, 0, 0) ; 0 = bit 00, 4 = Bit 01, 8 = Bit 10 $iFreqNum = BitAND($iFreqByte, 12) Switch $iFreqNum Case 0 $iFrequency = 44100 Case 4 $iFrequency = 48000 Case 8 $iFrequency = 32000 Case Else $iFrequency = 0 EndSwitch ; If not valid return If $iFrequency = 0 Then Return SetError(1, 0, 0) ; MPEG-2 halves the value If $iMPEGVer = 0 Then $iFrequency = $iFrequency / 2 ; Duration in secs = No of frames * Samples per frame / Sampling freq $iLengthMs = Int(($iFrames * $iSamples / $iFrequency) * 1000) ; Convert to hh:mm:ss and return __SoundTicksToTime($iLengthMs, $iLengthHours, $iLengthMins, $iLengthSecs) Return StringFormat("%02i:%02i:%02i", $iLengthHours, $iLengthMins, $iLengthSecs) EndFunc ;==>__SoundReadXingFromMP3 ; #INTERNAL_USE_ONLY# =========================================================================================================== ; Name...........: _HexToString ; Description ...: Convert a hex string to a string. ; Syntax.........: _HexToString($strHex) ; Parameters ....: $strHex - an hexadecimal string ; Return values .: Success - Returns a string. ; Failure - Returns -1 and sets @error to 1. ; Author ........: Jarvis Stubblefield ; Modified.......: SmOke_N - (Re-write using BinaryToString for speed) ; Remarks .......: ; Related .......: _StringToHex ; Link ..........: ; Example .......: Yes ; =============================================================================================================================== Func __SoundHexToString($strHex) If StringLeft($strHex, 2) = "0x" Then Return BinaryToString($strHex) Return BinaryToString("0x" & $strHex) EndFunc ;==>__SoundHexToString ; #INTERNAL_USE_ONLY# =========================================================================================================== ; Name...........: _TicksToTime ; Description ...: Converts the specified tick amount to hours, minutes and seconds. ; Syntax.........: _TicksToTime($iTicks, ByRef $iHours, ByRef $iMins, ByRef $iSecs) ; Parameters ....: $iTicks - Tick amount. ; $iHours - Variable to store the hours. ; $iMins - Variable to store the minutes. ; $iSecs - Variable to store the seconds. ; Return values .: Success - 1 ; Failure - 0 ; @Error - 0 - No error. ; |1 - $iTicks isn't an integer. ; Author ........: Marc <mrd at gmx de> ; Modified.......: ; Remarks .......: ; Related .......: __SoundTimeToTicks ; Link ..........: ; Example .......: Yes ; =============================================================================================================================== Func __SoundTicksToTime($iTicks, ByRef $iHours, ByRef $iMins, ByRef $iSecs) If Number($iTicks) > 0 Then $iTicks = Round($iTicks / 1000) $iHours = Int($iTicks / 3600) $iTicks = Mod($iTicks, 3600) $iMins = Int($iTicks / 60) $iSecs = Round(Mod($iTicks, 60)) ; If $iHours = 0 then $iHours = 24 Return 1 ElseIf Number($iTicks) = 0 Then $iHours = 0 $iTicks = 0 $iMins = 0 $iSecs = 0 Return 1 Else Return SetError(1,0,0) EndIf EndFunc ;==>__SoundTicksToTime ; #INTERNAL_USE_ONLY# =========================================================================================================== ; Name...........: _TimeToTicks ; Description ...: Converts the specified hours, minutes, and seconds to ticks. ; Syntax.........: _TimeToTicks([$iHours = @HOUR[, $iMins = @MIN[, $iSecs = @SEC]]]) ; Parameters ....: $iHours - The hours. ; $iMins - The minutes. ; $iSecs - The seconds. ; Return values .: Success - Returns the number of ticks. ; Failure - 0 ; @Error - 0 - No error. ; |1 - The specified hours, minutes, or seconds are not valid. ; Author ........: Marc <mrd at gmx de> ; Modified.......: SlimShady: added the default time and made parameters optional ; Remarks .......: ; Related .......: _TicksToTime ; Link ..........: ; Example .......: Yes ; =============================================================================================================================== Func __SoundTimeToTicks($iHours = @HOUR, $iMins = @MIN, $iSecs = @SEC) ;============================================== ; Local Constant/Variable Declaration Section ;============================================== Local $iTicks If StringIsInt($iHours) And StringIsInt($iMins) And StringIsInt($iSecs) Then $iTicks = 1000 * ((3600 * $iHours) + (60 * $iMins) + $iSecs) Return $iTicks Else Return SetError(1,0,0) EndIf EndFunc ;==>__SoundTimeToTicks When the new stable version of Autoit is released, the standard Sound.au3 include will be updated to this version - at least that is the plan! M23 Link to comment Share on other sites More sharing options...
Developers Jos Posted July 25, 2009 Developers Share Posted July 25, 2009 Have a look in the other thread you have going for the answer this this question. You have a Func .. endfunc inside another Func..EndFunc. Jos SciTE4AutoIt3 Full installer Download page - Beta files Read before posting How to post scriptsource Forum etiquette Forum Rules Live for the present, Dream of the future, Learn from the past. Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted July 25, 2009 Moderators Share Posted July 25, 2009 ajhhanekamp,Take a look at this code. The HotKey is not needed because your GUI code will exit the script. The "stopped" check is now inside your GUIGetMsg loop.expandcollapse popup#include <GUIConstantsEx.au3> #include <ButtonConstants.au3> #include <Sound_Beta.au3> Opt('MustDeclareVars', 1) Global $vSndID Example() Func Example() Local $Button_1, $Button_2, $Button_3, $Button_4, $msg, $n Local $GUI_DISABLE $vSndID = _SoundOpen("C:\Pangya - caramelldansen.mp3") GUICreate("Games & Programs Launcher", 800, 600) GUICtrlCreatePic('C:\Fire.jpg', 0, 0, @DesktopWidth, @DesktopHeight) GUICtrlSetState(1, $GUI_DISABLE) $n = GUICtrlCreatePic("C:\games.jpg", 380, 10, 120, 120) Opt("GUICoordMode", 2) $Button_1 = GUICtrlCreateButton("Command and Conquer Generals", -480, 15, 170, 50) $Button_2 = GUICtrlCreateButton("Command and Conquer Generals Zero: Hour", 140, -50, 220, 50) $Button_3 = GUICtrlCreateButton("NTreev USA Pangya", 110, -50, 120, 50) $n = GUICtrlCreatePic("C:\programs.jpg", -490, 30, 300, 100) $Button_4 = GUICtrlCreateButton("Microsoft Office Word 2003", -570, 15, 170, 50) GUISetState() While 1 $msg = GUIGetMsg() Select Case $msg = $GUI_EVENT_CLOSE ExitLoop Case $msg = $Button_1 Run("E:\Command and Conquer Generals\generals.exe") Case $msg = $Button_2 Run("E:\Command & Conquer Generals Zero Hour\generals.exe") Case $msg = $Button_3 Run("E:\Ntreev USA\Pangya\update.exe") Case $msg = $Button_4 Run("C:\Program Files\Microsoft Office\OFFICE11\WINWORD.EXE") EndSelect If _SoundStatus($vSndID) = "stopped" Then _SoundPlay($vSndID) WEnd EndFunc ;==>Example _SoundStop($vSndID) MsgBox(64, 'Notification', 'Games & Programs Launcher will close now') ExitOne more thing - do you understand what Jos was saying about nested function definitions?M23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
ajhhanekamp Posted July 25, 2009 Author Share Posted July 25, 2009 Yes i do understand that i had a func inside another. Now it indeed plays the sound again after it has stopped, however i still can not click any buttons... somehow i think the background image is set to foreground? ajhhanekamp, Take a look at this code. The HotKey is not needed because your GUI code will exit the script. The "stopped" check is now inside your GUIGetMsg loop. expandcollapse popup#include <GUIConstantsEx.au3> #include <ButtonConstants.au3> #include <Sound_Beta.au3> Opt('MustDeclareVars', 1) Global $vSndID Example() Func Example() Local $Button_1, $Button_2, $Button_3, $Button_4, $msg, $n Local $GUI_DISABLE $vSndID = _SoundOpen("C:\Pangya - caramelldansen.mp3") GUICreate("Games & Programs Launcher", 800, 600) GUICtrlCreatePic('C:\Fire.jpg', 0, 0, @DesktopWidth, @DesktopHeight) GUICtrlSetState(1, $GUI_DISABLE) $n = GUICtrlCreatePic("C:\games.jpg", 380, 10, 120, 120) Opt("GUICoordMode", 2) $Button_1 = GUICtrlCreateButton("Command and Conquer Generals", -480, 15, 170, 50) $Button_2 = GUICtrlCreateButton("Command and Conquer Generals Zero: Hour", 140, -50, 220, 50) $Button_3 = GUICtrlCreateButton("NTreev USA Pangya", 110, -50, 120, 50) $n = GUICtrlCreatePic("C:\programs.jpg", -490, 30, 300, 100) $Button_4 = GUICtrlCreateButton("Microsoft Office Word 2003", -570, 15, 170, 50) GUISetState() While 1 $msg = GUIGetMsg() Select Case $msg = $GUI_EVENT_CLOSE ExitLoop Case $msg = $Button_1 Run("E:\Command and Conquer Generals\generals.exe") Case $msg = $Button_2 Run("E:\Command & Conquer Generals Zero Hour\generals.exe") Case $msg = $Button_3 Run("E:\Ntreev USA\Pangya\update.exe") Case $msg = $Button_4 Run("C:\Program Files\Microsoft Office\OFFICE11\WINWORD.EXE") EndSelect If _SoundStatus($vSndID) = "stopped" Then _SoundPlay($vSndID) WEnd EndFunc ;==>Example _SoundStop($vSndID) MsgBox(64, 'Notification', 'Games & Programs Launcher will close now') Exit One more thing - do you understand what Jos was saying about nested function definitions? M23 Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted July 25, 2009 Moderators Share Posted July 25, 2009 ajhhanekamp,You have declared $GUI_DISABLE as Local to the function, so it no longer has the correct value. Delete that line.You have also used 1 and not -1 in the GUIControlSetState line. Correct that.You create the picture control $n twice. I suggest removing one of them!Now it should all work. M23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now