Sign in to follow this  
Followers 0
demandnothing

Loop Difficulties

3 posts in this topic

#1 ·  Posted (edited)

im using this script for a music player i got off these forums.. +thx to who posted it, but what it does is it only plays one song, so of course im trying to get it to do a continuous loop so that it plays all the songs on the list, but every time i try to do any sort of a loop for it, anywhere, it voids out the entire script and not even the exit button works.. here's the script:

#include <GuiConstants.au3>
#include <File.au3>
#include <Sound.au3>
#include <Misc.au3>

HotKeySet("^{F5}", "play")
HotKeySet("^{F6}", "stop")
HotKeySet("^{F7}", "skip")
HotKeySet("^{F8}", "previous")
HotKeySet("{F1}", "_exit")
If Not FileExists(@ScriptDir & "\Thatsgreat2345\") Then
    DirCreate(@ScriptDir & "\Thatsgreat2345\")
EndIf
GUICreate("Music Player Deluxe", 453, 399, 356, 173)
GUISetBkColor(0x000000)
$play = GUICtrlCreateButton("Play", 184, 8, 75, 25)
GUICtrlSetBkColor(-1, 0x000000)
GUICtrlSetColor(-1, 0x0000FF)
GUICtrlSetCursor(-1, 0)
$stop = GUICtrlCreateButton("Stop", 184, 72, 75, 25)
GUICtrlSetBkColor(-1, 0x000000)
GUICtrlSetColor(-1, 0xFF0000)
GUICtrlSetCursor(-1, 0)
$previous = GUICtrlCreateButton("Previous", 128, 40, 75, 25)
GUICtrlSetBkColor(-1, 0x000000)
GUICtrlSetColor(-1, 0xFFFF00)
GUICtrlSetCursor(-1, 0)
$Next = GUICtrlCreateButton("Next", 240, 40, 75, 25)
GUICtrlSetBkColor(-1, 0x000000)
GUICtrlSetColor(-1, 0x00FF00)
GUICtrlSetCursor(-1, 0)
$slider = GUICtrlCreateSlider(296, 200, 150, 45)
GUICtrlSetBkColor(-1, 0x000000)
GUICtrlSetData($slider, 100)
GUICtrlSetLimit($slider, 100, 0)
$list = GUICtrlCreateList("", 0, 248, 449, 149)
GUICtrlSetFont(-1, 8, 800, 0, "Palatino Linotype")
GUICtrlSetColor(-1, 0xFF0000)
GUICtrlSetBkColor(-1, 0x000000)
GUICtrlSetCursor(-1, 3)
$Mute = GUICtrlCreateButton("Mute", 360, 168, 75, 25)
GUICtrlSetBkColor(-1, 0x000000)
GUICtrlSetColor(-1, 0x800000)
GUICtrlSetCursor(-1, 0)
$Add = GUICtrlCreateButton("Add", 8, 216, 75, 25)
GUICtrlSetBkColor(-1, 0x000000)
GUICtrlSetColor(-1, 0x0000FF)
GUICtrlSetCursor(-1, 0)
$clear = GUICtrlCreateButton("Clear", 96, 216, 75, 25)
GUICtrlSetBkColor(-1, 0x000000)
GUICtrlSetColor(-1, 0xFF0000)
GUICtrlSetCursor(-1, 0)
$Label1 = GUICtrlCreateLabel("DemandNothing", 2, 134, 312, 80)
GUICtrlSetFont(-1, 48, 800, 2, "Script")
GUICtrlSetColor(-1, 0x0000FF)
GUISetState(@SW_SHOW)
$filehandle = FileOpen(@ScriptDir & "\Thatsgreat2345\Playlist.txt", 0)
While 1
    Local $Line = FileReadLine($filehandle)
    If @error Then ExitLoop
    GUICtrlSetData($list, $Line & "|")
WEnd
FileClose($filehandle)
GUISetState()
Dim $play
Dim $Mute
Dim $stop
Dim $exit
Dim $slider
Dim $msg
Dim $playlist
Dim $list
Dim $file
Dim $Line
Dim $previous
Dim $playlistpath = @ScriptDir & "\Thatsgreat2345\Playlist.txt"
Dim $songlistpath = @ScriptDir & "\Thatsgreat2345\song.txt"
Global $linenumber = 1
Dim $ps = _SoundPos($Line, 2)
Dim $len = "a"
Do
    $msg = GUIGetMsg()
    Select
        Case $msg = $stop
            stop()
        Case $msg = $play
            play()
        Case $msg = $Mute
            Send("{VOLUME_MUTE}")
        Case $msg = $slider
            SoundSetWaveVolume(GUICtrlRead($slider))
        Case $msg = $Add
            $playlist = FileOpenDialog("Choose music", "", "Musicfiles(*.mp3;*.wma;*.wav;*.wave;*.mid;*)")
            If Not @error Then
                FileWrite($playlistpath, $playlist & @CRLF)
                GUICtrlSetData($list, $playlist)
            EndIf
        Case $msg = $clear
            GUICtrlSetData($list, "")
            FileDelete($playlistpath)
            _FileCreate($playlistpath)
        Case $msg = $Next
            skip()
        Case $msg = $previous
            previous()
    EndSelect
Until $msg = $GUI_EVENT_CLOSE

Func stop()
    SoundPlay("C:\exit.mp3")
EndFunc   ;==>stop
Func play()
    $Line = FileReadLine($playlistpath, $linenumber)
    FileDelete($songlistpath)
    _FileCreate($songlistpath)
    FileWrite($songlistpath, $Line & @CRLF)
    SoundPlay($Line)
    $linenumber = $linenumber + 1
EndFunc   ;==>play
Func skip()
    If $linenumber <> _FileCountLines($playlistpath) Then
        $linenumber = $linenumber + 1
        $Line = FileReadLine($playlistpath, $linenumber)
        FileDelete($songlistpath)
        _FileCreate($songlistpath)
        FileWrite($songlistpath, $Line & @CRLF)
        SoundPlay($Line)
    EndIf
EndFunc   ;==>skip
Func previous()
    If $linenumber > 1 Then
        $linenumber = $linenumber - 1
        $Line = FileReadLine($playlistpath, $linenumber)
        FileDelete($songlistpath)
        _FileCreate($songlistpath)
        FileWrite($songlistpath, $Line & @CRLF)
        SoundPlay($Line)
    EndIf
EndFunc   ;==>previous
Func _exit()
    Exit
EndFunc   ;==>_exit

the play() function is where i need the loop to be, and as you can see i added $linenumber = $linenumber + 1 for my attempts at making a loop, here's the function:

Func play()
    $Line = FileReadLine($playlistpath, $linenumber)
    FileDelete($songlistpath)
    _FileCreate($songlistpath)
    FileWrite($songlistpath, $Line & @CRLF)
    SoundPlay($Line)
    $linenumber = $linenumber + 1
EndFunc   ;==>play

i've tried Do..Until loop, While..WEnd loop, i've put them in the function, i've put them in the Case statement, but no matter what i do, putting in a loop voids out the entire script, so that nothing will work.. can anybody help me out??

Edited by demandnothing

Share this post


Link to post
Share on other sites



Although i dont really know this script at all...from what i can see is that if you were to put a loop in the play() function, all thats happenning is that its..

Its starts the first song,

Then before you youd actually have heard anything, it will have started the next

And so on and so on..

SoundPlay() doesnt wait around for the whole song to play and then continue the script, it starts the song, then straightaway, in your script, it returns to the main while loop.

If you replace your Play() function with this one:

Just so you know...this isnt a solution..this is just to show you what i mean..Wait 5 or so secs everytime a msgbox appears!

Func play()
While $linenumber<5 ;Change the number to however many songs youre gonna have in your playlist
    $Line = FileReadLine($playlistpath, $linenumber)
    FileDelete($songlistpath)
    _FileCreate($songlistpath)
    FileWrite($songlistpath, $Line & @CRLF)
    SoundPlay($Line)
msgbox(0,"","Song Number: "&$linenumber)
    $linenumber = $linenumber + 1
WEnd
EndFunc   ;==>play

So, what im guessing youre going to need to do is have another

Case msg=

in the main while loop which can check when a song has finished, and then when it has call a function which will play the next song in the list..i hope this has been of some help, but sorry i cant be anymore helpful!


Instant Lockerz Invite - www.instantlockerzinvite.co.uk

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Func play()
    While $linenumber < _FileCountLines($playlistpath) ;Change the number to however many songs youre gonna have in your playlist
        $Line = FileReadLine($playlistpath, $linenumber)
        FileDelete($songlistpath)
        _FileCreate($songlistpath)
        FileWrite($songlistpath, $Line & @CRLF)
        $Snd = SoundPlay($Line)
        $h = _SoundPos($Snd, 2)
        $i = _SoundLength($Snd, 2)
        If $h = $i Then
            $linenumber = $linenumber + 1
        EndIf
    WEnd
EndFunc   ;==>play

this is what i've changed the function to.. which it seems like it should work, but i get an array error from Sound.au3.. it says exactly this:

C:\Program Files\AutoIt3\Include\Sound.au3 (442) : ==> Array variable has incorrect number of subscripts or subscript dimension range exceeded.:
If $aSndID[1] <> 0 Then
If ^ ERROR
->20:43:40 AutoIT3.exe ended.rc:1

which points to _SoundPos function in the au3 file:

Func _SoundPos($aSndID, $iMode = 1)
    ;Declare variables
    Local $iSndPosMs, $iSndPosMin, $iSndPosHour, $iSndPosSecs, $sSndPosFormat, $vTemp
    ;validate $iMode
    If $iMode <> 1 And $iMode <> 2 Then Return SetError(1, 0, 0)
    If Not IsArray($aSndID) Then
        If Not FileExists($aSndID) Then Return SetError(3, 0, 0) ; invalid file/alias
        $vTemp = FileGetShortName($aSndID)
        Dim $aSndID[1] = [$vTemp]
    EndIf
    If StringInStr($aSndID[0], '!') Then Return SetError(3, 0, 0) ; invalid file/alias

    ;tell mci to use time in milliseconds
    __mciSendString("set " & $aSndID[0] & " time format miliseconds")
    ;receive position of sound
    $iSndPosMs = Number(__mciSendString("status " & $aSndID[0] & " position", 255))
    If $aSndID[1] <> 0 Then
        $iSndPosMs -= $aSndID[2]
    EndIf

    ;modify data and assign to variables
    _TicksToTime($iSndPosMs, $iSndPosHour, $iSndPosMin, $iSndPosSecs)

    ;assign formatted data to $sSndPosFormat
    $sSndPosFormat = StringFormat("%02i:%02i:%02i", $iSndPosHour, $iSndPosMin, $iSndPosSecs)
    ;return correct variable
    If $iMode = 1 Then Return $sSndPosFormat
    If $iMode = 2 Then Return $iSndPosMs
EndFunc   ;==>_SoundPos

specifically:

;receive position of sound
    $iSndPosMs = Number(__mciSendString("status " & $aSndID[0] & " position", 255))
    If $aSndID[1] <> 0 Then
        $iSndPosMs -= $aSndID[2]
    EndIf

i've never opened (let alone edited) Sound.au3 before this error occured

Edited by demandnothing

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