Jump to content

Sound.au3 vs. WMPlayer.ocx


CYCho
 Share

Recommended Posts

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

#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

#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 by CYCho
Link to comment
Share on other sites

  • 7 months later...

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
 Share

×
×
  • Create New...