Sign in to follow this  
Followers 0
netman74501

[SOLVED] _FileReadToArray stops working in while loop.

8 posts in this topic

#1 ·  Posted (edited)

My script:

;#NoTrayIcon
#include <GUIConstants.au3>
#include <WindowsConstants.au3>
#include <StaticConstants.au3>
#include <wmp.au3>
#include <file.au3>
#include <array.au3>
#include <Misc.au3>

_Singleton("lyricsdisplay")

Opt("GUIOnEventMode", 1)

$display1 = GUICreate("Lyrics Display", 518, 292, (@DesktopWidth-950)/2, (@DesktopHeight-750)/2)
$file = FileOpenDialog("Please Choose A Playlist or Song", @MyDocumentsDir, "All Supported Media (*.wpl;*.m3u;*.mp3;*.wav;*.mpg;*.wmv;*.avi)|Windows Media Playlists (*.wpl;*.m3u)|Music (*.mp3;*.wav)|Videos (*.mpg;*.wmv;*.avi)", 1 + 2, "", $display1)
If @error Then Exit
$display2 = GUICreate("Lyrics Display", 518, 277, (@DesktopWidth-700)/2, (@DesktopHeight-600)/2, BitOr($WS_MAXIMIZEBOX, $WS_MINIMIZEBOX))
GUISetOnEvent($GUI_EVENT_CLOSE, "quit")
GUISetOnEvent($GUI_EVENT_MAXIMIZE, "maximize")
GUISetOnEvent($GUI_EVENT_RESTORE, "restore")
$wmp = _wmpcreate(1, 0, 0, 425, 425);creates object
_wmploadmedia( $wmp, $file );loads media
_wmpsetvalue( $wmp, "controls" );shows controls
$labeltop = GUICtrlCreateLabel( "No Lyrics Found", 255, 68, 255, 60, $SS_CENTER)
$labelmid = GUICtrlCreateLabel( "", 255, 98, 255, 60, $SS_CENTER)
$labelbottom = GUICtrlCreateLabel( "", 255, 128, 255, 60, $SS_CENTER)
GUICtrlSetResizing ( $labeltop, $GUI_DOCKBORDERS )
GUICtrlSetResizing ( $labelmid, $GUI_DOCKBORDERS )
GUICtrlSetResizing ( $labelbottom, $GUI_DOCKBORDERS )
GUICtrlSetFont ($labelmid, -1, 900)
GUISetState(@SW_SHOW)

While 1
    $media = $wmp.currentMedia.SourceURL
    $lrcfile = StringTrimRight( $media, 3) & "lrc"
    $wmptime = _wmpsetvalue( $wmp, "getpositionstring" )
    $time = "[0" & StringTrimLeft($wmptime, 1) & "]"
    Dim $logArray
    If FileExists($lrcfile) Then
        _FileReadToArray($lrcfile, $logArray)
        $result = _ArraySearch ($logArray, $time, 0, 0, 0, true) 
        $openedfile = FileOpen($lrcfile, 0)
        $line = Ubound($logArray)-1
        If ($result == "-1") Then ContinueLoop
        $lyricstop = StringTrimLeft($logArray[$result-1], 7)
        $lyricsmid = StringTrimLeft($logArray[$result], 7)
        If $result <> $line Then
            $lyricsbottom = StringTrimLeft($logArray[$result+1], 7)
        Else
            $lyricsbottom = ""
        EndIf
        GUICtrlSetData ( $labeltop, $lyricstop)
        GUICtrlSetData ( $labelmid, $lyricsmid)
        GUICtrlSetData ( $labelbottom, $lyricsbottom)
    Else
        GUICtrlSetData ( $labeltop, "No Lyrics Found")
        GUICtrlSetData ( $labelmid, "")
        GUICtrlSetData ( $labelbottom, "")
    EndIf
    FileClose($openedfile)
    Sleep(500)
WEnd

$openedfile = FileOpen($lrcfile, 0)
FileClose($openedfile)
Msgbox(0, "Error", "Loop has ended.")
Exit

Func maximize()
GUICtrlSetPos ( $labeltop, 8, 260 , (@DesktopWidth-10) , 70 )
GUICtrlSetPos ( $labelmid, 8, 360 , (@DesktopWidth-10) , 70 )
GUICtrlSetPos ( $labelbottom, 8, 480 , (@DesktopWidth-10) , 70 )
GUICtrlSetFont ($labeltop, 24)
GUICtrlSetFont ($labelmid, 30, 900)
GUICtrlSetFont ($labelbottom, 24)
EndFunc

Func restore()
    $state = WinGetState("Lyrics Display", "")
    If $state == "15" Then
    GUICtrlSetPos ( $labeltop, 255, 68, 255, 60 )
    GUICtrlSetPos ( $labelmid, 255, 98, 255, 60 )
    GUICtrlSetPos ( $labelbottom, 255, 128, 255, 60 )
    GUICtrlSetFont ($labeltop, -1)
    GUICtrlSetFont ($labelmid, -1, 900)
    GUICtrlSetFont ($labelbottom, -1)
    EndIf
EndFunc

Func quit()
    $openedfile = FileOpen($lrcfile, 0)
    FileClose($openedfile)
    Exit
EndFunc

The problem:

After running for a while the _FileReadToArray seems to stop working correctly. The array stops being over written. I have narrowed my problem down to the _FileReadToArray function by using consolewrite to display the values of different variables in the script but do not know exactly why it refuses to overwrite the array after running for some time.

About the script:

This script requires a modified wmp.au3 UDF from here:

http://www.autoitscript.com/forum/index.php?showtopic=27352&st=0

The only change in the UDF is changing

Case $setting = "controls"
            $object.uiMode = "mini"

to

Case $setting = "controls"
            $object.uiMode = "full"

Other requirements are an lrc file that goes with a song of which the file names are identical except for the extensions. The lrc file must have the time code format of [MM:SS]. If you happen to have one that has miliseconds in it they must be taken out. I don't know if you can tell me just by looking at my script what is wrong, or if you have to test it, so I am trying to give as much info about it as I can. I am new to arrays and autoit programming in general, (fact is I dont know anything but HTML), so I have no idea what could be causing it to fail. I also have another script to take the miliseconds out so in case you need to do that it is below but it is not part of my problem.

#NoTrayIcon
#include <Misc.au3>

_Singleton("lyricsfixer")

$file = FileOpenDialog("Please Choose A Lyrics File", @MyDocumentsDir, "Lyrics File (*.lrc)", 1 + 2, "", GUICreate(""))
If @error Then Exit
$sSource = FileRead($file)
$sSource = StringRegExpReplace($sSource, "(\.\d\d)", "")
FileOpen($file, 2)
FileWrite($file, $sSource)
FileClose($file)
Run("notepad.exe " & $file, "")
Exit

That should be everything you need to run and test this... minus the lrc file and mp3 or playlist.

Btw, the purpose of this script is to simply display lyrics. :)

Edited by netman74501

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Why do you have this part? Your opening a file and then closing it the very next line.

$openedfile = FileOpen($lrcfile, 0)
FileClose($openedfile)

Edit: The more I looked at it I don't think you need to use fileopen() at all. You don't ever read from the file.

Edited by bchris01

Share this post


Link to post
Share on other sites

Why do you have this part? Your opening a file and then closing it the very next line.

$openedfile = FileOpen($lrcfile, 0)
FileClose($openedfile)

Edit: The more I looked at it I don't think you need to use fileopen() at all. You don't ever read from the file.

Because I wasn't sure if the file was opened when _FileReadToArray was called so i wanted to put in a FileClose just in case but then realized if it wasn't open it would probably error out or something so I put FileOpen just before to make sure that it was open. lol That was kinda confusing now that I say it out loud. I'll take it out but I don't think it is the source of my problems. Like I said, I am new to arrays. This is the first script I have used one in.

Share this post


Link to post
Share on other sites

Ya.. Ok well _filereadtoarray() closes the file when it done so don't worry about that. Looking at the other parts of your code I found some other mistakes.

$file = FileOpenDialog("Please Choose A Lyrics File", @MyDocumentsDir, "Lyrics File (*.lrc)", 1 + 2, "", GUICreate(""))
If @error Then Exit
$sSource = FileRead($file)
$sSource = StringRegExpReplace($sSource, "(\.\d\d)", "")
FileOpen($file, 2)
FileWrite($file, $sSource)
FileClose($file)
Run("notepad.exe " & $file, "")
Exit

See when you use fileopen() your deciding if you want to read to the file or write to it. This is why it must be called BEFORE you read or write to a file. I havent checked this code but should go something like this.

$file = FileOpenDialog("Please Choose A Lyrics File", @MyDocumentsDir, "Lyrics File (*.lrc)", 1 + 2, "", GUICreate(""))
If @error Then Exit
$sSource = FileOpen($file, 0)
$sSource = FileRead($file)
$sSource = StringRegExpReplace($sSource, "(\.\d\d)", "")
FileClose($sSource)
$sSource = FileOpen($file, 2)
FileWrite($file, $sSource)
FileClose($sSource)

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Ya.. Ok well _filereadtoarray() closes the file when it done so don't worry about that. Looking at the other parts of your code I found some other mistakes.

$file = FileOpenDialog("Please Choose A Lyrics File", @MyDocumentsDir, "Lyrics File (*.lrc)", 1 + 2, "", GUICreate(""))
If @error Then Exit
$sSource = FileRead($file)
$sSource = StringRegExpReplace($sSource, "(\.\d\d)", "")
FileOpen($file, 2)
FileWrite($file, $sSource)
FileClose($file)
Run("notepad.exe " & $file, "")
Exit

See when you use fileopen() your deciding if you want to read to the file or write to it. This is why it must be called BEFORE you read or write to a file. I havent checked this code but should go something like this.

$file = FileOpenDialog("Please Choose A Lyrics File", @MyDocumentsDir, "Lyrics File (*.lrc)", 1 + 2, "", GUICreate(""))
If @error Then Exit
$sSource = FileOpen($file, 0)
$sSource = FileRead($file)
$sSource = StringRegExpReplace($sSource, "(\.\d\d)", "")
FileClose($sSource)
$sSource = FileOpen($file, 2)
FileWrite($file, $sSource)
FileClose($sSource)

lol.. Ok. So I am a little fuzzy on when to use the FileOpen and FileClose commands but that isn't my concern right now. That script works how I want it to but nevertheless it should be fixed to correctly open and close files so thanks for pointing that out. The main program (the first script) is my real concern right now. I need to find out why the array doesn't update like it is meant to do after the program has been running for a while. It goes through the first 3 or 4 songs in a playlist just fine. Then after say... 6 songs, it just stops updating. It keeps whatever was the last value in the array. Am I hitting some kind of limit in autoit by continuously updating the array? Edited by netman74501

Share this post


Link to post
Share on other sites

Remove the fileopen() and fileclose() and make sure the error is coming from _filereadtoarray(). Put some error handling in..

If Not _FileReadToArray($lrcfile, $logArray) Then
   MsgBox(0,'Error Number', @error)
   Exit
EndIf

Share this post


Link to post
Share on other sites

Remove the fileopen() and fileclose() and make sure the error is coming from _filereadtoarray(). Put some error handling in..

If Not _FileReadToArray($lrcfile, $logArray) Then
   MsgBox(0,'Error Number', @error)
   Exit
EndIf

Well, I just spent the last 10 minutes trying to reproduce my problem after making the changes you suggested and failed. I don't have a clue why the FileOpen and FileClose would interfere with the array but I guess it somehow did. So, thank you for being patient and helping. (Runs off to learn why the code did what it did. Bad code! -spat, spat- Bad code!)

Share this post


Link to post
Share on other sites

Well remember _Filereadtoarray() uses fileopen() and fileclose(), so if you are using them incorrectly theres a chance it could end up causing _filereadtoarray() to fail.

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