kleiner schlanker musik player

  • Jetzt hab ich mein erstes kleines ordentliches Programm geschrieben zum Test.
    Über Kritik und verbesserungsvorschläge würde ich mich freuen :)
    (und ja ich weiß, mein Sprache ist nicht die beste :D )

    Spoiler anzeigen
    [autoit]

    ; include the UDf for the sound options
    #include <Sound.au3>

    [/autoit] [autoit][/autoit] [autoit]

    ; User Settings
    dim $musicdir = @ScriptDir & "\" ;dir for the music
    dim $hotkeyexit = "{ESC}";shortcut for left window
    dim $hotkeyvolumeup = "{UP}";shortcut for more volume
    dim $hotkeyvolumedown = "{DOWN}";shortcut for less volume
    dim $hotkeylasttitel = "{LEFT}";shortcut for last titel
    dim $hotkeynexttitel = "{RIGHT}";shortcut for next titel
    dim $hotkeyinfo = "^i";shortcut for info tray
    Dim $volume = 100;start Volume
    SoundSetWaveVolume ($volume);set start Volume
    dim $titelAtTime = 1; just at time playing song

    [/autoit] [autoit][/autoit] [autoit]

    ;System Settings
    dim $sound; object to play music
    dim $soundlength; length from song at time
    dim $titel = _FileListToArray( $musicdir ,"*.mp3", 1)
    ;search all mp3 in the folder
    Dim $titelnumber = $titel[0];how many songs are ready to play

    [/autoit] [autoit][/autoit] [autoit]

    ;Hotkeys for the script
    HotKeySet($hotkeyexit, "Terminate")
    HotKeySet($hotkeyvolumeup, "volumeup")
    HotKeySet($hotkeyvolumedown, "volumedown")
    HotKeySet($hotkeylasttitel, "lastTitel")
    HotKeySet($hotkeynexttitel, "nextTitel")
    HotKeySet($hotkeyinfo, "Info")

    [/autoit] [autoit][/autoit] [autoit]

    ; start Player and sound spooler
    playsound($titel[$titelAtTime])
    soundspooler()

    [/autoit] [autoit][/autoit] [autoit]

    ;terminate script
    Func Terminate()
    _SoundClose($sound)
    Exit 0
    EndFunc

    [/autoit] [autoit][/autoit] [autoit]

    ;function to increase the volume Max Volume = 100
    Func volumeup()
    if $volume < 90 Then
    $volume = $volume + 10
    elseif $volume < 100 Then
    $volume = 100
    EndIf
    SoundSetWaveVolume ($volume)
    TrayTip ( "Volume", "Wurde auf " & $volume & " erhöht.", 10 )
    EndFunc

    [/autoit] [autoit][/autoit] [autoit]

    ;function to decrease the volume Min Volume = 0
    Func volumedown()
    if $volume > 10 Then
    $volume = $volume - 10
    elseif $volume > 0 Then
    $volume = 0
    EndIf
    SoundSetWaveVolume ($volume)
    TrayTip ( "Volume", "Wurde auf " & $volume & " verringert.", 10 )
    EndFunc

    [/autoit] [autoit][/autoit] [autoit]

    ;function to start the next titel
    Func nextTitel()
    _SoundClose($sound)
    if $titelAtTime < $titelnumber Then
    $titelAtTime = $titelAtTime +1
    playsound($titel[$titelAtTime])
    TrayTip ( "Titel", "Nächster Titel wird gespielt.", 10 )
    Else
    $titelAtTime = 1
    playsound($titel[$titelAtTime])
    TrayTip ( "Titel", "Start vom Anfang.", 10 )
    EndIf
    soundspooler()
    EndFunc

    [/autoit] [autoit][/autoit] [autoit]

    ;function to start the last titel
    Func lastTitel()
    _SoundClose($sound)
    if $titelAtTime > 1 Then
    $titelAtTime = $titelAtTime - 1
    playsound($titel[$titelAtTime])
    TrayTip ( "Titel", "Letzter Titel wird gespielt.", 10 )
    Else
    $titelAtTime = $titelnumber
    playsound($titel[$titelAtTime])
    TrayTip ( "Titel", "Start des letzten Titels.", 10 )
    EndIf
    soundspooler()
    EndFunc

    [/autoit] [autoit][/autoit] [autoit]

    ;function to open a sound file and play the music
    Func playsound($music)
    $sound = _SoundOpen($music)
    If @error = 2 Then
    MsgBox(0, "Error", "The file does not exist")
    Exit
    ElseIf @extended <> 0 Then
    $extended = @extended ;assign because @extended will be set after DllCall
    $stText = DllStructCreate("char[128]")
    $errorstring = DllCall("winmm.dll", "short", "mciGetErrorStringA", "str", $extended, "ptr", DllStructGetPtr($stText), "int", 128)
    MsgBox(0, "Error", "The open failed." & @CRLF & "Error Number: " & $extended & @CRLF & "Error Description: " & DllStructGetData($stText, 1) & @CRLF & "Please Note: The sound may still play correctly.")
    EndIf
    _SoundPlay($sound, 0)
    EndFunc

    [/autoit] [autoit][/autoit] [autoit]

    ;function to show any Information about this song
    Func Info()
    TrayTip ( "Infos", "Lautstärke: " & $volume & @CRLF & "Derzeitiges Lied:" & $titelAtTime & " von" & $titelnumber & @CRLF & " Liedlänge:" & _SoundLength( $sound, 1 ) & " und die derzeitige Position im Lied " & _SoundPos ( $sound, 1 ), 10 )
    EndFunc

    [/autoit] [autoit][/autoit] [autoit]

    ;function to start all music
    func soundspooler ()
    $soundlength = _SoundLength( $sound, 2 )
    Sleep($soundlength -_SoundPos ( $sound, 2 ))
    nextTitel()
    EndFunc

    [/autoit]

    Einmal editiert, zuletzt von Blackflip (14. Dezember 2010 um 16:46)

  • Schöner kleiner Player :thumbup: , da aber nicht jeder User sämtliche MP3 's in einem Ordner hat solltest du noch eine Möglichkeit schaffen mit dem der Anwender den Ordner selbst aussuchen kann,

    mfg autoBert

  • Danke für die Kritik :)
    die nächste Version ist fertig, aber ich hab noch ein großes Problem.
    Wenn ich einen anderen Folder Name eingebe, gibt er mir nur aus, das dort keine Dateien gefunden wurden. Wenn ich die Datei in den Ordner kopiere, funktioniert es aber alles, die Musik erkennt er also und ist alles in Ordnung.
    Die Variable sieht aber ok aus, welche von meinen Filedialog zurückgegeben wird, da stehe ich jetzt irgendwie auf dem Schlauch

    Neue Funktionen:
    - Alle Hotkeys sind jetzt über eine Gui anpassbar
    - Es wird das Lied gespeichert, bei welchen beendet wurde
    - Es wird die Lautstärke gespeichert
    - ein Help Shortcut wurde eingefügt
    - ebenso ein shortcut um die Einstellungen zu löschen
    - eine Pausefunktion gibt es jetzt auch

    bekannter Bug:
    Wenn die Einstellung gemacht werden, soll er eigentlich wieder an der selben Stelle starten, aber das funktioniert noch nicht
    Wenn die Ini gelöscht wird, werden noch keine neuen Standardeinträge geschrieben, behebe ich dann wenn der andere Bug gefixt ist.
    Auch wenn pause eingestellt ist, zählt die Zeit weiter und das nächste Lied startet dann.

    Geplant:
    Das Tray Menü muss noch angepasst werden, das alle Funktionen, welche es als hotkey gibt, auch dort zur Verfügung stehen
    Besser Beschriftung desQuelltext später

    Spoiler anzeigen
    [autoit]

    ; include the UDf for the sound options
    #include <Sound.au3>
    #include <ButtonConstants.au3>
    #include <EditConstants.au3>
    #include <GUIConstantsEx.au3>
    #include <StaticConstants.au3>
    #include <WindowsConstants.au3>
    #include <Date.au3>

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    dim $sound; object to play music
    dim $soundlength; length from song at time
    dim $Paused = False;if the music paused or not
    Global $Secs, $Mins, $Hour

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    Dim $musicdir = check_ini("User Settings", "musicdir", @ScriptDir & "\")
    Dim $volume = check_ini("User Settings", "volume", "100")
    Dim $titelAtTime = check_ini("User Settings", "$titelAtTime", "1")
    Dim $hotkeyexit = check_ini("Hotkey", "exit", "{ESC}")
    Dim $hotkeyvolumeup = check_ini("Hotkey", "volumeup", "{UP}")
    Dim $hotkeyvolumedown = check_ini("Hotkey", "volumedown", "{DOWN}")
    Dim $hotkeylasttitel = check_ini("Hotkey", "lasttitel", "{LEFT}")
    Dim $hotkeynexttitel = check_ini("Hotkey", "nexttitel", "{RIGHT}")
    Dim $hotkeypause = check_ini("Hotkey", "Pause", "{PAUSE}")
    Dim $hotkeyinfo = check_ini("Hotkey", "info", "^i")
    Dim $hotkeydeleatini = check_ini("Hotkey", "DelIni", "^l")
    Dim $hotkeyhelp = check_ini("Hotkey", "Help", "{F1}")
    Dim $hotkeysettings = check_ini("Hotkey", "Settings", "^!s")

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    ;System Settings
    SoundSetWaveVolume ($volume);set start Volume

    [/autoit] [autoit][/autoit] [autoit]

    dim $titel = _FileListToArray( $musicdir ,"*.mp3", 1)
    ;search all mp3 in the folder
    Dim $titelnumber = $titel[0];how many songs are ready to play

    [/autoit] [autoit][/autoit] [autoit]

    ;Hotkeys for the script
    HotKeySet($hotkeyexit, "Terminate")
    HotKeySet($hotkeyvolumeup, "volumeup")
    HotKeySet($hotkeyvolumedown, "volumedown")
    HotKeySet($hotkeylasttitel, "lastTitel")
    HotKeySet($hotkeynexttitel, "nextTitel")
    HotKeySet($hotkeyinfo, "Info")
    HotKeySet($hotkeydeleatini, "delini")
    HotKeySet($hotkeyhelp, "helper")
    HotKeySet($hotkeysettings, "opensettings")
    HotKeySet($hotkeypause, "TogglePause")

    [/autoit] [autoit][/autoit] [autoit]

    ; start Player and sound spooler
    if $CMDLINE[0] = 0 Then
    playsound($titel[$titelAtTime])
    Else
    playsound($titel[$titelAtTime], $CMDLINE[1])
    EndIf
    soundspooler()

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    Func helper()
    TrayTip ( "Hilfe", "Es giebt die folgendne Shortcuts: ...", 20 )
    EndFunc

    [/autoit] [autoit][/autoit] [autoit]

    Func opensettings()
    TrayTip ( "Settings", "werden geöffnet.", 5 )
    $Form1 = GUICreate("Settings", 633, 447, 757, 189)
    $Group1 = GUICtrlCreateGroup("Settings", 16, 16, 465, 57)
    $lbl_Pfad = GUICtrlCreateLabel("Pfad zur Musik", 26, 36, 88, 17)
    GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
    $Input1 = GUICtrlCreateInput($musicdir, 122, 36, 257, 21)
    $Btn_choose = GUICtrlCreateButton("auswählen", 394, 36, 75, 25, $WS_GROUP)
    GUICtrlSetCursor (-1, 0)
    GUICtrlCreateGroup("", -99, -99, 1, 1)

    [/autoit] [autoit][/autoit] [autoit]

    $Group2 = GUICtrlCreateGroup("Hotkeys", 16, 88, 465, 161)
    $Label1 = GUICtrlCreateLabel("Exit", 36, 110, 21, 17)
    $Label2 = GUICtrlCreateLabel("Pause", 244, 110, 34, 17)
    $Label3 = GUICtrlCreateLabel("next Titel", 36, 134, 47, 17)
    $Label4 = GUICtrlCreateLabel("last Titel", 244, 134, 43, 17)
    $Label5 = GUICtrlCreateLabel("Volume up", 37, 157, 54, 17)
    $Label6 = GUICtrlCreateLabel("Volume down", 245, 157, 68, 17)
    $Label7 = GUICtrlCreateLabel("Info tray", 37, 181, 42, 17)
    $Label8 = GUICtrlCreateLabel("Deleate Settings", 245, 181, 82, 17)
    $Label9 = GUICtrlCreateLabel("Help", 37, 205, 26, 17)
    $Label10 = GUICtrlCreateLabel("Settings", 245, 205, 42, 17)

    [/autoit] [autoit][/autoit] [autoit]

    $Input2 = GUICtrlCreateInput($hotkeyexit, 108, 110, 121, 21)
    $Input3 = GUICtrlCreateInput($hotkeypause, 324, 110, 121, 21)
    $Input4 = GUICtrlCreateInput($hotkeynexttitel, 108, 134, 121, 21)
    $Input5 = GUICtrlCreateInput($hotkeylasttitel, 324, 134, 121, 21)
    $Input6 = GUICtrlCreateInput($hotkeyvolumeup, 325, 157, 121, 21)
    $Input7 = GUICtrlCreateInput($hotkeyvolumedown, 109, 157, 121, 21)
    $Input8 = GUICtrlCreateInput($hotkeyinfo, 109, 181, 121, 21)
    $Input9 = GUICtrlCreateInput($hotkeydeleatini, 325, 181, 121, 21)
    $Input10 = GUICtrlCreateInput($hotkeyhelp, 109, 205, 121, 21)
    $Input11 = GUICtrlCreateInput($hotkeysettings, 325, 205, 121, 21)
    GUICtrlCreateGroup("", -99, -99, 1, 1)
    $Btn_save = GUICtrlCreateButton("Save", 392, 256, 75, 25, $WS_GROUP)
    GUICtrlSetCursor (-1, 0)
    GUISetState(@SW_SHOW)

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
    Case $GUI_EVENT_CLOSE
    GUIDelete ( $Form1)
    ExitLoop
    Case $Btn_choose
    GUICtrlSetData($Input1,FileSelectFolder ( "Please choose the folder from playing Music", "",1, GuiCtrlRead($Input1))&"\")
    Case $Btn_save
    IniWrite_("User Settings", "musicdir", GuiCtrlRead($Input1))
    IniWrite_("Hotkey", "exit", GuiCtrlRead($Input2))
    IniWrite_("Hotkey", "volumeup", GuiCtrlRead($Input6 ))
    IniWrite_("Hotkey", "volumedown", GuiCtrlRead($Input7))
    IniWrite_("Hotkey", "lasttitel", GuiCtrlRead($Input5))
    IniWrite_("Hotkey", "nexttitel", GuiCtrlRead($Input4))
    IniWrite_("Hotkey", "Pause", GuiCtrlRead($Input3))
    IniWrite_("Hotkey", "info", GuiCtrlRead($Input8))
    IniWrite_("Hotkey", "DelIni", GuiCtrlRead($Input9))
    IniWrite_("Hotkey", "Help", GuiCtrlRead($Input10))
    IniWrite_("Hotkey", "Settings", GuiCtrlRead($Input11))
    if GuiCtrlRead($Input1) = $musicdir Then
    Run(@AutoItExe & " " &_SoundPos ( $sound, 2 ))
    Else
    IniWrite_("User Settings", "$titelAtTime", 1)
    Run(@AutoItExe)
    EndIf
    GUIDelete ( $Form1)
    exit
    EndSwitch
    WEnd
    EndFunc

    [/autoit] [autoit][/autoit] [autoit]

    Func delini()
    FileDelete(@ScriptDir & "\player.ini")
    TrayTip ( "Einstellung", "Wurden erfolgreich zurückgesetzt.", 10 )
    _FileCreate(@ScriptDir & "\player.ini")
    EndFunc

    [/autoit] [autoit][/autoit] [autoit]

    ;funktion to read in less text the Ini
    Func IniRead_($section, $name)
    return IniRead ( @ScriptDir & "\player.ini", $section, $name, "")
    EndFunc

    [/autoit] [autoit][/autoit] [autoit]

    ;funktion to write in less text the Ini
    Func IniWrite_($section, $name, $text)
    IniWrite(@ScriptDir & "\player.ini", $section, $name, $text)
    EndFunc

    [/autoit] [autoit][/autoit] [autoit]

    ;function for check if setting write in file or not
    func check_ini($sec, $nam, $defould)
    $spacer = IniRead_($sec, $nam)
    if @error OR $spacer = "" Then
    IniWrite_($sec, $nam, $defould)
    $spacer = $defould
    EndIf
    Return $spacer
    EndFunc

    [/autoit] [autoit][/autoit] [autoit]

    ;terminate script
    Func Terminate()
    IniWrite_("User Settings", "$titelAtTime", $titelAtTime)
    _SoundClose($sound)
    Exit 0
    EndFunc

    [/autoit] [autoit][/autoit] [autoit]

    ;function to increase the volume Max Volume = 100
    Func volumeup()
    if $volume < 90 Then
    $volume = $volume + 10
    elseif $volume < 100 Then
    $volume = 100
    EndIf
    IniWrite_("User Settings", "volume", $volume)
    SoundSetWaveVolume ($volume)
    TrayTip ( "Volume", "Wurde auf " & $volume & " erhöht.", 10 )
    EndFunc

    [/autoit] [autoit][/autoit] [autoit]

    ;function to decrease the volume Min Volume = 0
    Func volumedown()
    if $volume > 10 Then
    $volume = $volume - 10
    elseif $volume > 0 Then
    $volume = 0
    EndIf
    IniWrite_("User Settings", "volume", $volume)
    SoundSetWaveVolume ($volume)
    TrayTip ( "Volume", "Wurde auf " & $volume & " verringert.", 10 )
    EndFunc

    [/autoit] [autoit][/autoit] [autoit]

    ;function to start the next titel
    Func nextTitel()
    _SoundClose($sound)
    if $titelAtTime < $titelnumber Then
    $titelAtTime = $titelAtTime +1
    playsound($titel[$titelAtTime])
    TrayTip ( "Titel", "Nächster Titel wird gespielt.", 10 )
    Else
    $titelAtTime = 1
    playsound($titel[$titelAtTime])
    TrayTip ( "Titel", "Start vom Anfang.", 10 )
    EndIf
    soundspooler()
    EndFunc

    [/autoit] [autoit][/autoit] [autoit]

    ;function to start the last titel
    Func lastTitel()
    _SoundClose($sound)
    if $titelAtTime > 1 Then
    $titelAtTime = $titelAtTime - 1
    playsound($titel[$titelAtTime])
    TrayTip ( "Titel", "Letzter Titel wird gespielt.", 10 )
    Else
    $titelAtTime = $titelnumber
    playsound($titel[$titelAtTime])
    TrayTip ( "Titel", "Start des letzten Titels.", 10 )
    EndIf
    soundspooler()
    EndFunc

    [/autoit] [autoit][/autoit] [autoit]

    ;function to open a sound file and play the music
    Func playsound($music, $soundseek = 0)
    $sound = _SoundOpen($music)
    If @error = 2 Then
    MsgBox(0, "Error", "The file does not exist")
    Exit
    ElseIf @extended <> 0 Then
    $extended = @extended ;assign because @extended will be set after DllCall
    $stText = DllStructCreate("char[128]")
    $errorstring = DllCall("winmm.dll", "short", "mciGetErrorStringA", "str", $extended, "ptr", DllStructGetPtr($stText), "int", 128)
    MsgBox(0, "Error", "The open failed." & @CRLF & "Error Number: " & $extended & @CRLF & "Error Description: " & DllStructGetData($stText, 1) & @CRLF & "Please Note: The sound may still play correctly.")
    EndIf
    ;_SoundPlay($sound, 0)
    _TicksToTime($soundseek/1000, $Hour, $Mins, $Secs)
    ;MsgBox(0, $Hour & $Mins& $Secs, $Hour & $Mins& $Secs)
    _SoundSeek ($sound, $Hour, $Mins, $Secs)
    _SoundPlay($sound, 0)
    EndFunc

    [/autoit] [autoit][/autoit] [autoit]

    Func TogglePause()
    $Paused = NOT $Paused
    if $Paused Then
    _SoundPause ($sound)
    Else
    _SoundResume ($sound)

    [/autoit] [autoit][/autoit] [autoit]

    EndIf
    EndFunc

    [/autoit] [autoit][/autoit] [autoit]

    ;function to show any Information about this song
    Func Info()
    TrayTip ( $titel[$titelAtTime], "Lautstärke: " & $volume & @CRLF & "Derzeitiges Lied:" & $titelAtTime & " von" & $titelnumber & @CRLF & " Liedlänge:" & _SoundLength( $sound, 1 ) & " und die derzeitige Position im Lied " & _SoundPos ( $sound, 1 ), 10 )
    EndFunc

    [/autoit] [autoit][/autoit] [autoit]

    ;function to start all music
    func soundspooler ()
    IniWrite_("User Settings", "$titelAtTime", $titelAtTime)
    $soundlength = _SoundLength( $sound, 2 )
    Sleep($soundlength -_SoundPos ( $sound, 2 ))
    nextTitel()
    EndFunc

    [/autoit]



    Edit Oscar: Spoiler gesetzt. Bitte in Zukunft bei längeren Skripten immer auch den Spoiler benutzen!

    2 Mal editiert, zuletzt von Oscar (14. Dezember 2010 um 15:22)

  • Wenn ich einen anderen Folder Name eingebe, gibt er mir nur aus, das dort keine Dateien gefunden wurden.

    Das liegt daran dass _FileListToArray nur den Dateinamen, nicht den kompletten Pfad zurückgibt, du must also beim Abspielen

    [autoit]

    $musicdir & $titel

    [/autoit]

    statt

    [autoit]

    $titel

    [/autoit]

    verwenden,

    mfg autoBert

  • Danke für die Hilfe, das klapt jetzt wunderbar.
    Ich werde das nach weihnachten dann weiter machen :)