'===========================================================================
' Subject: SONIDO 1.0 (MP3 PLAYER)            Date: 08-29-00 (01:50)       
'  Author: Gerome Guillemin                   Code: RAPIDQ                 
'  Origin: gedd123@free.fr                  Packet: RAPIDQ.ABC
'===========================================================================
' Sonido 1.0 : hidden MCI command to play MP3 Files with PC equipped with Windows 9.x, NT or Win2k
' equipped with MS-Media Player 2 or higher
' Author : Gerome GUILLEMIN (FRANCE)
' Mailto : gedd123@free.fr
' Web page : http://gedd123.free.fr (My own MP3 rippers/encoders, Rapid-Q source code...)
' Feel free to use this snippet to play your favorite MP3 files
' Started : 08-08-2000  - Modified 08-29-2000 at 10.30 PM (huh... what a good programming time :)
' This code is dedicated to William's Great Rapid-Q compiler and for all basic code lovers !
' This code is also dedicated to ABC packets if William decides to packet it :)
' BUG fixed in this release : shorten paths & mp3 filename oops... :(
' Improvements : Added a way to 'registrize' MP3 files with Sonido :) 
' THIS CODE HAS BEEN FULLY DOCUMENTED - TELL ME IF SOMETHING IS MISSING - THANKS A LOT -

' Security / Optimizations
$Include "Rapidq.inc" 
$TypeCheck ON   ' Useful for me :) 
$Optimize ON    ' Yeah...
$Option Level 3 ' For Fun

' Global Definitions / Prototypes
$Define FALSE  0
$Define TRUE   1
DECLARE SUB Button1Click (Sender AS QBUTTON)
DECLARE SUB Button2Click (Sender AS QBUTTON)
DECLARE SUB Button3Click (Sender AS QBUTTON)

' Main API Declaration
DECLARE Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" _ 
(ByVal lpstrCommand As String, ByVal lpstrReturnString As String, ByVal uReturnLength As Long, _ 
ByVal hwndCallback As Long) As Long

' Globals
Dim ret as Long            ' Returned value for 'mciSendString'
Dim mp3file As String      ' Name of the MP3 to play
Dim nCounter as Integer    ' Counter to manage the action Playing/Pausing/Continuing...
Dim szCmd as string        ' MCI Command -> Open
Dim szPlay as String       ' MCI Command -> Play Sonido
Dim szRegCmd as String     ' String for MP3 File description for the Registry

' My constants 
mp3file = command$(1) 'old code was this (hard coded) -> "D:\REGS\Rapid-Q\Projets\GG_Works\Sonido\01-Zarathoustra.mp3" '-> change this to a valid MP3 filename from your PC
szCmd = "Open " & chr$(34) & mp3file & chr$(34) & " Alias Sonido"
szPlay = "Play sonido"

Const HKEY_CLASSES_ROOT = &H80000000     ' Registry constant (William has forgotten to document this one !)

$EscapeChars ON 'Put this on the Top of the code to avoid conflicts !

Dim Registry as Qregistry    

CREATE Form AS QFORM
    Caption = "Sonido : FREE MP3 Player"
    Width = 320
    Height = 115
    Center
    CREATE Button1 AS QBUTTON
        Caption = "Play !"
        Left = 9
        Top = 10
        OnClick = Button1Click
    END CREATE
    CREATE Button2 AS QBUTTON
        Caption = "Stop !"
        Left = 89
        Top = 10
        TabOrder = 1
        OnClick = Button2Click
    END CREATE
    CREATE Button3 AS QBUTTON
            Caption = "Associate MP3@Sonido !"
        Left = 170        
        Top = 10
        Width = 135
        TabOrder = 1
        OnClick = Button3Click
    END CREATE
    
    CREATE Label1 as Qlabel
        Align = 2
        Alignment = 2 
        Color = &hFFFF
        Caption = "By Gérôme GUILLEMIN 08-08-2000\nMailto: gedd123@free.fr\nWeb: http://gedd123.free.fr" '-> fatidic date : 11 years ago i've succeded my driving code license :) 
        Left = 29 '-> will be my age the 12th of August 2000 ;)
        Top = 40  '-> cool height for my screen :)
    END CREATE
END CREATE

'Initialization code
if Command$(1) <> "" then
    ret = mciSendString(szCmd, 0, 0, 0)          ' Open the MP3 file
    ret = mciSendString(szPlay, 0, 0, 0)         ' Plays the MP3 file !
        Button1.Caption = "Playing..." : nCounter = 1
End if   

Form.ShowModal


'--------- Subroutines ---------

SUB Button1Click (Sender AS QBUTTON)

'nCounter manages the action for the MP3 -> 0 == Plays or Continue // 1 == Pauses the MP3
Select Case nCounter
 
 Case 0
   ret = mciSendString(szCmd, 0, 0, 0) 'Open the MP3 file
      if ret <> 0 AND Button1.Caption = "Play !" then
         Showmessage ("Invalid Handle -> MP3 File is not present or may be an invalid MP3 file !")
         Button1.Enabled = FALSE
      else
         '? ret 'left for debugging
      end if
    Button1.Caption = "Playing..." : nCounter = 1
    ret = mciSendString(szPlay, 0, 0, 0)         ' Plays the MP3 file !
 
 Case 1
    ret = mciSendString("Stop sonido", 0, 0, 0)  ' Pause (if we want to wait a while for a cup of tea :)
    Button1.Caption = "Paused !" : nCounter = 0
End Select
    
END SUB


SUB Button2Click (Sender AS QBUTTON)
    ret = mciSendString("Stop sonido", 0, 0, 0)  ' Pause (if we want to wait a while for a second cup of tea :)
    ret = mciSendString("Close sonido", 0, 0, 0) ' Stop Playing MP3 file (the file is rewinded at this moment)
END SUB


SUB Button3Click (Sender AS QBUTTON)      ' Associate MP3 files with Sonido !
szRegCmd = Command$(0) + " \34%1\34"      ' Full Path for Sonido + command

 Registry.RootKey = HKEY_CLASSES_ROOT
 Registry.OpenKey("\\.mp3", 1)
 Registry.WriteString("", "Sonido File")

 Registry.OpenKey("\\Sonido File", 1)
 Registry.WriteString("", "MP3 File")
 Registry.OpenKey("shell\\open\\command", 1)
 Registry.WriteString("", szRegCmd)
 
ShowMessage ("MP3 files successfully associated with Sonido :)\nJust click onto an MP3 file...\nEnjoy :-p")
Application.Terminate ' The 1st job is done... it's time to play mp3 now !
END SUB
