오디오를 MCISendString API를 이용해서 재생/일시정지/재시작/정지할 수 있습니다.
여기서 재생위치를 실시간으로 Timer API를 이용해서 표시하는 예시입니다.
MCISendString 명령어 목록:
실행 영상:
코드 보기:
더보기
Option Explicit
#If VBA7 Then
Public Declare PtrSafe Function SetTimer Lib "user32" (ByVal hwnd As LongPtr, ByVal nIDEvent As LongPtr, _
ByVal uElapse As Long, ByVal lpTimerFunc As LongPtr) As LongPtr
Public Declare PtrSafe Function KillTimer Lib "user32" (ByVal hwnd As LongPtr, ByVal nIDEvent As LongPtr) As Long
Public Declare PtrSafe Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" _
(ByVal lpstrCommand As String, ByVal lpstrReturnString As Any, _
ByVal uReturnLength As Long, ByVal hwndCallback As LongPtr) As Long
Public TimerID As LongPtr
#Else
Public Declare Function SetTimer Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent As Long, _
ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long
Public Declare Function KillTimer Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent As Long) As Long
Public Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" _
(ByVal lpstrCommand As String, ByVal lpstrReturnString As Any, _
ByVal uReturnLength As Long, ByVal hwndCallback As Long) As Long
Public TimerID As Long
#End If
Const FileName As String = "bensound.mp3"
Dim TimerPause As Boolean
Dim audioLength As Long
Const sldNum As Long = 1
Function getLength() As Long
Dim RetString As String * 256
mciSendString "Set SoundFile time format milliseconds", vbNullString, 0, 0&
mciSendString "Status SoundFile length", RetString, Len(RetString), 0&
getLength = (RetString) / 1000
End Function
Function getPosition() As Long
Dim RetString As String * 256
mciSendString "Status SoundFile position", RetString, Len(RetString), 0&
getPosition = (RetString) / 1000
End Function
Function getStatus() As Variant
Dim RetString As String * 256
mciSendString "Status SoundFile mode", RetString, Len(RetString), 0&
getStatus = (RetString) '\ 1000
ActivePresentation.Slides(sldNum).Shapes("Status").TextFrame.TextRange = getStatus
End Function
'Show the current position
Private Sub myTimer()
On Error Resume Next
If TimerPause Then Exit Sub
Dim sld As Slide
Dim posShp As Shape, lineShp As Shape
Dim p As Long
p = getPosition
Set sld = ActivePresentation.Slides(sldNum)
Set posShp = sld.Shapes("position")
Set lineShp = sld.Shapes("line")
Call showTime("time", p)
With posShp
.Left = lineShp.Left + lineShp.Width * (p / audioLength) - .Width / 2
End With
getStatus
End Sub
Function showTime(strShp As String, lngtime As Long)
ActivePresentation.Slides(sldNum).Shapes(strShp).TextFrame.TextRange = _
Format(lngtime \ 60, "00") & ":" & Format(lngtime Mod 60, "00")
End Function
Sub PlayAudio()
Call showTime("time", 0)
mciSendString "close all", vbNullString, 0, 0&
'enclose the FileName with Chr(34)s for a long filename with blank characters
mciSendString "Open " & Chr(34) & FileName & Chr(34) & " alias SoundFile", vbNullString, 0, 0&
mciSendString "Play SoundFile", vbNullString, 0, 0&
'mciSendString "record record", vbNullString, 0, 0&
audioLength = getLength
Call showTime("time_end", audioLength)
startTimer
End Sub
Sub PauseAudio()
If TimerPause Then
mciSendString "Resume SoundFile", vbNullString, 0, 0&
Else
mciSendString "Pause SoundFile", vbNullString, 0, 0&
End If
pauseTimer
getStatus
End Sub
Sub StopAudio()
mciSendString "Stop SoundFile", vbNullString, 0, 0&
stopTimer
Call showTime("time", 0)
Call showTime("time_end", 0)
With ActivePresentation.Slides(sldNum)
.Shapes("position").Left = .Shapes("line").Left - .Shapes("position").Width / 2
End With
getStatus
End Sub
Private Sub startTimer()
If TimerID = 0& Then
TimerID = SetTimer(0&, 0&, 500&, AddressOf myTimer) ' 1000 = 1초, 500 = 0.5초
End If
End Sub
Private Sub stopTimer()
On Error Resume Next
KillTimer 0&, TimerID
TimerID = 0&
TimerPause = False
End Sub
Function pauseTimer()
TimerPause = Not TimerPause
End Function
Sub onSlideShowTerminate(SSW As SlideShowWindow)
stopTimer
mciSendString "close all", vbNullString, 0, 0&
End Sub
파일 다운로드:
Status 추가 버전:
현재 폴더에 bensound.mp3파일이 있어야 합니다. 다른 파일로 바꿀 수 있습니다.
출처:bensound.com
'PPT+VBA' 카테고리의 다른 글
2007에서 애니메이션 복사 기능 구현 (0) | 2021.12.05 |
---|---|
시계눈금, 회전살 그리기 (0) | 2021.11.29 |
클릭 시 표 도형이 펼쳐지는 효과 일괄 추가하기 (0) | 2021.11.26 |
ppt에 여러 개의 오디오가 연속으로 재생되게 하는 팁 (0) | 2021.11.14 |
그림 삽입 및 표(테이블)에 그림 삽입 VBA 매크로 (3) | 2021.11.02 |
파워포인트파일 사용자 속성 관리 (0) | 2021.10.31 |
RGB값의 변화에 따른 LED 색상 변화 시뮬레이션 PPT (0) | 2021.10.15 |
모든 폰트목록 보기 및 클라우드 폰트 일괄 다운로드 (0) | 2021.10.09 |
최근댓글