Sign in to follow this  
Followers 0
Alek

A-Player

4 posts in this topic

#1 ·  Posted (edited)

a simple Autoit mp3 player.

bugs:

-Slider doesn't always redraw after resizing the window for some reason.

#include <GUIConstants.au3>
#Include <GuiListView.au3>
#include <Guilistbox.au3>
#include <Sound.au3>
#include <File.au3>
#NoTrayIcon
Opt("GUIOnEventMode", 1)


Global $Song = "0"
Global $Lenght
Global $Old_Selected
Global $Name_Prop = 10
Global $Artist_Prop = 16
Global $Genre_Prop = 20
Global $Album_Prop = 17
If @OSVersion = "WIN_VISTA" Then 
    $Name_Prop = 21
    $Artist_Prop = 13
    $Genre_Prop = 16
    $Album_Prop = 14
EndIf
Dim $Old_Pos = 0

If Not FileExists(@ScriptDir & "\Data.dat") Or Not FileExists(@ScriptDir & "\Info.dat") Then
    If MsgBox(36 ,"A-Player","Do you want A-Player to create you a index of all your mp3 files??") = 6 Then
        $Dir = FileSelectFolder("Select a directory to scan for MP3 files","")
        If $Dir <> "" Then
            _Create_Index($Dir, @ScriptDir & "\Data.dat")
            _Create_Info(@ScriptDir & "\Data.dat")
        EndIf
    EndIf
Else
    $Date = FileReadLine(@ScriptDir & "\Data.dat")
    $Date = StringSplit($Date, ":")
    If $Date[1] < @MDAY-2 Or $Date[2] < @MON Then
        If MsgBox(36 ,"A-Player","Do you want A-Player to create you a index of all your mp3 files??") = 6 Then
            $Dir = FileSelectFolder("Select a directory to scan for MP3 files","")
            If $Dir <> "" Then
                FileDelete(@ScriptDir & "\info.dat")
                _Create_Index($Dir, @ScriptDir & "\Data.dat")
                _Create_Info(@ScriptDir & "\Data.dat")
            EndIf
        EndIf
    EndIf
EndIf

$Taskbar_Gui = GUICreate("A-Player",20,20,-100,-100)
$Form1 = GUICreate("A-Player", 625, 350, -1,-1,$WS_BORDER+$WS_MAXIMIZEBOX+$WS_SIZEBOX+$WS_MINIMIZEBOX, $WS_EX_TOOLWINDOW, $Taskbar_Gui)
GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")
GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit")

$List1 = GUICtrlCreateList("",5,5,140,270)

$ListView1 = GUICtrlCreateListView("#|Name|Artist|Genre|Album", 150, 5, 465, 265)
$Slider1 = GUICtrlCreateSlider(5, 275, 611, 25)
$Slider2 = GUICtrlCreateSlider(520, 300, 100, 25)
$Button1 = GUICtrlCreateButton("4", 5, 300, 35, 22, 0)
$Button2 = GUICtrlCreateButton("<", 45, 300, 35, 22, 0)

$Label1 = GUICtrlCreateLabel("Loading...",257,300,110,22,$SS_SUNKEN)
$Label2 = GUICtrlCreateLabel("X", 510,300,20,20)

GUICtrlSetResizing($ListView1, 102)
GUICtrlSetResizing($List1, 290+64)
GUICtrlSetResizing($Slider1, 70+512)
GUICtrlSetResizing($Slider2, 68+768)
GUICtrlSetResizing($Button1, 66+768)
GUICtrlSetResizing($Button2, 66+768)
GUICtrlSetResizing($Label1, 64+768)
GUICtrlSetResizing($Label2, 68+768)

GUICtrlSetFont($Button1, 14, 800, 0, "Webdings")
GUICtrlSetFont($Button2, 14, 800, 0, "Webdings")
GUICtrlSetFont($Label1, 12, 800)
GUICtrlSetFont($Label2, 14, 800, 0, "Webdings")

GUICtrlSetLimit($Slider2, 1000, 0)
GUICtrlSetData($Slider2, 1000)

GUICtrlSetOnEvent($Button1, "_Play")
GUICtrlSetOnEvent($Button2, "_Stop")
GUICtrlSetOnEvent($Slider2, "_Volumcontrol")

Global $B_DESCENDING[_GUICtrlListView_GetColumnCount($ListView1)]

_GUICtrlListView_HideColumn($ListView1, 0)

GUISetState(@SW_SHOW, $Taskbar_Gui)
GUISetState(@SW_SHOW, $Form1)

Dim $Data = _LibGetLibs(@ScriptDir & "\Info.dat")
If Not @error Then
    For $x = 1 To $Data[0]
        GUICtrlSetData($List1, $Data[$x])
    Next
EndIf

AdlibEnable("_SongTime",1000)

DllCall("psapi.dll", 'int', 'EmptyWorkingSet', 'long', -1)

While 1
    If GUICtrlRead($List1) <> $Old_Selected Then
        $Old_Selected = GUICtrlRead($List1)
        $Data = _LibRead(@ScriptDir & "\info.dat",$Old_Selected)
        If Not @error Then
            _GUICtrlListView_DeleteAllItems(GUICtrlGetHandle($ListView1))
            For $x = 1 To $Data[0]
                If $Data[$x] = "" Or Not Int($Data[$x]) Then ContinueLoop
                $S_Song = FileReadLine(@ScriptDir & "\Data.dat",$Data[$x])
                GUICtrlCreateListViewItem($Data[$x] & "|" & _GetExtProperty($S_Song, $Name_Prop) & "|" & _GetExtProperty($S_Song,$Artist_Prop) & "|" & _GetExtProperty($S_Song,$Genre_Prop) &"|" & _GetExtProperty($S_Song,$Album_Prop), $ListView1)
            Next
        EndIf
    EndIf
    Sleep(10)
    _Searchcontrol()
WEnd

Func _Volumcontrol()
    mciSendString ("setaudio " & $Song & " output volume to " & GUICtrlRead($Slider2))
EndFunc

Func _Play()
    If GUICtrlRead($Button1) = "4" Then
        If $Song = "0" And GUICtrlRead(GUICtrlRead($ListView1)) <> "" Then
            $Song = StringSplit(GUICtrlRead(GUICtrlRead($ListView1)), "|")
            $Song = FileReadLine(@ScriptDir & "\data.dat",$Song[1])
            $Song = _SoundOpen($Song)
            $Lenght = TimeToSec(_Sound_Length($Song,1))
            GUICtrlSetLimit($Slider1,$Lenght)
            _Volumcontrol()
        EndIf
        _SoundPlay($Song)
        GUICtrlSetData($Button1, ";")
    Else
        _SoundPause($Song)
        GUICtrlSetData($Button1, "4")
    EndIf
EndFunc

Func _Stop()
    _SoundStop($Song)
    _SoundClose($Song)
    $Song = 0
    GUICtrlSetData($Button1, "4")
EndFunc

Func _Searchcontrol()
    Local $oldposx
    
    $nInfo = GUIGetCursorInfo($Form1)
    If $nInfo[4] = $Slider1 And WinActive($Form1)  And _IsPressed(01) Then
        AdlibDisable()
        _SoundPause($Song)
        Do
            $nInfo = GUIGetCursorInfo($Form1)
            If $nInfo[0] <> $oldposx Then
                _SoundSeek($Song, 0, 0, GUICtrlRead($Slider1))
                _SoundPlay($Song)
                Sleep(100)
                GUICtrlSetData($Label1, SecToTime(GUICtrlRead($Slider1)) & " / " & SecToTime($Lenght))
                $oldposx = $nInfo[0]
            Else
                Sleep(10)
            EndIf
        Until _IsPressed(01) = 0
        _SoundPlay($Song)
        AdlibEnable("_SongTime",1000)
    EndIf
    
EndFunc

Func _Create_Index($S_File_Path = "", $S_Index_File = -1)
    
    If $S_Index_File = -1 Then
        $S_Index_File = @ScriptDir & "\Index.dat"
    EndIf
    
    FileDelete($S_Index_File)
    FileWrite($S_Index_File,@MDAY & ":" & @MON & @CRLF)
    $S_Paths = $S_File_Path
    $File = FileOpen($S_Index_File,1)
    While  $S_Paths
       
        $S_File_Path = StringSplit($S_Paths,"|")
        $S_Paths = ""
        
        For $x = 1 To $S_File_Path[0]
            
            If $S_File_Path[$x] <> "" Then
               
                $S_First_File = FileFindFirstFile($S_File_Path[$x] & "\*.*")
               
                While 1
                    $S_Files = FileFindNextFile($S_First_File)
                    If @error Then ExitLoop
                    
                    If StringInStr(FileGetAttrib($S_File_Path[$x] & "\" & $S_Files), "D") Then
                        $S_Paths &= $S_File_Path[$x] & "\" & $S_Files & "|"
                    ElseIf StringRight($S_Files,3) = "mp3" Then
                        If FileGetSize($S_File_Path[$x] & "\" & $S_Files)/1024 > 1000 Then
                            FileWrite($File,$S_File_Path[$x] & "\" & $S_Files & @CRLF)
                        EndIf
                    EndIf
                WEnd
            EndIf
        Next
    Wend
    
    FileClose($File)
    Return 1
EndFunc

Func _SongTime()
    $Pos = _SoundPos($Song,2)/1000
    If $Pos <> $Old_Pos Then
        $Old_Pos = $Pos
        GUICtrlSetData($Slider1, $Pos)
        GUICtrlSetData($Label1, SecToTime($Pos) & " / " & SecToTime($Lenght))
    EndIf   
    
    If $Lenght > 0 And $Pos >= $Lenght And _SoundStatus($Song) = "stopped" Then
        Local $Sel_Index = _GUICtrlListView_GetSelectedIndices($ListView1)
        _Stop()
        If  $Sel_Index = _GUICtrlListView_GetItemCount($ListView1)-1 Then
            _GUICtrlListView_SetItemSelected($ListView1,0)
        Else
            _GUICtrlListView_SetItemSelected($ListView1,$Sel_Index+1)
        EndIf
        _Play()
        $Old_Pos = 0
    EndIf
EndFunc

Func TimeToSec($s_timer)
    Local $s_time = StringSplit($s_timer,":")
    If $s_time[0] <> 3 Then Return 0
    Return ($s_time[1]*3600)+($s_time[2]*60)+$s_time[3]
EndFunc

Func SecToTime($s_timer)
    Local $s_Min
    Local $s_Sec = Floor($s_timer)
    
    While $s_Sec > 60
        Select
            Case $s_Sec > 3599
                $s_Min += 60
                $s_Sec -= 3600
            Case $s_Sec > 599
                $s_Min += 10
                $s_Sec -= 600
            Case $s_Sec > 59
                $s_Min += 1
                $s_Sec -= 60
        EndSelect
    WEnd
    
    Return StringFormat('%02d:%02d',  $s_Min, $s_Sec)
EndFunc

;########################################
;_GetExtPrperty
; Author(s): Simucal (Simucal@gmail.com)
;Changed a bit to work better with my script.
;Returns "" insted of 0 on error
;########################################
Func _GetExtProperty($sPath, $iProp)
    Local $iExist, $sFile, $sDir, $oShellApp, $oDir, $oFile, $aProperty, $sProperty
    $iExist = FileExists($sPath)
    If $iExist = 0 Then
        SetError(1)
        Return ""
    Else
        $sFile = StringTrimLeft($sPath, StringInStr($sPath, "\", 0, -1))
        $sDir = StringTrimRight($sPath, (StringLen($sPath) - StringInStr($sPath, "\", 0, -1)))
        $oShellApp = ObjCreate ("shell.application")
        $oDir = $oShellApp.NameSpace ($sDir)
        $oFile = $oDir.Parsename ($sFile)
        If $iProp = -1 Then
            Local $aProperty[35]
            For $i = 0 To 34
                $aProperty[$i] = $oDir.GetDetailsOf ($oFile, $i)
            Next
            Return $aProperty
        Else
            $sProperty = $oDir.GetDetailsOf ($oFile, $iProp)
            If $sProperty = "" Then
                Return ""
            Else
                Return $sProperty
            EndIf
        EndIf
    EndIf
EndFunc

Func WM_NOTIFY($hWnd, $MsgID, $wParam, $lParam)
    Local $tagNMHDR, $event, $hwndFrom, $code
    $tagNMHDR = DllStructCreate("int;int;int", $lParam)
    If @error Then Return 0
    $code = DllStructGetData($tagNMHDR, 3)
    If $wParam = $ListView1 And $code = -3 Then
        _Stop()
        _Play()
    ElseIf $wParam = $ListView1 And $code = $LVN_COLUMNCLICK Then
        Local $tInfo = DllStructCreate($tagNMLISTVIEW, $lParam)
        _GUICtrlListView_SimpleSort ($ListView1, $B_DESCENDING, DllStructGetData($tInfo, "SubItem"))
    EndIf
    Return $GUI_RUNDEFMSG
EndFunc

Func _Exit()
    Exit
EndFunc

Func _LibWrite($s_File, $s_Lib, $s_Info)
    Local $s_Data, $s_File_Data
    Local $s_Lines = _FileCountLines($s_File)
    
    For $x = 1 To $s_Lines
        If StringLeft(FileReadLine($s_File, $x), StringLen($s_Lib)) = $s_Lib Then
            $s_Data = FileReadLine($s_File, $x)
            $s_File_Data = FileRead($s_File)
            FileDelete($s_File)
            FileWrite($s_File, StringReplace($s_File_Data, $s_Data, $s_Data & Chr(1) & $s_Info))
            Return 1
        EndIf
    Next
    
    FileWriteLine($s_File,$s_Lib & Chr(1) & $s_Info)
    Return 0
EndFunc

Func _LibRead($s_File, $s_Lib)
    Local $s_Data
    Local $s_Lines = _FileCountLines($s_File)
    
    For $x = 1 To $s_Lines
        $s_Data = FileReadLine($s_File, $x)
        If StringLeft($s_Data , StringLen($s_Lib)) = $s_Lib Then
            $s_Data = StringSplit(StringReplace($s_Data ,$s_Lib & Chr(1), ""),Chr(1))
            Return $s_Data
        EndIf
    Next
    
    SetError(1)
    Return 0
EndFunc

Func _LibGetLibs($s_File)
    Local $s_Data[2] = [0,""]
    Local $s_File_Data
    Local $s_Lines = _FileCountLines($s_File)
    
    For $x = 1 To $s_Lines
        $s_File_Data = FileReadLine($s_File,$x)
        $s_Data[0] += 1
        $s_Data[UBound($s_Data)-1] = StringLeft($s_File_Data, StringInStr($s_File_Data,Chr(1)))
        ReDim $s_Data[UBound($s_Data)+1]
    Next
    
    Return $s_Data
EndFunc

Func _Sound_Length($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

Func _Create_Info($s_File)
    Local $s_Name ,$s_Artist, $s_Song
    Local $Song_On_List = 1
    Local $s_Songs = _FileCountLines($s_File)
    
    FileDelete(@ScriptDir & "\info.dat")
    
    If @error Then Return 0
    
    For $x = 1 To $s_Songs
        $s_Song = FileReadLine($s_File, $x)
        $s_Name = _GetExtProperty($s_Song,$Name_Prop)
        If $s_Name <> "" Then
            $s_Artist = _GetExtProperty($s_Song,$Artist_Prop)
            If $s_Artist = "" Then $s_Artist = "Other"
            _LibWrite(@ScriptDir & "\info.dat", $s_Artist, $x)
        EndIf
    Next
    
    Return 1
EndFunc
Edited by Alek

[font="Impact"]Never fear, I is here.[/font]

Share this post


Link to post
Share on other sites



Posible bug : when creating playlist, the name column shows the time instead of name and the artist and album columns are empty, and the genre column shows the artist ... :strange:

Nice app though !

Check my player, (it's in the signature), you can find some useful things :)

Share this post


Link to post
Share on other sites

Posible bug : when creating playlist, the name column shows the time instead of name and the artist and album columns are empty, and the genre column shows the artist ... :strange:

Nice app though !

Check my player, (it's in the signature), you can find some useful things :)

I'm guessing your running windows XP

if so then i feared that that would happen :s

hopefully the new script fixed the problem, i cant really test it properly, it worked great on my computer (vista).


[font="Impact"]Never fear, I is here.[/font]

Share this post


Link to post
Share on other sites

Oh i see them ...

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  
Followers 0