How to play OLE mid & wav-files?

Hello!

I have some mid and wav's I like to have embeded in my exe. As I know It's no way to use this type of files in a resource-file (*.res) in VB6. And if I don't know how to play them.
So I did try using the OLE-object, but I'm not familjar with it.
I have done this sub to play my files and it works OK with *.wav, but when playing *.mid the Mediaplayer gets visible and won't start and stop automatlicy. How to solve this? And is there a more proper way to solve this? My MSDN-helpfile have crached so I can't us it to read and solve it myself, the help in VB5 was mutch better....

Sub PlayMidWav(Index%)
On Error Resume Next
    If Not bSound Then Exit Sub
    OLE1(Index%).DoVerb
    Do 'To complete playing the sound.
        Me.SetFocus 'Otherwise my app's titlebar flashing.
        DoEvents
        If Not OLE1(Index%).AppIsRunning Then Exit Do
        If Err <> 0 Then
            bSound = False: mnuSound.Enabled = False
            mnuSound.Checked = False: Err.Clear: Exit Do
        End If
    Loop
End Sub


/Janne Sweden
LVL 1
janneaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

MirkwoodCommented:
____________________________________________________________________
              This goes in Form :
____________________________________________________________________

Option Explicit

Private Function AVI_Play(DLL_Path As String, ResourceNumber As Integer, Optional ResourceType As String = "CUSTOM", Optional PlayInParentObject As Boolean = False, Optional ParentHandle As Long = 0)
On Error Resume Next
   
  Dim hRsrc As Long
  Dim hGlobal As Long
  Dim lpString As String
   
  AVI_Stop
   
  If Dir(DLL_Path) = "" Then
    MsgBox DLL_Path & Chr(13) & Chr(13) & "Couldn't find .DLL resource to play.", vbOKOnly + vbExclamation, "  File Not Found"
  End If
   
  'Identifier for the AVI in the resource file from the resource.h
  'file of the resource dll, which must be preceded by '#'.
  lpString = "#" & CStr(ResourceNumber)
  If lpString = "#" Then
    GoTo ErrorTrap
  End If
   
  'Loads the resource
  'Change the filename argument in the next line to the path and
  'filename of your resource dll file.
  hInst = LoadLibrary(DLL_Path)
  If hInst = 0 Then
    GoTo ErrorTrap
  End If
   
  hRsrc = FindResource(hInst, lpString, "CUSTOM")
  If hRsrc = 0 Then
    GoTo ErrorTrap
  End If
   
  hGlobal = LoadResource(hInst, hRsrc)
  If hGlobal = 0 Then
    GoTo ErrorTrap
  End If
   
  lpData = LockResource(hGlobal)
  If lpData = 0 Then
    GoTo ErrorTrap
  End If
   
  fileSize = SizeofResource(hInst, hRsrc)
  If fileSize = 0 Then
    GoTo ErrorTrap
  End If
   
  Call mmioInstallIOProc(MEY, AddressOf IOProc, MMIO_INSTALLPROC + MMIO_GLOBALPROC)
   
  'Play the AVI file
  If PlayInParentObject = True And ParentHandle <> 0 Then
    mciSendString "open AVI.MEY+ type avivideo alias AVI parent " & ParentHandle & " style child", 0&, 0&, 0&
  Else
    mciSendString "open AVI.MEY+ type avivideo alias AVI", 0&, 0&, 0&
  End If
   
  mciSendString "play AVI", 0&, 0&, 0&
   
  Exit Function
   
ErrorTrap:
   
  MsgBox "An error occured while trying to load the .DLL file.  Make sure the .DLL is in the location specified, that it's a valid ActiveX .DLL resource file, and that the file hasn't been corrupted.", vbOKOnly + vbExclamation, "  Error While Loading"
   
End Function

Private Function AVI_Stop()
On Error Resume Next
   
  mciSendString "close AVI", 0&, 0&, 0&
   
  Call mmioInstallIOProc(MEY, vbNull, MMIO_REMOVEPROC)
  FreeLibrary hInst
   
End Function

Private Sub cmdPlay_Box_Click()
   
  AVI_Play App.Path & "\AVIDLL.dll", 101, , True, Picture1.hWnd
   
End Sub

Private Sub cmdPlay_Window_Click()
   
  AVI_Play App.Path & "\AVIDLL.dll", 101
   
End Sub

Private Sub cmdStop_Click()
   
  AVI_Stop
   
End Sub

Private Sub Form_Unload(Cancel As Integer)
   
  AVI_Stop
   
End Sub

____________________________________________________________________
              This goes in Module :
____________________________________________________________________

Option Explicit
 
Public lpData As Long
Public fileSize As Long
Public hInst As Long

Public Const MMIO_INSTALLPROC = &H10000   'mmioInstallIOProc: install MMIOProc
Public Const MMIO_GLOBALPROC = &H10000000 'mmioInstallIOProc: install globally
Public Const MMIO_READ = &H0
Public Const MMIOM_CLOSE = 4
Public Const MMIOM_OPEN = 3
Public Const MMIOM_READ = MMIO_READ
Public Const MMIO_REMOVEPROC = &H20000
Public Const MMIOM_SEEK = 2
Public Const SEEK_CUR = 1
Public Const SEEK_END = 2
Public Const SEEK_SET = 0
Public Const MEY = &H2059454D   'This is the value of "MEY " run through FOURCC

'Create a user defined variable for the API function calls
Public Type MMIOINFO
   dwFlags As Long
   fccIOProc As Long
   pIOProc As Long
   wErrorRet As Long
   htask As Long
   cchBuffer As Long
   pchBuffer As String
   pchNext As String
   pchEndRead As String
   pchEndWrite As String
   lBufOffset As Long
   lDiskOffset As Long
   adwInfo(4) As Long
   dwReserved1 As Long
   dwReserved2 As Long
   hmmio As Long
End Type

'Finds the specified resource in an executable file. The function
'returns a resource handle that can be used by other functions used
'to load the resource.
Public Declare Function FindResource Lib "kernel32" Alias "FindResourceA" (ByVal hInstance As Long, ByVal lpName As String, ByVal lpType As String) As Long

'Returns a global memory handle to a resource in the specified
'module. The resource is only loaded after calling the LockResource
'function to get a pointer to the resource data.
Public Declare Function LoadResource Lib "kernel32" (ByVal hInstance As Long, ByVal hResInfo As Long) As Long

'Locks the specified resource. The function returns a 32-bit pointer
'to the data for the resource.
Public Declare Function LockResource Lib "kernel32" (ByVal hResData As Long) As Long

'Loads the specified dll file and maps the address space for the current process.
Public Declare Function LoadLibrary Lib "kernel32" Alias "LoadLibraryA" (ByVal lpLibFileName As String) As Long

'Frees the specified .dll file loaded with the LoadLibrary function.
Public Declare Function FreeLibrary Lib "kernel32" (ByVal hLibModule As Long) As Long

'Copies a block of memory from one location to another.
Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (hpvDest As Any, hpvSource As Any, ByVal cbCopy As Long)

'Installs or removes a custom I/O procedure. This function also
'locates an installed I/O procedure, using its corresponding
'four-character code.
Public Declare Function mmioInstallIOProc Lib "winmm" Alias "mmioInstallIOProcA" (ByVal fccIOProc As Long, ByVal pIOProc As Long, ByVal dwFlags As Long) As Long

'Sends the specified command string to an MCI device.
Public Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" (ByVal lpstrCommand As String, ByVal lpstrReturnString As Long, ByVal uReturnLength As Long, ByVal hwndCallback As Long) As Long

'Returns the size, in bytes, of the specified resource.
Public Declare Function SizeofResource Lib "kernel32" (ByVal hInstance As Long, ByVal hResInfo As Long) As Long

'Accesses a unique storage system, such as a database or file
'archive. Install or remove this callback function with the
'mmioInstallIOProc function.
Public Function IOProc(ByRef lpMMIOInfo As MMIOINFO, ByVal uMessage As Long, ByVal lParam1 As Long, ByVal lParam2 As Long) As Long
   
  Static alreadyOpened As Boolean
   
  Select Case uMessage
    Case MMIOM_OPEN
      If Not alreadyOpened Then
        alreadyOpened = True
        lpMMIOInfo.lDiskOffset = 0
      End If
      IOProc = 0
    Case MMIOM_CLOSE
      IOProc = 0
    Case MMIOM_READ:
      Call CopyMemory(ByVal lParam1, ByVal lpData + lpMMIOInfo.lDiskOffset, lParam2)
      lpMMIOInfo.lDiskOffset = lpMMIOInfo.lDiskOffset + lParam2
      IOProc = lParam2
    Case MMIOM_SEEK
      Select Case lParam2
        Case SEEK_SET
          lpMMIOInfo.lDiskOffset = lParam1
        Case SEEK_CUR
          lpMMIOInfo.lDiskOffset = lpMMIOInfo.lDiskOffset + lParam1
          lpMMIOInfo.lDiskOffset = fileSize - 1 - lParam1
        Case SEEK_END
          lpMMIOInfo.lDiskOffset = fileSize - 1 - lParam1
      End Select
      IOProc = lpMMIOInfo.lDiskOffset
    ' Unexpected msgs.  For instance, we do not process MMIOM_WRITE in this sample
    Case Else
      IOProc = -1
  End Select
   
End Function
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
MirkwoodCommented:
Or try this

**********************************************************************
' * Comments         : Play wav stored in resource file
' *
' *
' **********************************************************************

Private Const SND_ALIAS = &H10000
Private Const SND_ASYNC = &H1
Private Const SND_FILENAME = &H20000
Private Const SND_LOOP = &H8
Private Const SND_NODEFAULT = &H2
Private Const SND_MEMORY = &H4      
Private Const SND_NOSTOP = &H10
Private Const SND_NOWAIT = &H2000
Private Const SND_SYNC = &H0

Private Declare Function sndPlaySound Lib "winmm.dll" Alias "sndPlaySoundA" (ByVal lpszSoundName As String, ByVal uFlags As Long) As Long

Public Sub PlayWaveRes(vntResourceID As Variant, Optional vntFlags)
   ' #VBIDEUtils#************************************************************
   ' * Programmer Name  : Waty Thierry
   ' * Web Site         : www.geocities.com/ResearchTriangle/6311/ 
   ' * E-Mail           : waty.thierry@usa.net
   ' * Date             : 3/12/98
   ' * Time             : 09:47
   ' * Module Name      : Multimedia_Module
   ' * Module Filename  : Multimedia.bas
   ' * Procedure Name   : PlayWaveRes
   ' * Parameters       :
   ' *                    vntResourceID As Variant
   ' *                    Optional vntFlags
   ' **********************************************************************
   ' * Comments         :
   '           If you want to play sound files asynchronously in
   '           Win32, then you MUST change bytSound() from a local
   '           variable to a module-level or static variable. Doing
   '           this prevents your array from being destroyed before
   '           sndPlaySound is complete. If you fail to do this, you
   '           will pass an invalid memory pointer, which will cause
   '           a GPF in the Multimedia Control Interface (MCI).
   ' *
   ' *
   ' **********************************************************************

   Dim bytSound() As Byte ' Always store binary data in byte arrays!
   
   bytSound = LoadResData(vntResourceID, "WAVE")
   
   If IsMissing(vntFlags) Then
      vntFlags = SND_NODEFAULT Or SND_SYNC Or SND_MEMORY
   End If
   
   If (vntFlags And SND_MEMORY) = 0 Then
      vntFlags = vntFlags Or SND_MEMORY
   End If
   
   sndPlaySound bytSound(0), vntFlags
   
End Sub
0
janneaAuthor Commented:
Thanks Mirkwood!

But... I guess in this case I need to compile a dll who contains the resource. And the main reason that I like to have a embeded (*mid,*wav) was that I just like to distr. the exe-file  and no other files.
The resourcecompiler I have is the one with VBAssist in VB5. And that one onely accept bmp, cur, ico and string. And this res-file is included in the exe-file when compiling my project.
Now I just have bmp-files in my res-file, and VB just accept one res-file in a project.
Do you know how to handle this in a proper way with the OLE-object instead?

/Janne
0
MirkwoodCommented:
   OLE1.CreateEmbed "D:\Chaos\Sound\badboys.wav"
    OLE1.Action = 7
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.