Play mp3-file using mcisendstring

Started by Theo Gottwald, September 06, 2023, 12:15:24 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Theo Gottwald

'--------------------------------------------------------------------------------------------
DECLARE FUNCTION GetShortPathName LIB "kernel32.dll" ALIAS "GetShortPathNameA" ( _
    BYVAL lpszLongPath AS ASCIIZ*%MAX_PATH, _
    BYVAL lpszShortPath AS ASCIIZ*%MAX_PATH, _
    BYVAL cchBuffer AS DWORD _
) AS DWORD


DECLARE FUNCTION mciSendString LIB "winmm.dll" ALIAS "mciSendStringA" ( _
    BYVAL lpstrCommand AS STRING, _
    BYVAL lpstrReturnString AS STRING, _
    BYVAL uReturnLength AS DWORD, _
    BYVAL hwndCallback AS DWORD) AS DWORD
'##################################################################################################
'
'##################################################################################################

SUB PlayMP3(BYVAL MP3FilePath AS STRING)
    LOCAL Result AS DWORD
    LOCAL Fname AS STRING, lpszLongPath AS ASCIIZ * %MAX_PATH + 1, lpszShortPath AS ASCIIZ * %MAX_PATH + 1
    LOCAL shortPathResult AS DWORD
    LOCAL ErrorMessage AS STRING

    ' Initialize
    ErrorMessage = "Success"

    ' Check if the file exists
    IF ISFILE(MP3FilePath) = 0 THEN
        ErrorMessage = "Error: File does not exist!"
        GOTO ExitPoint
    END IF
    Fname = MP3FilePath

    ' Open and play the MP3 file
    Result = mciSendString( "open " + $DQ + Fname + $DQ + " type mpegvideo alias mp3player", "", 0, 0 )
    IF Result = 0 THEN
        mciSendString "play mp3player", "", 0, 0
    ELSE
        ErrorMessage = "Error: Failed to play the MP3 file!"
    END IF

    ' Single exit point
ExitPoint:
    IF ErrorMessage <> "Success" THEN
        Set_Error(ErrorMessage)
    END IF
END SUB
'##################################################################################################
'
'##################################################################################################
                 
'--------------------------------------------------------------------------------------------
' Define named constants for better readability
%GET_LENGTH = 1
%OPEN_PLAY = 2
%PAUSE = 3
%RESUME = 4
%STOP = 5
%CLOSE = 6

FUNCTION MP_PlayControl(BYVAL Cmd AS LONG, OPT BYVAL MP3FilePath AS STRING) AS LONG
  LOCAL szRetString AS ASCIIZ * 256, lpszLongPath AS ASCIIZ * %MAX_PATH + 1, lpszShortPath AS ASCIIZ * %MAX_PATH + 1
  LOCAL MediaLength AS LONG, Result AS DWORD, FuncResult AS LONG
  LOCAL Fname AS STRING, CurrentVolume AS LONG, Artist AS STRING, Album AS STRING, SongTitle AS STRING, PlayLocation AS STRING, CurrentMP3FileName AS STRING
  LOCAL shortPathResult AS DWORD

  ' Initialize function result to a default value
  FuncResult = 0

  ' Set your CurrentVolume, PlayLocation, CurrentMP3FileName, Artist, Album, SongTitle here if needed
  ' ...

  ' Determine the file name to use
  IF LEN(MP3FilePath) = 0 THEN
    Fname = PlayLocation + CurrentMP3FileName
  ELSE
    Fname = MP3FilePath
  END IF

  ' Check if the file exists
  IF ISFILE(Fname) = 0 THEN
    FUNCTION = -1
    EXIT FUNCTION
  END IF

  SELECT CASE Cmd
    CASE %GET_LENGTH
      ' Get the length of the song
      lpszLongPath = Fname

shortPathResult = GetShortPathName( lpszLongPath, lpszShortPath, %MAX_PATH + 1 )
IF shortPathResult THEN
    Fname = lpszShortPath
END IF

      Result = mciSendString( "open " + $DQ + Fname + $DQ + " alias mp3", "", 0, 0 )
      IF Result <> 0 THEN
        FuncResult = -1
        GOTO ExitPoint
      ELSE
        mciSendString "Set MP3 time format milliseconds", "", 0, 0
        mciSendString "Status MP3 length", szRetString, BYVAL 256, 0
        MediaLength = CLNG( VAL( szRetString )) \ 1000
        mciSendString "close mp3", "", 0, 0
        FuncResult = MediaLength
      END IF

    CASE %OPEN_PLAY
      ' Open and play the song
      MciSendString "open " + $DQ + Fname + $DQ, "", 0, 0
      MciSendString "play " + $DQ + Fname + $DQ, "", 0, 0
      MciSendString "setaudio " + $DQ + Fname + $DQ + " volume to" + STR$( CurrentVolume ), "", 0, 0

    CASE %PAUSE
      ' Pause the song
      MciSendString "pause " + $DQ + Fname + $DQ, "", 0, 0

    CASE %RESUME
      ' Resume the song
      MciSendString "resume " + $DQ + Fname + $DQ, "", 0, 0

    CASE %STOP
      ' Stop the song
      MciSendString "stop " + $DQ + Fname + $DQ, "", 0, 0

    CASE %CLOSE
      ' Close the song
      MciSendString "close " + $DQ + Fname + $DQ, "", 0, 0

    CASE ELSE
      ' Unknown command
      FuncResult = -1
  END SELECT

  ' Single exit point
ExitPoint:
  FUNCTION = FuncResult

END FUNCTION
                   
'-------------------------------------------------------

I could have used Google, but i used A.I. because it will not only give me a Link but the complete Subprogram.

Zlatko Vid

you could play AVI file to using same method and Richedit control

Theo Gottwald

I have just added the MP3-Player Subprogram.

'##################################################################################################
'
'##################################################################################################

SUB PlayMP3(BYVAL MP3FilePath AS STRING)
    LOCAL Result AS DWORD
    LOCAL Fname AS STRING, lpszLongPath AS ASCIIZ * %MAX_PATH + 1, lpszShortPath AS ASCIIZ * %MAX_PATH + 1
    LOCAL shortPathResult AS DWORD
    LOCAL ErrorMessage AS STRING

    ' Initialize
    ErrorMessage = "Success"

    ' Check if the file exists
    IF ISFILE(MP3FilePath) = 0 THEN
        ErrorMessage = "Error: File does not exist!"
        GOTO ExitPoint
    END IF
    Fname = MP3FilePath

    ' Open and play the MP3 file
    Result = mciSendString( "open " + $DQ + Fname + $DQ + " type mpegvideo alias mp3player", "", 0, 0 )
    IF Result = 0 THEN
        mciSendString "play mp3player", "", 0, 0
    ELSE
        ErrorMessage = "Error: Failed to play the MP3 file!"
    END IF

    ' Single exit point
ExitPoint:
    IF ErrorMessage <> "Success" THEN
        Set_Error(ErrorMessage)
    END IF
END SUB
'##################################################################################################
'
'##################################################################################################