MP3 Player

BlitzMax Forums/BlitzMax GUI Programming/MP3 Player

A friend has asked me to write a little program that enables him to navigate his hard drive, display *.mp3 files, and when a file is clicked it starts playing.
From what i have read on other threads i just need to launch the external player for the file.
Problem is I am busy working on another project and i don't really have the time for this at the moment.
Does anyone have some source code to do this? nothing flash, just something functional.
Can i use your code?
I have BlitzMax and MaxGUI.
I would prefer the source code so i can learn from it later, but even an exe file would be fine so i can keep my friend happy.

Cheers
Glenn

ExecFile(pathToMp3:string)

will do it or if it needs to run side a side with your applications, look into pub freeprocess

Download the B3D demo, use this source.

I was thinking more along the lines of a gui like the one i just whipped up in LogicGui below, where i can navigate the drive, then chose a file and play it.
Dreamora - Your step is what i need behind the button press.
TonyG - that will help too.

I guess if someone can tell me how to populate the 3 lists (Drive, Folder, Files) then i am 95% of the way there.
Like i said, i don't want to spend much time on this as i am doing something far more interesting to me, plus i don't even have any mp3 files to test it with, so if anyone can fill in the blanks with existing code then that would be appreciated.
Yes i know it is lazy but it is likely to be about 2 months before i am free to do it properly myself, plus i haven't done much of this maxgui stuff yet.
so any help would be appreciated.


'Source Code created on 09 Aug 2007 00:38:19 with Logic Gui Version 2.1 Build 248
'Glenn Dodd

Local MainWindow:TGadget = CreateWindow:TGadget("Logic_Gui",0,0,1280,968,Null,WINDOW_TITLEBAR|WINDOW_RESIZABLE |WINDOW_MENU |WINDOW_STATUS |WINDOW_CLIENTCOORDS )
		Local ChildWindow2:TGadget = CreateWindow:TGadget("ChildWindow2",228,52,487,434,MainWindow:TGadget,WINDOW_TITLEBAR|WINDOW_TOOL |WINDOW_CHILD)
			Local Canvas1:TGadget = CreateCanvas:TGadget(11,-12,423,388,ChildWindow2:TGadget,Null)
				Local lstDrive:TGadget = CreateListBox:TGadget(11,24,123,133,Canvas1:TGadget,Null)
				Local lstFolders:TGadget = CreateListBox:TGadget(12,168,125,206,Canvas1:TGadget,Null)
				Local lstFiles:TGadget = CreateListBox:TGadget(157,15,247,313,Canvas1:TGadget,Null)
				Local butPlayMe:TGadget = CreateButton:TGadget("Play the Selected File",160,361,127,12,Canvas1:TGadget,BUTTON_PUSH)

Repeat
	WaitEvent()
	Select EventID()
		Case EVENT_WINDOWCLOSE
			Select EventSource()
				Case MainWindow	MainWindow_WC( MainWindow:TGadget )
				Case ChildWindow2	ChildWindow2_WC( ChildWindow2:TGadget )
			End Select

		Case EVENT_GADGETACTION
			Select EventSource()
				Case lstDrive	lstDrive_GA( lstDrive:TGadget , EventData() , EventExtra:Object() )
				Case lstFolders	lstFolders_GA( lstFolders:TGadget , EventData() , EventExtra:Object() )
				Case lstFiles	lstFiles_GA( lstFiles:TGadget , EventData() , EventExtra:Object() )
				Case butPlayMe	butPlayMe_GA( butPlayMe:TGadget )
			End Select

		Case EVENT_GADGETSELECT
			Select EventSource()
				Case lstDrive	lstDrive_GS( lstDrive:TGadget , EventData() , EventExtra:Object() )
				Case lstFolders	lstFolders_GS( lstFolders:TGadget , EventData() , EventExtra:Object() )
				Case lstFiles	lstFiles_GS( lstFiles:TGadget , EventData() , EventExtra:Object() )
			End Select

		Case EVENT_GADGETMENU
			Select EventSource()
				Case lstDrive	lstDrive_GM( lstDrive:TGadget , EventData() , EventExtra:Object() , MainWindow:TGadget )
			End Select

	End Select
Forever

Function MainWindow_WC( Window:TGadget )
	DebugLog "Window MainWindow wants to be closed"
'	HideGadget( Window:TGadget )

	End
End Function

Function ChildWindow2_WC( Window:TGadget )
	DebugLog "Window ChildWindow2 wants to be closed"
'	HideGadget( Window:TGadget )

	End
End Function

Function lstDrive_GA( ListBox:TGadget , Number:Int , Extra:Object )
	DebugLog "ListBox lstDrive double clicked an Item " + Number
    If Number => 0 DebugLog "Selected Text = "+ GadgetItemText( ListBox:TGadget , Number:Int )
	
End Function

Function lstFolders_GA( ListBox:TGadget , Number:Int , Extra:Object )
	DebugLog "ListBox lstFolders double clicked an Item " + Number
    If Number => 0 DebugLog "Selected Text = "+ GadgetItemText( ListBox:TGadget , Number:Int )
	
End Function

Function lstFiles_GA( ListBox:TGadget , Number:Int , Extra:Object )
	DebugLog "ListBox lstFiles double clicked an Item " + Number
    If Number => 0 DebugLog "Selected Text = "+ GadgetItemText( ListBox:TGadget , Number:Int )
	
End Function

Function butPlayMe_GA( Button:TGadget )
	DebugLog "Button butPlayMe was pressed"
	
End Function

Function lstDrive_GS( ListBox:TGadget , Number:Int , Extra:Object )
	DebugLog "ListBox lstDrive single clicked an Item"
	
End Function

Function lstFolders_GS( ListBox:TGadget , Number:Int , Extra:Object )
	DebugLog "ListBox lstFolders single clicked an Item"
	
End Function

Function lstFiles_GS( ListBox:TGadget , Number:Int , Extra:Object )
	DebugLog "ListBox lstFiles single clicked an Item"
	
End Function

Function lstDrive_GM( ListBox:TGadget , Number:Int , Extra:Object , Window:TGadget=Null )
	DebugLog "ListBox lstDrive right clicked an Item"
	
End Function



There are a couple of codes in the code archieves that finds all files and folders, so you might want to take a peek there. I doubt that someone is going to make this for you though.

I just figured someone would already have done this.
Found the code archives you were talking about so i guess i will do it this weekend. Got to keep the friend happy or the missus will just nag me...

Cheers
Glenn

I'm pretty sure winamp can do this via the media library.

A bit too hacky and too much globals but works:

'Source Code created on 09 Aug 2007 00:38:19 with Logic Gui Version 2.1 Build 248
'Glenn Dodd



Global MainWindow:TGadget = CreateWindow:TGadget("Logic_Gui",0,0,1280,968,Null,WINDOW_TITLEBAR|WINDOW_RESIZABLE |WINDOW_MENU |WINDOW_STATUS |WINDOW_CLIENTCOORDS )
Global ChildWindow2:TGadget = CreateWindow:TGadget("ChildWindow2",228,52,487,434,MainWindow:TGadget,WINDOW_TITLEBAR|WINDOW_TOOL |WINDOW_CHILD)
Global Canvas1:TGadget = CreateCanvas:TGadget(11,-12,423,388,ChildWindow2:TGadget,Null)
Global lstDrive:TGadget = CreateListBox:TGadget(11,24,123,133,Canvas1:TGadget,Null)
Global lstFolders:TGadget = CreateListBox:TGadget(12,168,125,206,Canvas1:TGadget,Null)
Global lstFiles:TGadget = CreateListBox:TGadget(157,15,247,313,Canvas1:TGadget,Null)
Global butPlayMe:TGadget = CreateButton:TGadget("Play the Selected File",160,361,127,12,Canvas1:TGadget,BUTTON_PUSH)
Global butStopMe:TGadget = CreateButton:TGadget("Stop the Selected File",360,361,127,12,Canvas1:TGadget,BUTTON_PUSH)


' find all drive names
For i = Asc("C") To Asc("Z")
  Local key:String = Chr(i)+":"
  dir=ReadDir(key)
	If dir
		AddGadgetItem lstDrive, key
	EndIf
Next


Global thisDrive:String
Global thisFolder:String
Global thisFile:String

Global process:TProcess


Function ListAll()
	Local files$[]


	files=LoadDir(thisDrive +  thisFolder,  False)

	Print thisDrive + thisFolder

	ClearGadgetItems lstFolders
	ClearGadgetItems lstFiles

	For t$=EachIn files
		Local f:String = thisDrive + thisFolder + "" + t
		If FileType(f) = 2
			AddGadgetItem lstFolders, t
		Else
			If ExtractExt(f) = "mp3"
				AddGadgetItem lstFiles, t
			EndIf
		EndIf
	Next
EndFunction


Function playMusic()
	Local prg:String = "mplayer\mplayer.exe"
	Local file:String = thisDrive + thisFolder + "" + thisFile
	
	Local calltext:String  = prg + " " +  Chr(34)+file+Chr(34)
	Print calltext
	
	stopMusic()
	process=CreateProcess(calltext)

	' stop with process.terminate()
	Rem
	Repeat
		If process.status() = 0 Then Exit
		Local error:String = process.err.readline()
		If error <> "" Then
			Print error
		EndIf
	Forever
	End Rem
End Function

Function stopMusic()
	If process Then process.terminate()
EndFunction


Repeat
	WaitEvent()
	Select EventID()
		Case EVENT_WINDOWCLOSE
			Select EventSource()
				Case MainWindow	MainWindow_WC( MainWindow:TGadget )
				Case ChildWindow2	ChildWindow2_WC( ChildWindow2:TGadget )
			End Select

		Case EVENT_GADGETACTION
			Select EventSource()
				Case lstDrive	lstDrive_GA( lstDrive:TGadget , EventData() , EventExtra:Object() )
				Case lstFolders	lstFolders_GA( lstFolders:TGadget , EventData() , EventExtra:Object() )
				Case lstFiles	lstFiles_GA( lstFiles:TGadget , EventData() , EventExtra:Object() )
				Case butPlayMe	butPlayMe_GA( butPlayMe:TGadget )
				Case butStopMe	butStopMe_GA( butStopMe:TGadget )
			End Select

		Case EVENT_GADGETSELECT
			Select EventSource()
				Case lstDrive	lstDrive_GS( lstDrive:TGadget , EventData() , EventExtra:Object() )
				Case lstFolders	lstFolders_GS( lstFolders:TGadget , EventData() , EventExtra:Object() )
				Case lstFiles	lstFiles_GS( lstFiles:TGadget , EventData() , EventExtra:Object() )
			End Select

		Case EVENT_GADGETMENU
			Select EventSource()
				Case lstDrive	lstDrive_GM( lstDrive:TGadget , EventData() , EventExtra:Object() , MainWindow:TGadget )
			End Select

	End Select
Forever

Function MainWindow_WC( Window:TGadget )
	DebugLog "Window MainWindow wants to be closed"
'	HideGadget( Window:TGadget )

	End
End Function

Function ChildWindow2_WC( Window:TGadget )
	DebugLog "Window ChildWindow2 wants to be closed"
'	HideGadget( Window:TGadget )

	End
End Function

Function lstDrive_GA( ListBox:TGadget , Number:Int , Extra:Object )
	DebugLog "ListBox lstDrive double clicked an Item " + Number
    If Number => 0 DebugLog "Selected Text = "+ GadgetItemText( ListBox:TGadget , Number:Int )

	thisDrive = GadgetItemText( ListBox:TGadget , Number:Int )
	thisFolder = ""
	ListAll()

End Function

Function lstFolders_GA( ListBox:TGadget , Number:Int , Extra:Object )
	DebugLog "ListBox lstFolders double clicked an Item " + Number
    If Number => 0 DebugLog "Selected Text = "+ GadgetItemText( ListBox:TGadget , Number:Int )


	If GadgetItemText( ListBox:TGadget , Number:Int ) = ".."
		thisFolder = thisFolder[..thisfolder.findlast("")]
	Else
		thisFolder = thisFolder + "" + GadgetItemText( ListBox:TGadget , Number:Int ) 
	EndIf
	
	ListAll()

End Function

Function lstFiles_GA( ListBox:TGadget , Number:Int , Extra:Object )
	DebugLog "ListBox lstFiles double clicked an Item " + Number
    If Number => 0 DebugLog "Selected Text = "+ GadgetItemText( ListBox:TGadget , Number:Int )
	
End Function

Function butStopMe_GA( Button:TGadget )
	DebugLog "Button butPlayMe was pressed"
	stopMusic()
EndFunction

Function butPlayMe_GA( Button:TGadget )
	DebugLog "Button butPlayMe was pressed"
	playMusic()
End Function

Function lstDrive_GS( ListBox:TGadget , Number:Int , Extra:Object )
	DebugLog "ListBox lstDrive single clicked an Item"
	
End Function

Function lstFolders_GS( ListBox:TGadget , Number:Int , Extra:Object )
	DebugLog "ListBox lstFolders single clicked an Item"
	
End Function

Function lstFiles_GS( ListBox:TGadget , Number:Int , Extra:Object )
	DebugLog "ListBox lstFiles single clicked an Item"
	thisFile = GadgetItemText( ListBox:TGadget , Number:Int )
	' Notify thisDrive + thisFolder + "" + thisFile
	
End Function

Function lstDrive_GM( ListBox:TGadget , Number:Int , Extra:Object , Window:TGadget=Null )
	DebugLog "ListBox lstDrive right clicked an Item"
	
End Function




Put the mplayer in the same directory.

*updated*

Thanks Dirk,
I will tidy up the gui code a bit this weekend and repost the resulting file for anyone else to use.

Cheers
Glenn

Glenn, no problem.

I just checked and found the 'listall' function empty, obviously a copy&paste error on my side.

I updated the code box above, please re-save the file, and tell me if it works.

yes that works.

Thanks very much Dirk

I assume Max still doesn't support the playing of mp3 files directly?

yes. but with a little effort you could try to get Bass running like here:
http://www.blitzmax.com/Community/posts.php?topic=71229#800336
which supports mp3.