I figured it out, although I still need to make the code a little cleaner. I will make updates in the Delcare book and send it out to subscribers when updates are complete during my ‘free’ time
… Here are the steps to get you going.
First, open the mp3 file with this code:
Sub Action() Handles Action
Var RetVal as Integer //Returned value or error
Var SoundType as String //used by Windows player
Var FileExtension as String = ExtensionName(TFShellPath.Value) //determine file format
//Determine file extension and assign SoundType
//There is a long list, and only a few are shown
//Add more file extensions and sound types here
If FileExtension = "mp3" Then
SoundType = "MPEGVideo"
ElseIf FileExtension = "avi" Then
SoundType = "MPEGVideo"
ElseIf FileExtension = "wav" Then
SoundType = "Waveaudio"
ElseIf FileExtension = "mid" Then
SoundType = "Sequencer"
Else
MessageDialog.Show ("Sound type not supported. Try another sound extension.")
Return
End If
//Open the sound file in Windows
Var tmp as New MemoryBlock(128)
RetVal = mciSendString("open """ + TFShellPath.Value + """ type " + SoundType + " alias Sound_Device ", Tmp, 0, 0)
If Retval <> 0 then
MessageDialog.Show (ReturnErrorString(Retval))
Return
End If
End Sub
To get the song length in seconds, use this code:
Sub Action() Handles Action
Var RetVal as Integer //Returned value or error
Var tmp as New MemoryBlock(128)
RetVal = mciSendString("status Sound_Device length", Tmp, tmp.Size, 0)
Dim MyTime as UInt32
Dim d as double = Val(tmp.WString(0))
d = d/1000
System.DebugLog(d.ToString + "seconds")
End Sub
The mciSendString method was also changed to this:
Public Function mciSendString(lpszCommand as String, lpszReturnString as Ptr, cchReturn as UInt32, hWndCallback as Integer) As Integer
#If TargetWindows
Declare Function mciSendStringW lib "Winmm.dll" (lpszCommand as Wstring, _
lpszReturnString as Ptr, cchReturn as UInt32, hWndCallback as Integer) as Integer
Declare Function mciSendStringA lib "Winmm.dll" (lpszCommand as Cstring, _
lpszReturnString as Ptr, cchReturn as UInt32, hWndCallback as Integer) as Integer
If System.IsFunctionAvailable("mciSendStringW","Winmm.dll") Then
Return mciSendStringW(lpszCommand, lpszReturnString, cchReturn, hWndCallback)
Else
Return mciSendStringA(lpszCommand, lpszReturnString, cchReturn, hWndCallback)
End If
#Else
//System.DebugLog "This is not a Windows OS"
Return 0
#Endif
End Function
This works on 32-bit and 64-bit systems, tested on Windows 11, using Xojo 2023, r1
Edit: fix grammer