|
楼主 |
发表于 2020-4-30 14:25
|
显示全部楼层
直接播放还是有难度的,我理解播放的过程了。
1、导出包文件。
2、播放这个文件里的音乐文件。
我觉得这个思路是对的。见http://club.excelhome.net/forum. ... 708&pid=2721115。
后来我把它改编成PowerPoint了。
模块代码:
- Public Declare Function sndPlaySound Lib "winmm.dll" Alias "sndPlaySoundA" (ByVal lpszSoundName As Any, ByVal uFlags As Long) As Long
- Public Const SND_NODEFAULT& = &H2
- Public Const SND_RESOURCE& = &H40004
- Public Const SND_ASYNC = &H1 ' play asynchronously
- Public Const SND_MEMORY = &H4 ' lpszSoundName points to a memory file
- Public Const SND_SYNC = &H0 ' play synchronously (default)
- Private Declare Function GlobalLock Lib "kernel32" (ByVal hMem As Long) As Long
- Private Declare Function GlobalUnlock Lib "kernel32" (ByVal hMem As Long) As Long
- Private Declare Function OpenClipboard Lib "user32" (ByVal hwnd As Long) As Long
- Private Declare Function CloseClipboard Lib "user32" () As Long
- Private Declare Function GetClipboardData Lib "user32" (ByVal wFormat As Long) As Long
- Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
- Private Declare Function GlobalSize Lib "kernel32" (ByVal hMem As Long) As Long
- Private Declare Function EmptyClipboard Lib "user32" () As Long
- Public Declare Function GetTempFileName Lib "kernel32" Alias "GetTempFileNameA" (ByVal lpszPath As String, ByVal lpPrefixString As String, ByVal wUnique As Long, ByVal lpTempFileName As String) As Long
- Public Declare Function GetTempPath Lib "kernel32" Alias "GetTempPathA" (ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long
- Public Declare Function mciExecute Lib "winmm.dll" (ByVal lpstrCommand As String) As Long
- Public 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
- Public Declare Function mciGetDeviceID Lib "winmm.dll" Alias "mciGetDeviceIDA" (ByVal lpstrName As String) As Long
- Public Declare Function mciGetCreatorTask Lib "winmm.dll" (ByVal wDeviceID As Long) As Long
- Public Const MAX_PATH = 255
- Public tmpFile As String
- Dim lRet As Long
- Dim ret As String * 1024
- Public bPlaying As Boolean
- Public Enum AudioFormatConstants
- wav = 0
- other = 1
- End Enum
- Public Function GetTempFile() As String
- Dim sTmpPath As String
- Dim sTmpFile As String
- sTmpPath = Space(MAX_PATH)
- GetTempPath MAX_PATH, sTmpPath
- sTmpPath = Left(sTmpPath, InStr(sTmpPath, Chr(0)) - 1)
- sTmpFile = Space(MAX_PATH)
- GetTempFileName sTmpPath, "bgm" & Chr(0), 0, sTmpFile
- GetTempFile = Left(sTmpFile, InStrRev(sTmpFile, ".") - 1) & ".mp3"
- End Function
- Sub playSound()
- lRet = mciSendString("play " & tmpFile & " repeat", ret, 1024, 0)
- Id = mciGetDeviceID(tmpFile)
- bPlaying = True
- End Sub
- Sub stopSound()
- On Error Resume Next
- mciSendString "close all", ret, 1024, 0
- bPlaying = False
- End Sub
- Sub test()
- tmpFile = GetTempFile
- On Error Resume Next
- itop = ActivePresentation.Slides(1).Shapes("Object 5").Top
- On Error GoTo 0
- If IsEmpty(itop) Then Exit Sub
- Export tmpFile, ActivePresentation.Slides(1).Shapes("Object 5"), other
- playSound
- End Sub
- Sub test2()
- On Error Resume Next
- stopSound
- Kill tmpFile
- End Sub
- Public Sub Export(targetFile As String, objOLE As Shape, AudioFormat As AudioFormatConstants)
- Dim hMem As Long
- Dim nClipsize As Long
- Dim lpData As Long
- Dim bytData() As Byte
- objOLE.Copy
- OpenClipboard 0&
- hMem = GetClipboardData(49156)
- If CBool(hMem) Then
- nClipsize = GlobalSize(hMem)
- lpData = GlobalLock(hMem)
- If lpData <> 0 Then
- ReDim bytData(0 To nClipsize) As Byte
- CopyMemory bytData(0), ByVal lpData, nClipsize
- End If
- GlobalUnlock hMem
- End If
- EmptyClipboard
- CloseClipboard
-
- If AudioFormat <> wav Then
- Dim iPos As Long
- Dim iCountZero As Integer
- Dim lOffset As Long
- Dim lFilesize As Long
- For iPos = 0 To nClipsize
- If bytData(iPos) = 0 Then
- iCountZero = iCountZero + 1
- If iCountZero = 3 Then Exit For
- End If
- Next
- iPos = iPos + 5
- CopyMemory lOffset, bytData(iPos), 4
- iPos = iPos + lOffset + 4
- CopyMemory lFilesize, bytData(iPos), 4
- iPos = iPos + 4
- CopyMemory bytData(0), bytData(iPos), lFilesize
- ReDim Preserve bytData(0 To lFilesize) As Byte
- End If
- Dim fileNumber As Integer
- fileNumber = FreeFile
- Open targetFile For Binary As #fileNumber
- Put #fileNumber, , bytData
- Close #fileNumber
-
- End Sub
复制代码
slide1代码(播放、停止两个按钮)
- Private Sub CommandButton1_Click()
- test
- End Sub
- Private Sub CommandButton2_Click()
- stopSound
- End Sub
复制代码
谢谢你的指导,稍后我会认真理解你的图意的。 |
|