CYCho Posted February 27, 2020 Share Posted February 27, 2020 (edited) When sound files have to be played, Sound.au3 UDF comes to our mind immediately. It is simple and easy to use. But WMPlayer.ocx has a lot more features: it can play video clips and flac audio files, which Sound.au3 UDF cannot. Below are quick and dirty comparison of codes utilizing two different methods. Both of them do exactly the same thing: play back a playlist of audio files, with a simple slider control. 1. Use of Sound.au3 UDF expandcollapse popup#include <GUIConstants.au3> #include <Sound.au3> #include <Misc.au3> $hGui = GUICreate("Sound.au3", 400, 75, -1, -1) $Label1 = GUICtrlCreateButton("", 0, 0, 400, 20) GUICtrlSetState(-1, $GUI_DISABLE) $Shuttle = GUICtrlCreatePic(StringReplace(@AutoItExe, "autoit3.exe", "\examples\gui\msoobe.jpg"), 0, 0, 20, 20) $Pause = GUICtrlCreateButton("Pause", 160, 40, 80, 20) $Progress = GUICtrlCreateLabel("0:00", 4, 25, 45, 20, $SS_LEFT) ; current media position $Length = GUICtrlCreateLabel("0:00", 350, 25, 45, 20, $SS_RIGHT) ; media length GUISetState(@SW_SHOW) Local $aPlayList[3] ; Put your own mp3 file paths in this playlist $aPlayList[0] = 2 ; number of files in the playlist $aPlayList[1] = "D:\Music - Classics\Beethoven - Piano Concerto 5 in Eb Major Allegro.mp3" $aPlayList[2] = "D:\Music - Pops\Adele - Hello.mp3" $nFiles = 0 For $i = 1 To $aPlayList[0] If Not FileExists($aPlayList[$i]) Then $nFiles += 1 EndIf Next If $nFiles = $aPlayList[0] Then MsgBox(0, "Sound.au3", "None of the files listed in $aPlayList array exists.", 5) GUIDelete($hGui) Exit EndIf $iPos = 0 ; x coordinate of $Shuttle cntrol in progress bar $hDLL = DllOpen("user32.dll") ; to dectect mouse down on the $Shuttle control $sliderLength = 380 ; in pixels $adlibInterval = 250 ; in milliseconds $nFile = 0 While 1 $nFile += 1 If $nFile > $aPlayList[0] Then $nFile = 1 EndIf $sFullPath = $aPlayList[$nFile] $aSound = _SoundOpen($sFullPath) If $aSound = 0 Then MsgBox(0, "Sound.au3", $sFullPath & @CRLF & @CRLF & "Cannot open this file.", 5) ContinueLoop EndIf _SoundPlay($aSound) $sFile = StringMid($sFullPath, StringInStr($sFullPath, "\", 0, -1)+1) GUICtrlSetData($Label1, $sFile) $mediaLength = _SoundLength($aSound, 2) / 1000 ; in seconds GUICtrlSetData($Length, Int($mediaLength/60) & ":" & StringFormat("%02i", Mod($mediaLength, 60))) AdlibRegister("Slider", $adlibInterval) While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE _SoundClose($aSound) Exit Case $Pause If GUICtrlRead($Pause) = "Pause" Then _SoundPause($aSound) AdlibUnRegister() GUICtrlSetData($Pause, "Resume") Else _SoundResume($aSound) AdlibRegister("Slider") GUICtrlSetData($Pause, "Pause") EndIf Case $Shuttle AdlibUnRegister() $x = MouseGetPos(0) $xOffset = $x - $iPos While _IsPressed("01", $hDLL) = 1 $x = MouseGetPos(0) If $x > 380+$xOffset Then $x = 380+$xOffset ElseIf $x < $xOffset Then $x = $xOffset EndIf $iPos = $x - $xOffset GUICtrlSetPos($Shuttle, $iPos) Sleep(1) WEnd $mediaPos = Round($iPos/$sliderLength*$mediaLength) $iHour = Int($mediaPos/3600) $iMin = Int(Mod($mediaPos, 3600)/60) $iSec = Mod($mediaPos, 60) _SoundSeek($aSound, $iHour, $iMin, $iSec) _SoundPlay($aSound) AdlibRegister("Slider", $adlibInterval) GUICtrlSetData($Pause, "Pause") EndSwitch If _SoundStatus($aSound) = "stopped" Then ; playing, paused, stopped ExitLoop EndIf WEnd WEnd Func Slider() $mediaPos = _SoundPos($aSound, 2)/1000 $iPos = $mediaPos * $sliderLength / $mediaLength GUICtrlSetPos($Shuttle, $iPos) GUICtrlSetData($Progress, Int($mediaPos/60) & ":" & StringFormat("%02i", Mod($mediaPos, 60))) EndFunc 2. Use of WMPlayer.ocx expandcollapse popup#include <GuiConstants.au3> #include <Misc.au3> $hGui = GUICreate("WMPlayer.OCX", 400, 75, -1, -1) $Label1 = GUICtrlCreateButton("", 0, 0, 400, 20) GUICtrlSetState(-1, $GUI_DISABLE) $Shuttle = GUICtrlCreatePic(StringReplace(@AutoItExe, "autoit3.exe", "examples\gui\msoobe.jpg"), 0, 0, 20, 20) $Pause = GUICtrlCreateButton("Pause", 160, 40, 80, 20) $Progress = GUICtrlCreateLabel("0:00", 4, 25, 45, 20, $SS_LEFT) ; current media position $Length = GUICtrlCreateLabel("0:00", 350, 25, 45, 20, $SS_RIGHT) ; media length GUISetState(@SW_SHOW) Local $aPlayList[3] ; Put your own mp3 file paths in this playlist $aPlayList[0] = 2 ; number of files in the playlist $aPlayList[1] = "D:\Music - Classics\Beethoven - Piano Concerto 5 in Eb Major Allegro.mp3" $aPlayList[2] = "D:\Music - Pops\Adele - Hello.mp3" $nFiles = 0 For $i = 1 To $aPlayList[0] If Not FileExists($aPlayList[$i]) Then $nFiles += 1 EndIf Next If $nFiles = $aPlayList[0] Then MsgBox(0, "WMPlayer.ocx", "None of the files listed in $aPlayList array exists.", 5) GUIDelete($hGui) Exit EndIf $oPlayer = ObjCreate("WMPlayer.OCX") If Not IsObj($oPlayer) Then MsgBox(0, "WMPlayer.OCX", "Cannot create a WMP object.", 5) GUIDelete($hGui) Exit EndIf $iPos = 0 ; x coordinate of $Shuttle cntrol in progress bar $hDLL = DllOpen("user32.dll") ; to dectect mouse down on the $Shuttle control $sliderLength = 380 ; in pixels $adlibInterval = 250 ; in milliseconds $oPlayer.Settings.Volume = 100 $nFile = 0 While 1 $nFile += 1 If $nFile > $aPlayList[0] Then $nFile = 1 EndIf $sFullPath = $aPlayList[$nFile] $oPlayer.URL = $sFullPath $hTimer = TimerInit() While $oPlayer.playState <> 3 ; 1 - stopped, 2 - paused, 3 - playing If TimerDiff($hTimer) > 3000 Then MsgBox(0, "WMPlayer.OCX", $sFullPath & @CRLF & @CRLF & "Cannot play this file.", 5) ExitLoop EndIf Sleep(5) WEnd If $oPlayer.playState <> 3 Then ContinueLoop EndIf $sFile = StringMid($sFullPath, StringInStr($sFullPath, "\", 0, -1)+1) GUICtrlSetData($Label1, $sFile) $mediaLength = Int($oPlayer.currentMedia.Duration) ; in seconds GUICtrlSetData($Length, Int($mediaLength/60) & ":" & StringFormat("%02i", Mod($mediaLength, 60))) AdlibRegister("Slider", $adlibInterval) While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE $oPlayer.Close Exit Case $Pause If GUICtrlRead($Pause) = "Pause" Then $oPlayer.Controls.Pause AdlibUnRegister() GUICtrlSetData($Pause, "Resume") Else $oPlayer.Controls.Play AdlibRegister("Slider") GUICtrlSetData($Pause, "Pause") EndIf Case $Shuttle AdlibUnRegister() $x = MouseGetPos(0) $xOffset = $x - $iPos While _IsPressed("01", $hDLL) = 1 $x = MouseGetPos(0) If $x > 380+$xOffset Then $x = 380+$xOffset ElseIf $x < $xOffset Then $x = $xOffset EndIf $iPos = $x - $xOffset GUICtrlSetPos($Shuttle, $iPos) Sleep(1) WEnd $mediaPos = $iPos/$sliderLength*$mediaLength $oPlayer.Controls.currentPosition = $mediaPos $oPlayer.Controls.Play AdlibRegister("Slider", $adlibInterval) GUICtrlSetData($Pause, "Pause") EndSwitch If $oPlayer.playState = 1 Then ExitLoop EndIf WEnd WEnd Func Slider() $mediaPos = $oPlayer.Controls.currentPosition $iPos = $mediaPos * $sliderLength / $mediaLength GUICtrlSetPos($Shuttle, $iPos) GUICtrlSetData($Progress, Int($mediaPos/60) & ":" & StringFormat("%02i", Mod($mediaPos, 60))) EndFunc Edited June 29, 2020 by CYCho pixelsearch and mLipok 1 1 zPlayer - A Small Audio and Video Player Time Sync + SystemTimeAdjustment Link to comment Share on other sites More sharing options...
nend Posted February 27, 2020 Share Posted February 27, 2020 Just use Bass UDF It's perfect for audio. Link to comment Share on other sites More sharing options...
CYCho Posted February 27, 2020 Author Share Posted February 27, 2020 @nend Thanks for your suggestion. The download link (http://www.signa5.com/autoit/BASS/BASS.zip) of the Bass UDF does not work. zPlayer - A Small Audio and Video Player Time Sync + SystemTimeAdjustment Link to comment Share on other sites More sharing options...
CYCho Posted February 29, 2020 Author Share Posted February 29, 2020 @mLipok Thanks for your "Like" reaction. zPlayer - A Small Audio and Video Player Time Sync + SystemTimeAdjustment Link to comment Share on other sites More sharing options...
pixelsearch Posted February 18, 2021 Share Posted February 18, 2021 Shame on me, I just realised that the original code is yours Well done @CYCho CYCho 1 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