Jump to content

Handy MusicPlayer + source


Jjo
 Share

Recommended Posts

Musicplayer which can be controlled by keys f2,f4,f6... and it can download music from youtube and convert it to mp3. Works with bass.dll.

Feel free to modify or use parts of the code.

.Rar download here

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_icon="icon.ico"
#AutoIt3Wrapper_outfile="Handy.exe"
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#include "GUIConstantsEx.au3"
#include "GuiListView.au3"
#include "bass.au3"
#include "inet.au3"

AutoItSetOption("TrayAutoPause", 0)
AutoItSetOption("TrayMenuMode", 1)
Opt("GUIResizeMode",802)

$ret = _BASS_Startup("bass.dll")
_BASS_Init(0, -1, 44100, 0, "")
If @error Then
    MsgBox(0, "Error", "Could not initialize audio")
    Exit
EndIf

Dim $folders[50000][2]
Dim $files[50000][4]
Dim $played[10000]
Global $filep = -1
Global $download
Global $folp
Global $listdir
Global $msg
Global $filefp
Global $mini
Global $find
Global $dfile
Global $bytes
Global $sound
Global $isfind
Global $win
Global $repeat = 0
$mini=0
$should = 0
$sound = 0
$pl = 0
$next = 0
$check = 0
$shuffle=1
$vsel = -1
$back = 0
$dl = 0
$vbytes = -10000
$size = 0
$name = ""
$vpros = 0
$earlier = 0
$seton=0
$np=-1
$aa=0
$lvl=0
$length=0


If Not FileExists(@AppDataDir & "\YouTube") Then DirCreate(@AppDataDir & "\YouTube")
If Not FileExists(@AppDataDir & "\YouTube\ffmpeg.exe") Then FileCopy(@ScriptDir & "\ffmpeg.exe", @AppDataDir & "\YouTube\ffmpeg.exe")
If Not FileExists(@ScriptDir & "\YouTube") Then DirCreate(@ScriptDir & "\YouTube")
$listdir = "" & @ScriptDir & "\list.dat"
If FileExists($listdir) = 0 Then
    MsgBox(0, "Creating playlist", "Select your music folder")
    $dir = FileSelectFolder("Select your music folder.", "")
    If $dir = "" Then
        While $dir = ""
            MsgBox(0, "Error", "Please select music folder correctly")
            $dir = FileSelectFolder("Select your music folder.", "")
        WEnd
    EndIf
    list($dir)
    list(@ScriptDir & "\Youtube")
    tofile()
ElseIf FileExists($listdir) = 1 Then
    fromfile()
EndIf
ReDim $files[$filep + 1][4]
$win = "HandyMusicPlayer " & $filep+1 & " Tracks"

$settings = GUICreate("Settings",200,200)
$sl1 = GUICtrlCreateLabel("Keys:",10,10)
$sl2 = GUICtrlCreateLabel("F2=Pause",15,25)
$sl3 = GUICtrlCreateLabel("F4=Play",15,40)
$sl4 = GUICtrlCreateLabel("F6=Previous",15,55)
$sl5 = GUICtrlCreateLabel("F8=Next",15,70)
$sl6 = GUICtrlCreateLabel("F9=Toggle shuffle",15,85)
$sl7 = GUICtrlCreateLabel("F12=Toggle repeat",15,100)
$sb1 = GUICtrlCreateButton("OK", 160,160,30,30)
$sc1 = GUICtrlCreateCheckbox("Shuffle", 10, 120)
$sc2 = GUICtrlCreateCheckbox("Repeat", 10, 140)


$dlgui = GUICreate("Downloading", 300, 50)
$dlstatus = GUICtrlCreateLabel("", 10, 10, 280, 50)
$gui = GUICreate($win, 400, 300)
GUISetIcon("icon.ico")
$l1 = GUICtrlCreateLabel("Music information, repeat = "&$repeat&", shuffle = "&$shuffle&"", 10, 10, 200, 15)
$l2 = GUICtrlCreateLabel("Name:", 15, 25, 250, 15)
$l3 = GUICtrlCreateLabel("%:", 15, 40, 320, 15)
$p = GUICtrlCreateProgress(15,55,370,15)

$b1 = GUICtrlCreateButton("Play", 170, 80, 70, 30)
$b2 = GUICtrlCreateButton("Pause", 90, 80, 70, 30)
$b3 = GUICtrlCreateButton("Previous", 10, 80, 70, 30)
$b4 = GUICtrlCreateButton("Next", 250, 80, 70, 30)
$b5 = GUICtrlCreateButton("Find track", 10, 120, 150, 30)
$b6 = GUICtrlCreateButton("Download from Youtube", 170, 120, 150, 30)
$b7 = GUICtrlCreateButton("Settings", 335, 10, 55, 30)
$b8 = GUICtrlCreateButton("Rebuild list", 330, 80, 60, 60)
$b9 = GUICtrlCreateButton("Mini mode", 260, 10, 65, 30)

$list = _GUICtrlListView_Create($gui, "Playlist", 0, 150, 400, 150)
_GUICtrlListView_SetColumnWidth($list, 0, 370)
_GUICtrlListView_AddColumn($list, "Directorys")
_GUICtrlListView_AddColumn($list, "Search results")
_GUICtrlListView_AddColumn($list, "Result directorys")
_GUICtrlListView_HideColumn($list, 1)
_GUICtrlListView_HideColumn($list, 2)
_GUICtrlListView_HideColumn($list, 3)
_GUICtrlListView_AddArray($list, $files)
GUISetState(@SW_SHOW, $gui)




While 1
    $alr = 0

    $state=_BASS_ChannelIsActive($sound)
    $pros=_BASS_ChannelGetPosition($sound, $BASS_POS_BYTE)/($length+0.0000001)*100
    GUICtrlSetData($p,$pros)
    if $vpros < $pros+0.1 then
        GUICtrlSetData($l3,"%: "&$pros)
        $vpros=$pros
    endif


    If $isfind = 1 Then
        $var = _GUICtrlListView_SubItemHitTest($list)
        If _IsPressed(01) and $var[0] > -1 Then
            For $i = 1 To $filep
                If $files[$i][0] = $files[$var[0]][2] Then
                    $sel = $i
                    _GUICtrlListView_SetSelectionMark($list, $i)
                EndIf
            Next
        EndIf
    Else
        $sel = _GUICtrlListView_GetSelectionMark($list)
    EndIf


    If $state = $BASS_ACTIVE_STOPPED And $earlier = 1 Then
        $next = 1
        If $repeat = 1 Then $check = 1
    EndIf

    If $sel <> $vsel And $sel > -1 Then $next = 1


    If $next = 1 Or $back = 1 Then
        $earlier = 1
        $vpos = 0
        _BASS_SampleFree($sound)
        If $check = 0 Then
            If $back = 0 Then
                If $sel = $vsel Then
                    if $shuffle=1 then
                        $np = Random(1, $filep, 1)
                    elseif $shuffle=0 then
                        if $np < $filep then
                            $np=$np+1
                        Else
                            $np=0
                        endif
                    endif
                Else
                    $np = $sel
                EndIf
                $pl = $pl + 1
                $played[$pl] = $np
            ElseIf $back = 1 Then
                $pl = $pl - 1
            EndIf
        EndIf


        $spot = $played[$pl]

        If $isfind = 1 Then
            _GUICtrlListView_SetColumnWidth($list, 0, 370)
            _GUICtrlListView_HideColumn($list, 1)
            _GUICtrlListView_HideColumn($list, 2)
            _GUICtrlListView_HideColumn($list, 3)
        EndIf
        $sound = _BASS_SampleLoad( False, $files[$spot][1], 0, 1, 0, 1, $BASS_SAMPLE_OVER_POS)
        $length = _BASS_ChannelGetLength( $sound, $BASS_POS_BYTE)
        GUICtrlSetData($l2, "Name: " & $files[$spot][0] & "")
        GUICtrlSetData($l3,"%: 0")
        $vpros=0
        $sound=_BASS_SampleGetChannel($sound, 0)

        _BASS_ChannelPlay($sound,1)
        if @error = $BASS_ERROR_HANDLE then MsgBox(0,"Error", "Handle is not a valid channel.")
        if @error = $BASS_ERROR_START then MsgBox(0,"Error", "The output is paused/stopped, use BASS_Start to start it.")
        if @error = $BASS_ERROR_DECODE then MsgBox(0,"Error", "The channel is not playable, it's a 'decoding channel'.")
        if @error = $BASS_ERROR_BUFLOST then MsgBox(0,"Error", "Should not happen... check that a valid window handle was used with BASS_Init.")
        if @error = $BASS_ERROR_NOHW then MsgBox(0,"Error", "No hardware voices are available.")

        $next = 0
        $back = 0
        $isfind = 0

    EndIf
    $vsel = $sel






    $msg = GUIGetMsg()
    if $msg = $b9 Then
        $w=@DesktopWidth
        $h=@DesktopHeight
        if $mini=0 then
            WindowGlide($gui, $win, $w-405, $h-143, 405, 113, 200)
            WinSetOnTop($win,"",1)
            $mini=1
        Else
            WindowGlide($gui, $win, $w/2-200, $h/2-150, 405, 333, 200)
            WinSetOnTop($win,"",0)
            $mini=0
        endif
    endif
    If $msg = $b1 Or _IsPressed(73) And $state = $BASS_ACTIVE_PAUSED Then
        _BASS_ChannelPlay($sound,0)
        Send("{F4 up}")
    EndIf
    If $msg = $b2 Or _IsPressed(71) And $state = $BASS_ACTIVE_PLAYING Then
        _BASS_ChannelPause($sound)
        Send("{F2 up}")
    EndIf
    If $msg = $b3 Or _IsPressed(75) And $pl > 1 Then
        $back = 1
        Send("{F6 up}")
    EndIf
    If $msg = $b4 Or _IsPressed(77) Then
        $next = 1
        Send("{F8 up}")
    EndIf

    If $msg = $b5 and $dl=0 and $seton=0 and $isfind=0 Then
        if $isfind=1 then
            _GUICtrlListView_SetColumnWidth($list, 0, 370)
            _GUICtrlListView_HideColumn($list, 1)
            _GUICtrlListView_HideColumn($list, 2)
            _GUICtrlListView_HideColumn($list, 3)
        endif
        $isfind=0
        $ff = InputBox("Find track", "Type part of track")
        If $ff Then
            findd($ff)
        EndIf

    EndIf
    If $msg = $b6 and $dl=0 and $seton=0 and $isfind=0 Then
        $search = InputBox("Find from Youtube", "Type correct link")
        If $search Then
            $source = _INetGetSource($search)
            $id = StringMid($source, StringInStr($source, "http://www.youtube.com/watch?v") + 31, 11)
            $size = download($id)
            If $size = -1 Then
                MsgBox(0, "Error", "")
            Else
                $spot=StringInStr($source, 'title=')+5
                $title = StringRight($source, StringLen($source)-$spot)
                If $title = "" Then
                    MsgBox(0, "Error", "Can't get the name of the track")
                Else
                    $name = "Youtube - " & _TitleConvertHTML($title)
                    For $i = 1 To $filep
                        If $files[$i][0] = $name Then
                            MsgBox(0, "Error", "You already have that track")
                            $alr = 1
                        EndIf
                    Next
                    If $alr = 0 Then
                        GUISetState(@SW_SHOW, $dlgui)
                        $dl = 1
                    EndIf
                EndIf
            EndIf
        EndIf
    EndIf

    If _IsPressed("7B") Then
        If $repeat = 0 Then
            $repeat = 1
        ElseIf $repeat = 1 Then
            $repeat = 0
        EndIf
        GUICtrlSetData($l1, "Music information, repeat = " & $repeat & ", shuffle = "&$shuffle&"")
        Send("{F12 up}")
        $check=0
    elseif _IsPressed("78") Then
        If $shuffle = 0 Then
            $shuffle = 1
        ElseIf $shuffle = 1 Then
            $shuffle = 0
        EndIf
        GUICtrlSetData($l1, "Music information, repeat = " & $repeat & ", shuffle = "&$shuffle&"")
        Send("{F9 up}")
    endif


    If $msg = $b7 and $dl=0 and $seton=0 and $isfind=0 Then
        GUISetState(@SW_SHOW, $settings)
        if $repeat=1 then
            GUICtrlSetData($sc2,1)
        EndIf
        if $shuffle=1 then GUICtrlSetState($sc1, 1)
        if $repeat=1 then GUICtrlSetState($sc2, 1)
        $seton=1
    endif


    If $msg = $b8 Then rebuild()
    If WinActive($gui) Then
        If $msg = $GUI_EVENT_CLOSE Then
            OnAutoItExit()
        EndIf
    EndIf

    if WinGetState($gui) = 23 then GUISetState(@SW_HIDE, $gui)

    $tmsg=TrayGetMsg()

    if $tmsg=-7 then
        GUISetState(@SW_SHOW, $gui)
        GUISetState(@SW_RESTORE, $gui)
    endif

    if $seton=1 then
        if $msg=$sb1 then
            GUISetState(@SW_HIDE,$settings)
            $seton=0
        EndIf
        if GUICtrlRead($sc1) = 1 then
            $shuffle=1
        Else
            $shuffle=0
        endif
        if GUICtrlRead($sc2) = 1 then
            $repeat=1
            $check=0
        Else
            $repeat=0
        endif
        GUICtrlSetData($l1, "Music information, repeat = " & $repeat & ", shuffle = "&$shuffle&"")
    endif


    If $dl = 1 Then
        $bytes=InetGetInfo($dfile,0)
        If Round($bytes / 1024 / 1024, 1) > $vbytes Then
            GUICtrlSetData($dlstatus, Round($bytes / 1024 / 1024, 1) & "Mb / " & Round(InetGetInfo($dfile,1) / 1024 / 1024, 1) & " Mb    " & Round(100 * ($bytes / InetGetInfo($dfile,1))) & "%" & Chr(10) & "" & $name & "")
            $vbytes = Round($bytes / 1024 / 1024, 1)
        EndIf
        If InetGetInfo($dfile,2) Then
            TrayTip("Download finished", "Downloaded track is the last track in playlist", 0)
            convert(@ScriptDir & "\Youtube\" & $name & ".mp3")
            GUISetState(@SW_HIDE, $dlgui)
            $f = FileOpen(@ScriptDir & "\list.dat", 1)
            FileWriteLine($f, $name)
            FileWriteLine($f, @ScriptDir & "\Youtube\" & $name & ".mp3")
            FileClose($f)
            $filep = $filep + 1
            ReDim $files[$filep + 2][4]
            fromfile()
            _GUICtrlListView_DeleteAllItems($list)
            _GUICtrlListView_SetColumnWidth($list, 0, 370)
            _GUICtrlListView_HideColumn($list, 1)
            _GUICtrlListView_HideColumn($list, 2)
            _GUICtrlListView_HideColumn($list, 3)
            _GUICtrlListView_AddArray($list, $files)
            _GUICtrlListView_SetSelectionMark($list, 0)
            $name = ""
            $size = 0
            GUICtrlSetData($dlstatus, "")
            $vbytes = 0
            $dl = 0
        EndIf
    EndIf


    Sleep(5)
WEnd

Func _TitleConvertHTML($n_title)
    $r_title = StringReplace($n_title, '+', ' ')
    $r_title = StringReplace($r_title, '%2B', '+')
    $r_title = StringReplace($r_title, '%3D', '=')
    $r_title = StringReplace($r_title, '%22', '"')
    $r_title = StringReplace($r_title, '%27', "'")
    $r_title = StringReplace($r_title, '%60', '`')
    $r_title = StringReplace($r_title, '%28', '(')
    $r_title = StringReplace($r_title, '%29', ')')
    $r_title = StringReplace($r_title, '%5B', '[')
    $r_title = StringReplace($r_title, '%5D', ']')
    $r_title = StringReplace($r_title, '%3A', ':')
    $r_title = StringReplace($r_title, '%2C', ',')
    $r_title = StringReplace($r_title, '%23', '#')
    $r_title = StringReplace($r_title, '%2F', '/')
    $r_title = StringReplace($r_title, '%21', '!')
    $r_title = StringReplace($r_title, '%3F', '?')
    $r_title = StringReplace($r_title, '%26', '&')
    $r_title = StringReplace($r_title, '%7C', '|')
    $r_title = StringReplace($r_title, '%40', '@')
    $r_title = StringReplace($r_title, '%5E', '^')
    $r_title = StringReplace($r_title, '%2A', '*')
    $r_title = StringReplace($r_title, '%7E', '~')

    $r_title = StringReplace($r_title, '%C2%AB', '«')
    $r_title = StringReplace($r_title, '%C2%BB', '»')
    $r_title = StringReplace($r_title, '%C2%B0', '°')
    $r_title = StringReplace($r_title, '%C2%B7', '·')
    $r_title = StringReplace($r_title, '%C3%A9', 'é')
    $r_title = StringReplace($r_title, '%C3%A8', 'è')
    $r_title = StringReplace($r_title, '%C3%AA', 'ê')
    $r_title = StringReplace($r_title, '%C3%A0', 'à')
    $r_title = StringReplace($r_title, '%C3%A1', 'á')
    $r_title = StringReplace($r_title, '%C3%A2', 'â')
    $r_title = StringReplace($r_title, '%C3%A7', 'ç')
    $r_title = StringReplace($r_title, '%C3%B1', 'ñ')
    $r_title = StringReplace($r_title, '%C2%A1', '¡')
    $r_title = StringReplace($r_title, '%C2%A5', '¥')
    $r_title = StringReplace($r_title, '%C2%BF', '¿')

    $r_title = StringReplace($r_title, '%E2%80%A2', '•')
    $r_title = StringReplace($r_title, '%E2%84%A2', '™')

    Return $r_title
EndFunc   ;==>_TitleConvertHTML


Func list($dir)
    $old = $dir
    FileChangeDir($dir)
    $first = FileFindFirstFile("*")
    While 1
        $h = FileFindNextFile($first)
        $whole = "" & $dir & "\" & $h & ""
        $att = FileGetAttrib($h)
        $ext = StringLower(StringRight($h, 3))
        If $ext = "mp3" Or $ext = "wma" Or $ext = "wav" Then
            $filep = $filep + 1
            $files[$filep][0] = StringLeft($h, StringLen($h) - 4)
            $files[$filep][1] = $whole
        ElseIf StringInStr($att, "D") <> 0 Then
            $folp = $folp + 1
            $folders[$folp][0] = $whole
        EndIf
        If $h = "" Then ExitLoop
        If @error = 1 Then ExitLoop
    WEnd
    For $x = 1 To $folp
        If $folders[$x][1] = 0 Then
            $folders[$x][1] = 1
            list($folders[$x][0])
        EndIf
    Next
    FileChangeDir($old)
EndFunc   ;==>list

Func tofile()
    $f = FileOpen($listdir, 2)

    For $i = 0 To $filep
        FileWriteLine($f, $files[$i][0])
        FileWriteLine($f, $files[$i][1])
    Next

    FileClose($f)
EndFunc   ;==>tofile

Func fromfile()
    $f = FileOpen($listdir, 0)
    $i = -1
    While 1
        $i = $i + 1
        $files[$i][0] = FileReadLine($f)
        $files[$i][1] = FileReadLine($f)
        If @error Then ExitLoop
    WEnd
    $filep = $i
    FileClose($f)
EndFunc   ;==>fromfile


Func findd($stri)
    If $stri = "" Then
        MsgBox(0, "Error", "input field is empty")
        $sure = 1
    Else
        $sure = 0
    EndIf

    If $stri <> "" And $sure = 0 Then
        For $k = 0 To $filefp
            $files[$k][3] = ""
            $files[$k][2] = ""
        Next
        $filefp = -1
        $files[0][2] = "Search results with (" & $stri & ")"
        For $d = 0 To $filep
            If StringInStr($files[$d][0], $stri) <> 0 Then
                $filefp = $filefp + 1
                $files[$filefp][2] = $files[$d][0]
                $files[$filefp][3] = $files[$d][1]
            EndIf
        Next
        If $filefp > -1 Then
            _GUICtrlListView_DeleteAllItems($list)
            _GUICtrlListView_SetColumnWidth($list, 2, 370)
            _GUICtrlListView_HideColumn($list, 0)
            _GUICtrlListView_HideColumn($list, 1)
            _GUICtrlListView_HideColumn($list, 3)
            _GUICtrlListView_AddArray($list, $files)
            $isfind = 1
        Else
            MsgBox(0, "Find track", "0 tracks founded")
        EndIf
    EndIf
EndFunc   ;==>findd

Func download($id)
    $source = _INetGetSource("http://www.youtube.com/get_video_info?&video_id=" & $id)
    $sign = StringMid($source, StringInStr($source, "&token=") + 7, StringInStr($source, "&thumbnail_url="))
    $download = "http://www.youtube.com/get_video?video_id=" & $id & "&t=" & $sign & "&fmt=18"
    If StringInStr($source, "fail") > 0 Then
        $size = -1
    Else
        FileDelete(@AppDataDir & "\YouTube\tempfile.mp4")
        $dfile=Inetget($download,@AppDataDir & "\YouTube\tempfile.mp4", 1, 1)
        $size=1
    EndIf
    Return $size
EndFunc   ;==>download


Func convert($out)
    $s_cmd = "ffmpeg.exe -i " & Chr(34) & "" & @AppDataDir & "\YouTube\tempfile.mp4" & Chr(34) & " -acodec mp3 -ab 128 -ar 44100 -ac 2 " & Chr(34) & "" & $out & "" & Chr(34) & ""
    $ffmpeg = Run(@AppDataDir & '\YouTube\' & $s_cmd, @AppDataDir & '\YouTube', @SW_HIDE, 4)
EndFunc   ;==>convert

Func rebuild()
    GUISetState(@SW_HIDE, $gui)
    FileDelete(@ScriptDir & "\list.dat")
    Run(@ScriptDir & "\Handy.exe")
    Exit
EndFunc   ;==>rebuild

Func OnAutoItExit()
    _BASS_Free()
    _BASS_SampleFree($sound)
    exit
EndFunc   ;==>OnAutoItExit



Func WindowGlide($hwnd, $text, $x, $y, $width, $height, $time = 200)
    Local $OrgPos = WinGetPos($hwnd, $text)

    If @error Then
        Return SetError(1, 0, 0)
    EndIf

    Local $NewPos[4]
    Local $timer = TimerInit()
    Local $percent_done = 0

    While TimerDiff($timer) < $time
        Sleep(10)
        $percent_done = TimerDiff($timer) / $time
        $NewPos[0] = $OrgPos[0] + ($x - $OrgPos[0]) * $percent_done
        $NewPos[1] = $OrgPos[1] + ($y - $OrgPos[1]) * $percent_done
        $NewPos[2] = $OrgPos[2] + ($width - $OrgPos[2]) * $percent_done
        $NewPos[3] = $OrgPos[3] + ($height - $OrgPos[3]) * $percent_done
        WinMove($hwnd, $text, $NewPos[0], $NewPos[1], $NewPos[2], $NewPos[3])
    WEnd

    WinMove($hwnd, $text, $x, $y, $width, $height)

    Return 1
EndFunc
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...