Drag and drop music?
BlitzMax Forums/BlitzMax Beginners Area/Drag and drop music?
| ||
| Hi all, I'd like to be able to drag and drop a music file into my program. Don't want to do anything with it, just get it in and load it with LoadSound. I know how to read strings, but I'm not sure what to do with EventExtra for music. Thanks, Bookworm |
| ||
Inside the event loop:Case EVENT_WINDOWACCEPT Select EventSource() Case Window1 DropFile( Window1:TGadget , String(EventExtra:Object()) ) End Select In your drop function: Function DropFile ( Window:TGadget , Drop:String ) DebugLog "Window 1 got "+Drop+" dropped onto!" Local Ext$=ExtractExt$( Drop$ ) Select Ext.toLower() Case "wav" Do_Something_With_Music() End Select End Function |
| ||
| What jsp said, except don't forget that you need to to create the window in MaxGUI with the WINDOW_ACCEPTFILES flag too... |
| ||
| Thanks, but I've already decided it'd be better to simply have a textbox to put the filename in anyhow. More intuitive for most people. I am having another problem, though: when I compile on Windows, the text box and everything except the buttons and status text at the top (Music playing, music paused, etc) don't appear. When a button is clicked, most of the other buttons disappear as well. I can't figure out what X/Y coords to use to get them all to appear... some help? Here's the full code
'set appearance
Global Style:Int = WINDOW_TITLEBAR|WINDOW_MENU|WINDOW_STATUS|WINDOW_RESIZABLE
Global version:String = "0.9.1"
'music variables
Global music:TSound
Global channel:TChannel
Global paused:Int = 0
channel = AllocChannel()
'create window
Global mainWindow:TGadget=CreateWindow("Bookworm's OGG Player - Version "+version$,100,100,500,200,Null,Style)
'create music playing label
Global musicFile:TGadget=CreateLabel("No music playing...",10,10,900,100,mainWindow)
'create exit/reset
Global pExit:TGadget=CreateButton("Quit",430,5,50,20,mainWindow)
Global pReset:TGadget=CreateButton("Reset",350,5,50,20,mainWindow)
'create music file drop area
Global musicDrop:TGadget=CreateLabel("Enter music file here: ",10,50,20,20,mainWindow)
Global musicDropBox:TGadget=CreateTextField(100,20,200,20,mainWindow)
Global musicInfo:TGadget=CreateLabel("Current File: "+TextFieldText(musicDropBox), 10,80,200,200,mainWindow)
'create music options buttons
Global musicPlay:TGadget=CreateButton("Play!",350,35,50,20,mainWindow)
Global musicPause:TGadget=CreateButton("Pause",430,35,50,20,mainWindow)
Global musicStop:TGadget=CreateButton("Stop",350,65,50,20,mainWindow)
'main loop
Repeat
WaitEvent()
Select EventID()
'so you want to end it all?
Case EVENT_WINDOWCLOSE
End
Case EVENT_GADGETACTION
Select EventSource()
Case pExit
SetGadgetText musicFile,"Exiting..."
End
Case musicDropBox
SetGadgetText musicFile,"Attempting to load..."
SetGadgetText musicInfo,"Current File: "+TextFieldText(musicDropBox)
Case pReset
SetGadgetText musicFile,"No music playing..."
SetGadgetText musicDropBox,""
SetGadgetText musicInfo,"Current File: "
stopCurrent()
Case musicPlay
playCurrent()
Case musicPause
pauseCurrent()
Case musicStop
stopCurrent()
End Select
End Select
Forever
Function playCurrent()
music = LoadSound(TextFieldText(musicDropBox))
If music = Null
SetGadgetText musicFile,"Error: File Not Found!"
Else
PlaySound music,channel
SetGadgetText musicFile,"Music Playing!"
EndIf
End Function
Function stopCurrent()
StopChannel channel
SetGadgetText musicFile,"Music Stopped"
channel = AllocChannel()
If music = Null
SetGadgetText musicFile,"Error: File Not Found!"
EndIf
End Function
Function pauseCurrent()
If paused = 0
PauseChannel channel
SetGadgetText musicPause,"Resume"
SetGadgetText musicFile,"Music Paused"
If music = Null
SetGadgetText musicFile,"Error: File Not Found!"
EndIf
paused =1
Else If paused = 1
ResumeChannel channel
SetGadgetText musicPause,"Pause"
SetGadgetText musicFile,"Music Playing!"
If music = Null
SetGadgetText musicFile,"Error: File Not Found!"
EndIf
paused =0
EndIf
End Function
And a downloadable version: http://rybookrs.onlyplace4.com/forum/projects/bogg/bogg.bmx Thanks again! |
| ||
| Hi bookworm, The reason why your code wasn't displaying properly on Windows was because your labels were drawn so big that they were drawn over your other gadgets. On Mac OS X, as labels are transparent, it didn't really matter, but labels on Windows are drawn opaque and as such hid your other gadgets. From the looks of your gadget-code, it looks like you are having a hard time working out how to position your gadgets on the screen. Can I suggest you use a GUI Editor (such as LogicGUI [see Jsp's sig] or GUIde) which allows you to draw your control how you want and allows you to export the equivalent code? If you are set on doing it yourself, then I suggest you make use of the WINDOW_CLIENTCOORDS flag when creating windows to ensure your window looks the same on all platforms... I've quickly re-designed your interface if you are really stuck or are looking for some pointers: P.S. Oh, and I've also added some drag n' drop handling code as to me drag n' drop is more intuitive than typing in the file-path yourself... Still, each to their own I suppose. |
| ||
| @SebHoll I think you should at least try strict. I get a couple of bugs when running it. They don't show until a file start to play. |
| ||
| It's not a bad idea to switch label borders on when laying out GUI elements by hand. |
| ||
| @ Jesse: I always use SuperStrict in my own code, it's just as bookwork99 seems like a bit of a BlitzMax/MaxGUI beginner, it might make matters worse if he's trying to get to grasps with the syntax etc. All I did was simply re-arrange his gadgets - the rest of his code I kept the same was which wasn't Strict or SuperScript compliant. Ooops, nearly forgot the actual reason why I posted... @ bookworm99: You might want to check out Assari's fantastic MaxGUI Tutorials. |
| ||
| On the contrary I think BookWorm99 should start of by using "superstrict". it stops some errors dead on their tracks and others easier to hunt. |
| ||
| On the contrary I think BookWorm99 should start of by using "superstrict" Absolutely. As you say, it picks up all kinds of silly errors that are not obvious. Event though it might slow down a beginner getting their code to compile at first, they'll be better off in the long run, by understand exactly what can and can't be done. |