Jump to content
m4rcin93

MP3 player - playlist problem

Recommended Posts

m4rcin93

Hello.

I want to add playlist to my MP3 Player. To array i added 3 songs, i wanted to when first song finished, start 2nd song but it doesn't work.

It's works only with _soundplay($aaa, 1) - with flag 1, i want do it with flag 0. How to do it?

$file = FileReadToArray("file.txt")

For $x = 0 To Ubound($file) -1
$test = _Soundplay($file[$x],0)
Next

 

Share this post


Link to post
Share on other sites
Melba23

m4rcin93,

Try opening the files before playing them - this works for me:

#include <Array.au3>
#include <Sound.au3>

$aFile = FileReadToArray("file.txt")

; Add another column
_ArrayColInsert($aFile, 1)

For $x = 0 To Ubound($aFile) - 1
    ; Open each file before playing and store the ID
    $aFile[$x][1] = _SoundOpen($aFile[$x][0])
    ; Now play the file
    _Soundplay($aFile[$x][1], 0)

    Sleep(1000)
Next

; Close the files when terminated
$iStopped = 0
Do
    For $x = 0 To UBound($aFile) - 1
        ; Check if track ended and not already closed
        If ($aFile[$x][1] <> 1) And (_SoundStatus($aFile[$x][1]) = "stopped") Then
            ; Close the file
            _SoundClose($aFile[$x][1])
            ; Set the flag to indicate this
            $iStopped += 1
        EndIf
    Next
    ; Save the CPU
    Sleep(1000)
Until $iStopped = 3

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites
m4rcin93

Its works for me too but when music playing i can't do anything in program.

I can't use buttons, i can't exit program etc.

Its works now like function _SoundPlay with flag 1.

Have you any other idea for solve this problem?

Share this post


Link to post
Share on other sites
Melba23

m4rcin93,

Quote

Have you any other idea for solve this problem?

Of course! Run your normal idle loop once the files have been opened and are playing and check every now and again from within that loop (use TimerInit/Diff or Adlib) to see if they have finished and should be closed.

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites
Bilgus

I threw Something together

#include <Array.au3>
#include <Sound.au3>
#include <File.au3>

;FileReadToArray("file.txt")
$aFile = _FileListToArray(@ScriptDir, "*.mp3", $FLTA_FILES, True)

If Not IsArray($aFile) Then
    MsgBox(0, "SoundPlay", "No Files")
    Exit
EndIf

_ArrayDisplay($aFile)
_ArrayAdd($aFile, "")
$aFile[0] += 1 ; add an extra entry so we can play the last file


OnAutoItExitRegister("_exit")

Global $iArrPos = 1
Global $bRet

;Main Loop
While $iArrPos <= $aFile[0]

    $bRet = PlayNext($aFile[$iArrPos])
    If $bRet == True Then
        ToolTip($aFile[$iArrPos], 0, 0)
        Sleep(500)
        ToolTip("", 0, 0)
        $iArrPos += 1
    EndIf
    Sleep(500)
WEnd

Func _exit()
    PlayNext("", True)
EndFunc

Func PlayNext($sFile, $bQuit = False)
    Local Static $aSong[3] = [0, 0, ""]
    Local Const $iWait = 1000

    If $bQuit = False And ($aSong[1] = 0 Or $aSong[0] <= TimerDiff($aSong[1])) Then
        $aSong[0] = _SoundLength($sFile, 2) + $iWait
        ConsoleWrite($aSong[0] / 1000 & " Secs" & @CRLF)
        $aSong[2] = $sFile
        Sleep(500)
        If _SoundPlay($aSong[2], 0) <> 1 Then
            if $aSong[2] <> "" Then MsgBox(0, "error", "Error Playing " & $aSong[2])
            $aSong[1] = 0
        EndIf
        $aSong[1] = TimerInit()
        Return True
    ElseIf $bQuit = True Then
        ConsoleWrite("Stopping " & $aSong[2])
        _SoundStop($aSong[2])
    Else
        ;ConsoleWrite($aSong[0] & " MS " & TimerDiff($aSong[1]) & @CRLF)
    EndIf
    Return False
EndFunc

You might still need to do the trick with preopening the handles but if you have a lot of files it would be a bad idea to pre-open the whole playlist and instead do it in chunks

Share this post


Link to post
Share on other sites
CYCho

When I wanted to learn Autoit, I decided to make my own audio player with it. It took me couple of years to come up with a decent player. Through the process I learned a lot about AutoIt and I am proud to have learned it in my sixties after retirement.

My goal with the player was:

1. The player should be a stand-alone, with no need to set up.

2. Upon my selection of a folder the player should play all the music in that folder and/or it's subfolders.

3. I should be able to play the files in alphabetical order or in randomly shuffled order.

4. The player will have a bare minimum of user interface with only absolutely necessary functions.

5. I should be able to hide all its windows and even the task bar thumb nail.

6. All the mouse clicks should have corresponding hot keys, so that functions can be called when the windows are hidden.

7. When I start the program, it should automatically start playing the music it was playing at the time of last closing.

 

My player is named zPlayer in the sense that it is the most ignoble player in the world. I know now there is a mobile player of the same name, but mine came first.

I'm attaching the compiled version of my player. The source is poorly coded and I can't recommend it to anyone as a good example, but please tell me if you still want the code.

 

PS: The download of exe or zip format is being prevented by windows, so I'm uploading my code below.

Edited by CYCho
exe format not downloaded

Share this post


Link to post
Share on other sites
Bilgus

CYCHo I think Posting Exes files is a cop out and dangerous for people as well I think I probably speak for everyone in that we'd rather see the code

Also.. I ran with the example a bit.. :)

#include <Array.au3>
#include <Sound.au3>
#include <File.au3>
#include <WindowsConstants.au3>
#include <GUIConstantsEx.au3>

;FileReadToArray("file.txt")
$aFile = _FileListToArray(@ScriptDir, "*.mp3", $FLTA_FILES, True)

If Not IsArray($aFile) Then
    MsgBox(0, "SoundPlay", "No Files")
    Exit
EndIf

_ArrayDisplay($aFile)
_ArrayAdd($aFile, "")
$aFile[0] += 1 ; add an extra entry so we can play the last file


OnAutoItExitRegister("_exit")


#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 250, 68, 192, 124)
$Label1 = GUICtrlCreateLabel("", 5, 1, 240, 20)
$ButtonBack = GUICtrlCreateButton("<<", 10, 32, 60, 25)
$ButtonStop = GUICtrlCreateButton("Stop", 95, 32, 60, 25)
$ButtonFwd = GUICtrlCreateButton(">>", 180, 32, 60, 25)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

Global $iWait = 0
Global $iArrPos = 1
Global $bRet

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $ButtonBack
            $iArrPos -= 2
            PlayNext("", True)
            $iWait = 0
        Case $ButtonFwd
            If $iArrPos = 0 Then $iArrPos = 1
            PlayNext("", True)
            $iWait = 0
        Case $ButtonStop
            PlayNext("", True)
            $iArrPos = 0
        Case Else
            $iWait += 1 ; just so we don't check constantly
    EndSwitch

    If $iWait > 200 Or $iWait = 0 Then
        $iWait = 1
        ;Main Loop
        If $iArrPos > 0 And $iArrPos <= $aFile[0] Then
            $bRet = PlayNext($aFile[$iArrPos])
            If $bRet == True Then
                GUICtrlSetData($Label1, SplitFileName($aFile[$iArrPos]))
                ToolTip($aFile[$iArrPos], 0, 0)
                Sleep(1500)
                ToolTip("", 0, 0)
                $iArrPos += 1
            EndIf
        Else
            If $iArrPos < 0 Then $iArrPos = 0
            GUICtrlSetData($Label1, "")
        EndIf
    EndIf
WEnd

Func _exit()
    PlayNext("", True)
EndFunc   ;==>_exit

Func PlayNext($sFile, $bQuit = False)
    Local Enum $eaS_sName = 0, $eaS_iDuration, $eaS_hOpen, $eaS_hTimer
    Local Static $aSong[4] = ["", 0, 0, 0]
    Local Const $iWait = 1000

    If $bQuit = False And ($aSong[$eaS_hOpen] = 0 Or $aSong[$eaS_iDuration] <= TimerDiff($aSong[$eaS_hTimer])) Then
        If $aSong[$eaS_hOpen] <> 0 Then _SoundClose($aSong[$eaS_hOpen])
        $aSong[$eaS_sName] = $sFile
        $aSong[$eaS_hOpen] = _SoundOpen($aSong[$eaS_sName])
        $aSong[$eaS_iDuration] = _SoundLength($aSong[$eaS_hOpen], 2) + $iWait
        ConsoleWrite($aSong[$eaS_iDuration] / 1000 & " Secs" & @CRLF)
        $aSong[3] = $sFile
        Sleep(500)
        If _SoundPlay($aSong[$eaS_hOpen], 0) <> 1 Then
            If $aSong[$eaS_sName] <> "" Then MsgBox(0, "error", "Error Playing " & $aSong[$eaS_sName])
            $aSong[$eaS_sName] = ""
            $aSong[$eaS_iDuration] = 0
            _SoundClose($aSong[$eaS_hOpen])
            $aSong[$eaS_hOpen] = 0
            $aSong[$eaS_hTimer] = 0
        Else
            $aSong[$eaS_hTimer] = TimerInit()
        EndIf
        Return True

    ElseIf $bQuit = True And $aSong[$eaS_hOpen] <> 0 Then
        ConsoleWrite("Stopping " & $aSong[$eaS_sName] & @CRLF)
        _SoundStop($aSong[$eaS_hOpen])
        $aSong[$eaS_sName] = ""
        $aSong[$eaS_iDuration] = 0
        _SoundClose($aSong[$eaS_hOpen])
        $aSong[$eaS_hOpen] = 0
        $aSong[$eaS_hTimer] = 0
    Else
        ;;Update elapsed etc
    EndIf
    Return False
EndFunc   ;==>PlayNext

Func SplitFileName($FullPath)
    Local $sDummy, $sFileName, $sExt
    _PathSplit($FullPath, $sDummy, $sDummy, $sFileName, $sExt)
    Return $sFileName & "" & $sExt
EndFunc   ;==>SplitFileName

 

Share this post


Link to post
Share on other sites
CYCho

OK, here is my code. I know there is a lot to improve. I would appreciate any comments or ideas for correction.

 

P.S: A small bug was fixed in zPlayer.au3 - 2018/11/07

 

 

 

zPlayer.au3

Edited by CYCho

Share this post


Link to post
Share on other sites
Bilgus

Pretty cool I can't get random to work though It just pops up my music folder.. Also Whats up with $sNumbers? I assume thats random numbers for shuffle

but rather than that Why not 

SRandom(@SEC) ;Seed random number generator
Random(1, UBound($aList) - 1)

 

Share this post


Link to post
Share on other sites
CYCho

When I use Random(), it had certain routines which I didn't like. So I just took some numbers ffom RANDOM.ORG. Please bear in mind that this project was and still is my learning process without any previous programming background.

Edited by CYCho

Share this post


Link to post
Share on other sites
Bilgus

If you need unique numbers chck out this specifically the post from jchd

 

Share this post


Link to post
Share on other sites
CYCho

Bilgus, thank you for your suggestion. I don't clearly understand what you mean by "can't get random to work though It just pops up my music folder." Can you elaborate it a little more?

Share this post


Link to post
Share on other sites
Bilgus

I press ctrl + Alt + M and It pops up my my music folder rather than randomizing the playlist

Share this post


Link to post
Share on other sites
CYCho

Please see if you have both zPlayList.txt and zPlaylist-Shuffle.txt files in your music folder. If not, please press Ctrl+Alt+u to update your playlist.

Share this post


Link to post
Share on other sites
m4rcin93

Bilgus, your script is good but i have one more question.

I want to add sound via button in GUI and function FileOpenDialog, not on the top script. How can i do it?

Share this post


Link to post
Share on other sites
Bilgus

create a button and call 

FileOpenDialog ( "title", "init dir", "filter" [, options = 0 [, "default name" [, hwnd]]] )

Share this post


Link to post
Share on other sites
careca

There is a lot that can be improved. Nice first try.

I prefer a different approach to the playlist, instead of having 2 playlists one for sequencial and the random (random didn't work btw), i think would be better to only have one list, random would jump around the list.

Also, would be nice to be able to open the playlist and just double click any item to play.

Also, when i close the gui, running as a script, i expect the sound to stop, only stopped when i closed autoit.

A music player is hard work, i respect what you did so far.


Spoiler

Paster - Main function is to paste text, but has more functions. (No longer mantained, switched to String Trigger)

Renamer - Rename files and folders, remove portions of text from the filename etc.

GPO Tool - Export/Import Group policy settings.

MirrorDir - Synchronize/Backup/Mirror Folders

BeatsPlayer - Music player.

Params Tool - Right click an exe to see it's parameters or execute them.

String Trigger - Triggers pasting text or applications or internet links on specific strings.

Inconspicuous - Hide files in plain sight, not fully encrypted.

Regedit Control - Registry browsing history, quickly jump into any saved key.

Time4Shutdown - Write the time for shutdown in minutes.

Power Profiles Tool - Set a profile as active, delete, duplicate, export and import.

Firefox Profile Backup - Backup/restore previously saved profile.

Finished Task Shutdown - Shuts down pc when specified window/Wndl/process closes.

NetworkSpeedShutdown - Shuts down pc if download speed goes under "X" Kb/s.

IUIAutomation - Topic with framework and examples

Au3Record.exe

Share this post


Link to post
Share on other sites
CYCho

careca, Thank you for your comments. I really appreciate hearing from an expert like you.

I intentionally have 2 playlists, because sometimes I want to know what title comes next and I wnat to make it certain that the random playlist plays all the files.

Double click on the playlist currently plays the music.

The player can be closed in 3 ways: Ctrl+Alt+End, Click on the GUI player exit button, and tray icon menu. Closing the GUI will not terminate the program. It was one of my concept to let it play the music in the background, while I do other work on the computer, without consuming any of my desktop assets such as the screen and task bar. I tried hard to make the player use as little CPU as possible.

Yes, I agree it is a hard task. I am learning a lot by doing this.

Share this post


Link to post
Share on other sites
careca
Quote

I really appreciate hearing from an expert like you

Thank you, that's flattering, but im not an expert. :P There are experts in this forum tho.

Quote

It was one of my concept to let it play the music in the background

What if i close the gui and then want to change the song? or stop it, or change volume? :\

Double click is not working for me, doesn't do anything.

What do you want to do next?

Quote

I am learning a lot by doing this.

That's the best thing, i sure did.

It's only when you program something, you can appreciate the work done in some applications you see.

Sometimes, something small can take a lot of time to implement, and debug.

Edited by careca

Spoiler

Paster - Main function is to paste text, but has more functions. (No longer mantained, switched to String Trigger)

Renamer - Rename files and folders, remove portions of text from the filename etc.

GPO Tool - Export/Import Group policy settings.

MirrorDir - Synchronize/Backup/Mirror Folders

BeatsPlayer - Music player.

Params Tool - Right click an exe to see it's parameters or execute them.

String Trigger - Triggers pasting text or applications or internet links on specific strings.

Inconspicuous - Hide files in plain sight, not fully encrypted.

Regedit Control - Registry browsing history, quickly jump into any saved key.

Time4Shutdown - Write the time for shutdown in minutes.

Power Profiles Tool - Set a profile as active, delete, duplicate, export and import.

Firefox Profile Backup - Backup/restore previously saved profile.

Finished Task Shutdown - Shuts down pc when specified window/Wndl/process closes.

NetworkSpeedShutdown - Shuts down pc if download speed goes under "X" Kb/s.

IUIAutomation - Topic with framework and examples

Au3Record.exe

Share this post


Link to post
Share on other sites
CYCho
Quote

What if i close the gui and then want to change the song? or stop it, or change volume? :\

I use hot keys. Several hot keys are defined in the main window: play/pause, repeat/unrepeat, move forward/backward, volume up/down, show/hide windows, etc.

Quote

Double click is not working for me, doesn't do anything.

I introduced this function rather recently after my system was upgraded to Windows 10, so I could not test this under Windows 7. Are you on Windows 10?

Quote

What do you want to do next?

I am learning how to play videos from a mixed list of local files and internet URIs. I asked a question on this forum, but I have not heard an answer. Maybe it's beyond my scope.

 

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

×