Board logo

標題: [發問] WindowsMediaPlayer播放音檔問題 [打印本頁]

作者: lshsien    時間: 2016-1-19 20:55     標題: WindowsMediaPlayer播放音檔問題

請問各位大大,小弟使用vba WindowsMediaPlayer撥放音檔,
分別加入14個音檔list後,卻無法一次播完,平均每4個音檔會停止撥放,
在煩請大大解惑謝謝!
程式碼如下
Public Sub macro_test2()

Dim Bk_A As String
Bk_A = Left(ThisWorkbook.Name, Len(ThisWorkbook.Name) - 5)
'-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Dim book_name As String
Dim book_path As String

book_name = Left(ThisWorkbook.Name, Len(ThisWorkbook.Name) - 5)
book_path = ThisWorkbook.Path

工作表001.WindowsMediaPlayer1.currentPlaylist.Clear

music_path01 = ThisWorkbook.Path & "\" & "常用廣播" & "\" & "國語_各位旅客您好" & ".wav"
music_path02 = ThisWorkbook.Path & "\" & "常用廣播" & "\" & "國語_6點" & ".wav"
music_path03 = ThisWorkbook.Path & "\" & "常用廣播" & "\" & "國語_分50" & ".wav"
music_path04 = ThisWorkbook.Path & "\" & "常用廣播" & "\" & "國語_分" & ".wav"
music_path05 = ThisWorkbook.Path & "\" & "常用廣播" & "\" & "國語_開往" & ".wav"
music_path06 = ThisWorkbook.Path & "\" & "常用廣播" & "\" & "國語_台北的" & ".wav"
music_path07 = ThisWorkbook.Path & "\" & "常用廣播" & "\" & "國語_車次5" & ".wav"
music_path08 = ThisWorkbook.Path & "\" & "常用廣播" & "\" & "國語_車次0" & ".wav"
music_path09 = ThisWorkbook.Path & "\" & "常用廣播" & "\" & "國語_車次0" & ".wav"
music_path10 = ThisWorkbook.Path & "\" & "常用廣播" & "\" & "國語_次" & ".wav"
music_path11 = ThisWorkbook.Path & "\" & "常用廣播" & "\" & "國語_高鐵" & ".wav"
music_path12 = ThisWorkbook.Path & "\" & "常用廣播" & "\" & "國語_北上" & ".wav"
music_path13 = ThisWorkbook.Path & "\" & "常用廣播" & "\" & "國語_的列車即將進站請前往" & ".wav"
music_path14 = ThisWorkbook.Path & "\" & "常用廣播" & "\" & "國語_第二月台" & ".wav"
music_path15 = ThisWorkbook.Path & "\" & "常用廣播" & "\" & "國語_搭乘並留意月台間隙謝謝" & ".wav"

Dim Xwmp As IWMPMedia

工作表001.WindowsMediaPlayer1.currentPlaylist.Clear

Set Xwmp = 工作表001.WindowsMediaPlayer1.newMedia(music_path05)
工作表001.WindowsMediaPlayer1.currentPlaylist.insertItem 0, Xwmp

工作表001.WindowsMediaPlayer1.Controls.Play

Set Xwmp = 工作表001.WindowsMediaPlayer1.newMedia(music_path06)
工作表001.WindowsMediaPlayer1.currentPlaylist.insertItem 1, Xwmp

工作表001.WindowsMediaPlayer1.Controls.Play

Set Xwmp = 工作表001.WindowsMediaPlayer1.newMedia(music_path07)
工作表001.WindowsMediaPlayer1.currentPlaylist.insertItem 2, Xwmp

工作表001.WindowsMediaPlayer1.Controls.Play

Set Xwmp = 工作表001.WindowsMediaPlayer1.newMedia(music_path08)
工作表001.WindowsMediaPlayer1.currentPlaylist.insertItem 3, Xwmp

工作表001.WindowsMediaPlayer1.Controls.Play

End Sub
作者: stillfish00    時間: 2016-1-20 09:57

回復 1# lshsien
你下面只插入第 5,6,7,8 首到撥放清單。
作者: Joforn    時間: 2016-1-20 12:53

回復 1# lshsien
添加一個新的模塊,將下面的代碼粘貼到此模塊中:
  1. Option Explicit

  2. #If VBA7 Then
  3.   Private Declare PtrSafe Function waveOutGetNumDevs Lib "winmm.dll" () As Long
  4.   Private Declare PtrSafe Function mciSendString Lib "winmm.dll" Alias "mciSendStringW" (ByVal lpstrCommand As LongPtr, ByVal lpstrRetumString As LongPtr, ByVal uReturnLength As Long, ByVal hwndCallback As LongPtr) As Long
  5.   Private Declare PtrSafe Function GetShortPathNameW Lib "kernel32" (ByVal lpszLongPath As LongPtr, ByVal lpszShortPath As LongPtr, ByVal cchBuffer As Long) As Long
  6.   Private Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
  7.   Private Declare PtrSafe Function PathFindExtension Lib "shlwapi.dll" Alias "PathFindExtensionW" (ByVal pszPath As LongPtr) As LongPtr
  8.   Private Declare PtrSafe Function lStrLen Lib "kernel32.dll" Alias "lstrlenW" (ByVal lpString As LongPtr) As Long
  9.   Private Declare PtrSafe Sub CopyMemoryByPtr Lib "kernel32.dll" Alias "RtlMoveMemory" (ByVal Destination As LongPtr, ByVal Source As LongPtr, ByVal Length As Long)
  10. #Else
  11.   Private Declare Function waveOutGetNumDevs Lib "winmm.dll" () As Long
  12.   Private Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringW" (ByVal lpstrCommand As Long, ByVal lpstrRetumString As Long, ByVal uReturnLength As Long, ByVal hwndCallback As Long) As Long
  13.   Private Declare Function GetShortPathNameW Lib "kernel32" (ByVal lpszLongPath As Long, ByVal lpszShortPath As Long, ByVal cchBuffer As Long) As Long
  14.   Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
  15.   Private Declare Function PathFindExtension Lib "shlwapi.dll" Alias "PathFindExtensionW" (ByVal pszPath As Long) As Long
  16.   Private Declare Function lStrLen Lib "kernel32.dll" Alias "lstrlenW" (ByVal lpString As Long) As Long
  17.   Private Declare Sub CopyMemoryByPtr Lib "kernel32.dll" Alias "RtlMoveMemory" (ByVal Destination As Long, ByVal Source As Long, ByVal Length As Long)
  18. #End If

  19. Private Type FileList
  20.   FileName  As String
  21.   ShortName As String
  22. End Type

  23. Private FileList() As FileList
  24. Private ListCount  As Long

  25. Public Sub Clean()
  26.   ListCount = 0
  27.   Erase FileList
  28. End Sub

  29. Public Property Get Item(ByVal Index As Long) As String
  30.   If Index > 0 And Index <= ListCount Then
  31.     Item = FileList(Index).FileName
  32.   End If
  33. End Property

  34. Public Property Let Item(ByVal Index As Long, ByVal FileName As String)
  35.   Dim I As Long
  36.   If Index = ListCount + 1 Then
  37.     ListCount = ListCount + 1
  38.     ReDim Preserve FileList(1 To ListCount)
  39.   End If
  40.   If (Index > 0) And (Index <= ListCount) Then
  41.     With FileList(Index)
  42.       .FileName = FileName
  43.       
  44.       I = Len(FileName)
  45.       If I > 0 Then
  46.         .ShortName = GetShortPathName(FileName)
  47.       Else
  48.         .ShortName = vbNullString
  49.       End If
  50.     End With
  51.   End If
  52. End Property

  53. Public Function ItemAdd(ByVal FileName As String) As Long
  54.   Dim I As Long
  55.   I = ListCount + 1
  56.   Item(I) = FileName
  57.   ItemAdd = I
  58. End Function

  59. Public Function ItemReMove(ByVal Index As Long) As Boolean
  60.   If Index > 0 And Index <= ListCount Then
  61.     With FileList(Index)
  62.       .FileName = vbNullString
  63.       .ShortName = vbNullString
  64.     End With
  65.   End If
  66. End Function

  67. Public Sub StartPlay()
  68.   Dim I         As Long
  69.   
  70.   If waveOutGetNumDevs > 0 Then
  71.     For I = 1 To ListCount Step 1
  72.       With FileList(I)
  73.         If Len(.ShortName) Then
  74.           Select Case UCase$(ExtractFileExtension(.ShortName))
  75.             Case ".WAV": mciSendString StrPtr("open " & .ShortName & " type waveaudio alias JofornMusic"), 0, 0, 0
  76.             Case ".MDI": mciSendString StrPtr("open " & .ShortName & " type sequencer alias JofornMusic"), 0, 0, 0
  77.             Case Else:   mciSendString StrPtr("open " & .ShortName & " alias JofornMusic"), 0, 0, 0
  78.           End Select
  79.           mciSendString StrPtr("play JofornMusic FROM 0"), 0&, 0, 0
  80.           Do While Not IsEnd
  81.             Sleep 50
  82.             DoEvents
  83.           Loop
  84.           mciSendString StrPtr("close JofornMusic"), 0&, 0, 0
  85.         End If
  86.       End With
  87.     Next I
  88.   End If
  89. End Sub

  90. Private Function IsEnd() As Boolean
  91.   Dim I         As Long
  92.   Dim strStatus As String
  93.   
  94.   strStatus = String$(256, vbNullChar)
  95.   mciSendString StrPtr("status JofornMusic mode"), StrPtr(strStatus), 256, 0
  96.   I = InStr(strStatus, vbNullChar)
  97.   If I > 1 Then
  98.     IsEnd = UCase$(Left$(strStatus, I - 1)) = "STOPPED"
  99.   Else
  100.     IsEnd = True
  101.   End If
  102. End Function

  103. '从路径提取文件后缀名
  104. Public Function ExtractFileExtension(ByVal strPath As String) As String
  105.   #If VBA7 Then
  106.     Dim ptrExt As LongPtr
  107.   #Else
  108.     Dim ptrExt As Long
  109.   #End If
  110.   Dim ExtLen As Long
  111.   
  112.   strPath = strPath & vbNullChar
  113.   ptrExt = PathFindExtension(StrPtr(strPath))
  114.   If ptrExt Then
  115.     ExtLen = lStrLen(ptrExt)
  116.     If ExtLen > 0 Then
  117.       ExtractFileExtension = String(ExtLen, vbNullChar)
  118.       CopyMemoryByPtr StrPtr(ExtractFileExtension), ptrExt, ExtLen * 2
  119.     End If
  120.   End If
  121. End Function

  122. Public Sub StopPlay()
  123.   mciSendString StrPtr("close JofornMusic"), 0&, 0, 0
  124. End Sub

  125. Private Function GetShortPathName(ByVal FileName As String) As String
  126.   Dim I       As Long
  127.   Dim strTemp As String
  128.   
  129.   strTemp = Space$(256)
  130.   I = GetShortPathNameW(StrPtr(FileName), StrPtr(strTemp), 256)
  131.   If I > 0 Then GetShortPathName = Left$(strTemp, I)
  132. End Function
複製代碼
將原有的代碼修改為:
  1. Public Sub macro_test2()

  2.     Dim Bk_A As String
  3.     Bk_A = Left(ThisWorkbook.Name, Len(ThisWorkbook.Name) - 5)
  4.     '-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  5.    
  6.     Dim book_name As String
  7.     Dim book_path As String
  8.    
  9.     book_name = Left(ThisWorkbook.Name, Len(ThisWorkbook.Name) - 5)
  10.     book_path = ThisWorkbook.Path
  11.    
  12.     Clear
  13.     ItemAdd ThisWorkbook.Path & "\" & "常用廣播" & "\" & "國語_各位旅客您好" & ".wav"
  14.     ItemAdd ThisWorkbook.Path & "\" & "常用廣播" & "\" & "國語_6點" & ".wav"
  15.     ItemAdd ThisWorkbook.Path & "\" & "常用廣播" & "\" & "國語_分50" & ".wav"
  16.     ItemAdd ThisWorkbook.Path & "\" & "常用廣播" & "\" & "國語_分" & ".wav"
  17.     ItemAdd ThisWorkbook.Path & "\" & "常用廣播" & "\" & "國語_開往" & ".wav"
  18.     ItemAdd ThisWorkbook.Path & "\" & "常用廣播" & "\" & "國語_台北的" & ".wav"
  19.     ItemAdd ThisWorkbook.Path & "\" & "常用廣播" & "\" & "國語_車次5" & ".wav"
  20.     ItemAdd ThisWorkbook.Path & "\" & "常用廣播" & "\" & "國語_車次0" & ".wav"
  21.     ItemAdd ThisWorkbook.Path & "\" & "常用廣播" & "\" & "國語_車次0" & ".wav"
  22.     ItemAdd ThisWorkbook.Path & "\" & "常用廣播" & "\" & "國語_次" & ".wav"
  23.     ItemAdd ThisWorkbook.Path & "\" & "常用廣播" & "\" & "國語_高鐵" & ".wav"
  24.     ItemAdd ThisWorkbook.Path & "\" & "常用廣播" & "\" & "國語_北上" & ".wav"
  25.     ItemAdd ThisWorkbook.Path & "\" & "常用廣播" & "\" & "國語_的列車即將進站請前往" & ".wav"
  26.     ItemAdd ThisWorkbook.Path & "\" & "常用廣播" & "\" & "國語_第二月台" & ".wav"
  27.     ItemAdd ThisWorkbook.Path & "\" & "常用廣播" & "\" & "國語_搭乘並留意月台間隙謝謝" & ".wav"
  28.     StartPlay
  29. End Sub
複製代碼
完成上面的兩步操作後運行macro_test2測試。
作者: Joforn    時間: 2016-1-20 13:00

注:樓上的代碼不需要MediaPlayer控件。
作者: c_c_lai    時間: 2016-1-20 19:02

回復 1# lshsien
正如 Joforn 大大所提示的,以下的
代碼不需要 MediaPlayer 的支援控件。
如果你較習慣使用 MediaPlayer 控件
那麼你便可以不用往下閱覽了。
  1. Option Explicit

  2. Private Const SND_APPLICATION = &H80         '  look for application specific association
  3. Private Const SND_ALIAS = &H10000            '  name is a WIN.INI [sounds] entry
  4. Private Const SND_ALIAS_ID = &H110000        '  name is a WIN.INI [sounds] entry identifier
  5. Private Const SND_ASYNC = &H1                '  play asynchronously
  6. Private Const SND_FILENAME = &H20000         '  name is a file name
  7. Private Const SND_LOOP = &H8                 '  loop the sound until next sndPlaySound
  8. Private Const SND_MEMORY = &H4               '  lpszSoundName points to a memory file
  9. Private Const SND_NODEFAULT = &H2            '  silence not default, if sound not found
  10. Private Const SND_NOSTOP = &H10              '  don't stop any currently playing sound
  11. Private Const SND_NOWAIT = &H2000            '  don't wait if the driver is busy
  12. Private Const SND_PURGE = &H40               '  purge non-static events for task
  13. Private Const SND_RESOURCE = &H40004         '  name is a resource name or atom
  14. Private Const SND_SYNC = &H0                 '  play synchronously (default)

  15. #If VBA7 Then
  16.     Private Declare PtrSafe Function PlaySoundA Lib "winmm.dll" (ByVal lpszName As String, ByVal hModule As Long, ByVal dwFlags As Long) As Long
  17.     '  以上係 .Wav 檔之參數宣告; 下列三行為 .MP3 檔案宣告之引用功能函式及變數宣告。
  18.     Private Declare PtrSafe 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
  19.     Private Declare PtrSafe Function GetShortPathName Lib "kernel32" Alias "GetShortPathNameA" (ByVal lpszLongPath As String, ByVal lpszShortPath As String, ByVal cchBuffer As Long) As Long
  20. #Else
  21.     Private Declare Function PlaySoundA Lib "winmm.dll" (ByVal lpszName As String, ByVal hModule As Long, ByVal dwFlags As Long) As Long
  22.     '  以上係 .Wav 檔之參數宣告; 下列三行為 .MP3 檔案宣告之引用功能函式及變數宣告。
  23.     Private 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
  24.     Private Declare Function GetShortPathName Lib "kernel32" Alias "GetShortPathNameA" (ByVal lpszLongPath As String, ByVal lpszShortPath As String, ByVal cchBuffer As Long) As Long
  25. #End If

  26. Dim Mp3File, LastMp3File As String, lst() As String, idx As Long

  27. Sub Ex()
  28.     Dim cnt As Long
  29.    
  30.     idx = 0
  31.     addList (ThisWorkbook.Path & "\" & "常用廣播" & "\" & "國語_各位旅客您好.wav")
  32.     addList (ThisWorkbook.Path & "\" & "常用廣播" & "\" & "國語_6點.wav")
  33.     addList (ThisWorkbook.Path & "\" & "常用廣播" & "\" & "國語_分50.wav")
  34.     addList (ThisWorkbook.Path & "\" & "常用廣播" & "\" & "國語_分.wav")
  35.     addList (ThisWorkbook.Path & "\" & "常用廣播" & "\" & "國語_開往.wav")
  36.     addList (ThisWorkbook.Path & "\" & "常用廣播" & "\" & "國語_台北的.wav")
  37.     addList (ThisWorkbook.Path & "\" & "常用廣播" & "\" & "國語_車次5.wav")
  38.     addList (ThisWorkbook.Path & "\" & "常用廣播" & "\" & "國語_車次0.wav")
  39.     addList (ThisWorkbook.Path & "\" & "常用廣播" & "\" & "國語_車次0.wav")
  40.     addList (ThisWorkbook.Path & "\" & "常用廣播" & "\" & "國語_次.wav")
  41.     addList (ThisWorkbook.Path & "\" & "常用廣播" & "\" & "國語_高鐵.wav")
  42.     addList (ThisWorkbook.Path & "\" & "常用廣播" & "\" & "國語_北上.wav")
  43.     addList (ThisWorkbook.Path & "\" & "常用廣播" & "\" & "國語_的列車即將進站請前往.wav")
  44.     addList (ThisWorkbook.Path & "\" & "常用廣播" & "\" & "國語_第二月台.wav")
  45.     addList (ThisWorkbook.Path & "\" & "常用廣播" & "\" & "國語_搭乘並留意月台間隙謝謝.wav")
  46.         
  47.     For cnt = 1 To UBound(lst)
  48.         If UCase(Right(lst(cnt), 4)) = ".WAV" Then
  49.             PlaySound (lst(cnt))
  50.         ElseIf UCase(Right(lst(cnt), 4)) = ".MP3" Then
  51.             MMStop (LastMp3File)
  52.             LastMp3File = lst(cnt)
  53.             MMPlay (lst(cnt))
  54.         End If
  55.     Next cnt
  56.         
  57.     rmvList
  58. End Sub

  59. Public Function addList(ByVal fn As String)
  60.     Dim num As Long
  61.    
  62.     If idx = 0 Then num = 1 Else num = UBound(lst) + 1
  63.     ReDim Preserve lst(num)
  64.     lst(num) = fn
  65.     idx = idx + 1
  66. End Function

  67. Public Function rmvList()
  68.     If idx = 0 Then Exit Function
  69.     ReDim Preserve lst(0)   '  lst :  String(0 to 0)
  70.     idx = 0                 '  UBound(lst) = 0 : Long
  71. End Function

  72. ' Purpose     :  Plays an audio file
  73. ' Inputs      :  sFilePath           The location of the wav file
  74. '                lFlags              Can be one or many of the constants given above
  75. ' Outputs     :  The text found on the web site
  76. ' Author      :  Andrew Baker
  77. ' Date        :  21/10/2000 12:37
  78. ' Notes       :  Code adapted from code found on www.allapi.net (excellent site!)
  79. ' Revisions   :

  80. Function PlaySound(sFilePath As String, Optional lFlags As Long = SND_FILENAME Or SND_ASYNC) As Long
  81.     PlaySound = PlaySoundA(sFilePath, 0&, lFlags)
  82. End Function

  83. Function ConvShortFilename(ByVal strLongPath$) As String
  84.     Dim strShortPath$
  85.     If InStr(1, strLongPath, " ") Then
  86.         strShortPath = String(LenB(strLongPath), Chr(0))
  87.         GetShortPathName strLongPath, strShortPath, Len(strShortPath)
  88.         ConvShortFilename = Left(strShortPath, InStr(1, strShortPath, Chr(0)) - 1)
  89.     Else
  90.         ConvShortFilename = strLongPath
  91.     End If
  92. End Function

  93. Private Sub MMPlay(ByRef FileName As String)
  94.     FileName = ConvShortFilename(FileName)
  95.     mciSendString "close " & FileName, vbNullString, 0, 0
  96.     mciSendString "open " & FileName, vbNullString, 0, 0
  97.     mciSendString "play " & FileName, vbNullString, 0, 0
  98. End Sub

  99. Private Sub MMStop(ByRef FileName As String)
  100.     FileName = ConvShortFilename(FileName)
  101.     mciSendString "stop " & FileName, vbNullString, 0, 0
  102.     mciSendString "close " & FileName, vbNullString, 0, 0
  103. End Sub
複製代碼

作者: lshsien    時間: 2016-1-22 11:44

To stillfish00大大
不好意思,小弟沒貼到後面的。
To Joforn、c_c_lai兩位大大
感謝您的指導,目前正在努力測試中,
因小弟學習剛起步,對於兩位大大所寫的還是一知半解,
目前努力的理解中,
想請問大大,若使用MediaPlayer 控件,
是否有辦法解決小弟的問題,
謝謝!
作者: 准提部林    時間: 2016-1-22 13:12

以〔短語音〕mp3實測,
WindowsMediaPlayer並無四檔即停播問題,

或許上傳檔案較可能找出原因∼∼
作者: c_c_lai    時間: 2016-1-22 18:48

回復 6# lshsien
雖然你偏好使用 Windows Media Player,
但我使用 Joforn 大大提供的程式碼測試
三種型態的媒體檔 (*.mp3;*.wav;*.mp4)
它在執行撥放過程非常穩定,DoEvents 的
時段拿捏得恰當,尤其是 mp4 影片撥放
視窗也很流暢,蠻值得你採用。其實
Windows Media Player元件亦使用同樣之
Windows System 程式庫 (Lib)。
  1. Sub Ex()
  2.     ItemAdd (ThisWorkbook.Path & "\" & "大悲咒天使波羅蜜.wav")
  3.     ItemAdd (ThisWorkbook.Path & "\" & "舒伯特 小夜曲 大提琴演奏 .mp4")
  4.     ItemAdd (ThisWorkbook.Path & "\" & "寶篋咒.mp3")
  5.     ItemAdd (ThisWorkbook.Path & "\" & "聖誕快樂 01.mp4")
  6.     ItemAdd (ThisWorkbook.Path & "\" & "01_巴哈 G弦之歌 Air On the G string.mp3")
  7.     ItemAdd (ThisWorkbook.Path & "\" & "舒伯特 小夜曲 大提琴演奏 .mp4")
  8.     ItemAdd (ThisWorkbook.Path & "\" & "02_NOCTURNE.mp3")
  9.     ItemAdd (ThisWorkbook.Path & "\" & "03_SAULT D' AMOUR.mp3")
  10.     ItemAdd (ThisWorkbook.Path & "\" & "04_JUNE(BARCAROLLE).mp3")
  11.    
  12.     StartPlay
  13. End Sub
複製代碼

作者: ML089    時間: 2016-1-22 20:04

回復 3# Joforn

請問播放以後如何臨時暫停或中斷撥放
作者: Joforn    時間: 2016-1-23 12:32

回復  Joforn

請問播放以後如何臨時暫停或中斷撥放
ML089 發表於 2016-1-22 20:04
  1. Public Property Get Pause() As Boolean
  2.   Dim I         As Long
  3.   Dim strStatus As String
  4.   
  5.   strStatus = String$(256, vbNullChar)
  6.   mciSendString StrPtr("status JofornMusic mode"), StrPtr(strStatus), 256, 0
  7.   I = InStr(strStatus, vbNullChar)
  8.   If I > 1 Then
  9.     Debug.Print Left$(strStatus, I - 1)
  10.     Pause = UCase$(Left$(strStatus, I - 1)) = "PAUSED"
  11.   End If
  12. End Property

  13. Public Property Let Pause(ByVal Value As Boolean)
  14.   If Value Xor Pause Then
  15.     If Value Then
  16.       mciSendString StrPtr("pause JofornMusic"), 0, 0, 0
  17.     Else
  18.       mciSendString StrPtr("resume JofornMusic"), 0, 0, 0
  19.     End If
  20.   End If
  21. End Property
複製代碼
調用Pause=True就可以暫停,Pause=False繼續播放。
作者: Joforn    時間: 2016-1-23 12:52

mciSendString函數是用來播放多媒體文件的API指令,可以播放MPEG,AVI,WAV,MP3,等等,使用此API函數可以創建一個簡易的播放器,下面介紹它的使用方法:
  該函數有四個參數:
第一個參數:要發送的命令字符串。字符串結構是:[命令][設備別名][命令參數].
第二個參數:返回信息的緩衝區,為一指定了大小的字符串變量.
第三個參數:緩衝區的大小,就是字符變量的長度.
第四個參數:回調方式,一般設為零
返回值:函數執行成功返回零,否則返回錯誤代碼

一、常用命令
1.打開(Open),格式:Open 設備名[type 設備型式][alias 別名]
Dim mName as string
mName = "f:\mpeg\mpeg1.avi"
mciSendString "open " & mName & " type MPEGVideo Alias​​ JofornMusic parent %u Style %u notify",0&, 0, 0
 其中:
open 操作命令
mName 全路徑文件名
type MPEGVideo 是指打開MPEG,AVI等類型,如果不加這一句,就是打開WAV,MP3等
Alias​​ JofornMusic 定義了該操作的別名為movie,後續操作只要指明別名即可
parent %u 源
Style %u 樣式
notify 通知
2.播放(Play),格式:Play 設備名[from 起點][to 終點]
mciSendString "play JofornMusic", 0&, 0, 0
mciSendString "play JofornMusic fullscreen", 0&, 0, 0 '全屏播放
  3.暫停(Pause):
mciSendString "pause JofornMusic", 0&, 0, 0
  4.繼續(Resume):
mciSendString "resume JofornMusic", 0&, 0, 0
  5.停止(Stop):
mciSendString "stop JofornMusic", 0&, 0, 0
  6.​​關閉(Colse):
mciSendString "close JofornMusic", 0&, 0, 0
  7.前進到下一個位置:
mciSendString "step JofornMusic", 0&, 0, 0
  8.後退到上一個位置:
mciSendString "step JofornMusic reverse", 0&, 0, 0
9.前進或後退N 個位置(其中N<0 即表示後退)
mciSendString "step JofornMusic by " & str(N), 0&, 0, 0
  10.獲取當前播放位置:
Dim ST As String*64
mciSendString "status JofornMusic position", st, len(st), 0
  11. ​​獲取媒體的總長度:
mciSendString "status JofornMusic length", st, len(st), 0
l=val(st) 'l就是所播放文件的長度
  12.獲取播放當前狀態:
Dim ST As String*64
mciSendString "status JofornMusic mode", ST, Len(ST), 0
If Left(ST, 7) = "stopped" Then (處理代碼) '播放完畢
  13.循環播放:
mciSendString "play JofornMusic repeat", 0&, 0, 0

二、控制聲音大小(1-1000):
Dim V As Long
mciSendString "status JofornMusic volume",&V, len(v), 0 'V是獲取的音量大小值。
V = 50
mciSendString "setaudio JofornMusic volume to 數值", 0, 0, 0 'V是設置的音量值

三、設置播放位置.(需事先設定時間格式),格式:Seek 設備名[to 位置| to start | to end]
Dim P1 as Long, P2 as Long
P1 = 100: P2 = 3000
mciSendString "seek JofornMusic to ", P1, 0, 0 'P1是當前起始位置,單位:毫秒
mciSendString "seek JofornMusic to start", 0&, 0, 0 '定位到開頭位置
mciSendString "play JofornMusic", 0&, 0, 0 '定位後再播放
或者:
mciSendString "play JofornMusic FROM P1 to P2",0&, 0, 0 'P1是起始位置,P2是停止位置。單位:毫秒
mciSendString "seek JofornMusic to end", 0&, 0, 0 '定位到最後位置

四、在指定控件上播放視頻:
mciSendString "open AVI 文件名parent hWnd style child", 0&, 0, 0
其中,hWnd 是控件的句柄
執行上述命令之後,影片會被放置在控件的左上角,且影片的大小不受控件大小的影響,如果想要改變
影片播放的位置及大小,可以在執行play 指令前先執行put 指令,格式如下:
mcisendString "put AVI 文件名window at XY [Width Height]", 0&, 0, 0
其中:X、Y為影片左上角坐標,Width、Height為影片的寬高度

五、如果播放視頻還可控制亮度(1-2000)
Dim B As Long
mciSendString "status JofornMusic brightness", B, 0, 0 'B是獲取的亮度值。
B = 50
mciSendString "setvideo JofornMusic brightness to " & B, &0, 0, 0 'B是設置的亮度值

六、錄音設置:
  錄音前,用以下語句初始化
  1.設為8位:
mciSendString "set wave bitpersample 8", "", 0, 0
  2.設為11025Hz
mciSendString "set wave samplespersec 11025", "", 0, 0
  3.設為立體聲:
mciSendString "set wave channels 2", "", 0, 0
  4.實現PCM格式(不一定正確):
MCISENDSTRING "set wave format tag pcm","", 0, 0
  5.開始錄音:
mciSendString "close JofornMusic",0&,0,0
mciSendString "open new type WAVEAudio alias JofornMusic",0&,0,0
mciSendString "record JofornMusic",0&,0,0
  6.​​保存錄音到c:\123.wav
mciSendString "stop JofornMusic",0&,0,0
mciSendString "save JofornMusic C:\123.wav",0&,0,0
mciSendString "close JofornMusic",0&,0,0

七、開關光驅:
mciSendString "set cdaudio door open", "", 0, 0 '打開
mciSendString "set cdaudio door close", "", 0, 0 '關閉

八、其它
  1.設置設備的各種狀態(Set)
Set alias_name[audio all off][audio all on][time format ms]:
Set命令用來設置設備的各種狀態.如:靜音,有聲音,時間格式為毫秒等.
  2.取得設備的狀態(Status)
Status alias_name[length][mode][position]:
Status命令用來取得設備的狀態.如:該媒體文件的長度,該媒體文件所處狀態,該媒體文件的當前位置等. 的長度,該媒體文件所處狀態,該
媒體文件的當前位置等.
參考代碼:
TCHAR fileName[]="D:\俺的文檔\my music\爺爺泡的茶.mp3";
TCHAR shortName[MAX_PATH];
GetShortPathName(fileName,shortName,sizeof(shortName)/sizeof(TCHAR));
TCHAR cmd[MAX_PATH+10];
wsprintf(cmd,"play %s",shortName);
mciSendString(cmd,"",NULL,NULL);
(調用mciSendString第一個參數傳“play 文件全路徑”就可以,“文件全路徑”最好傳絕對路徑,不建議wanghepeng10那樣值傳遞文件名。
另外如果文件全路徑中含有空格的話要使用GetShortPathName轉換成短路徑。 )
作者: c_c_lai    時間: 2016-1-23 14:33

回復 10# Joforn
回復 9# ML089
Pause  以及 Resume 的處理,我寫了一
函式,如下:
  1. Public Sub PauseResume()
  2.     Static Msg As Boolean    '  用以判定是否為每日第一次執行
  3.    
  4.     If Msg = False Then
  5.         mciSendString StrPtr("pause JofornMusic"), 0&, 0, 0
  6.         Msg = True
  7.     Else
  8.         mciSendString StrPtr("resume JofornMusic"), 0&, 0, 0
  9.         Msg = False
  10.     End If
  11. End Sub
複製代碼
另外,再請教 Joforn 大大,如果想要執行 *.rmvb 的媒體檔,
還要加掛那些 Lib,才能夠執行?
謝謝你!
作者: ML089    時間: 2016-1-23 15:22

回復 10# Joforn
回復 12# c_c_lai

想不道 EXCEL 除計算外,還能撥放音樂,各路英雄的踴躍發表讓EXCEL越來越有趣。
作者: jackyq    時間: 2016-1-23 15:44

回復 13# ML089


只要能調用 std dll 的語言
幾乎都能無所不能
所以語言不是關鍵
底層技術才是關鍵
作者: Joforn    時間: 2016-1-24 01:37

回復  Joforn
回復  c_c_lai

想不道 EXCEL 除計算外,還能撥放音樂,各路英雄的踴躍發表讓EXCEL越來越 ...
ML089 發表於 2016-1-23 15:22

要先安裝相關的解碼器,不過本人沒有具體測試過,你可以自己測試下,但要注意的是調用時指定好用來顯示畫面的窗體句柄。
作者: c_c_lai    時間: 2016-1-24 20:38

本帖最後由 c_c_lai 於 2016-1-24 20:42 編輯

回復 1# lshsien
我特選了九首音樂 (樂曲長短不一,含MP4檔),反覆測試
均能正常撥放。你試試看吧!
  1. Option Explicit

  2. Dim Xwmp As IWMPMedia                       '  Playlist
  3. Dim MyPlayer As WindowsMediaPlayer
  4. Dim ListIdx As Long

  5. Public Function addList(ByVal fileName As String)
  6.     Set Xwmp = MyPlayer.newMedia(fileName)
  7.     MyPlayer.currentPlaylist.insertItem ListIdx, Xwmp
  8.     ListIdx = ListIdx + 1
  9.     DoEvents
  10.     MyPlayer.Controls.Play
  11. End Function

  12. Public Function rmvList()
  13.     DoEvents
  14.     If MyPlayer.playState = 1 Or MyPlayer.playState = 10 Then      '  "Stopped"  or  "Ready"
  15.         ListIdx = 0
  16.         MyPlayer.currentPlaylist.Clear
  17.     End If
  18. End Function

  19. Sub Ex()
  20.     Set MyPlayer = 工作表1.WindowsMediaPlayer1
  21.     rmvList
  22.    
  23.     addList ThisWorkbook.Path & "\\" & "大悲咒天使波羅蜜.wav"
  24.     addList ThisWorkbook.Path & "\\" & "寶篋咒.mp3"
  25.     addList ThisWorkbook.Path & "\\" & "舒伯特 小夜曲 大提琴演奏 .mp4"
  26.     addList ThisWorkbook.Path & "\\" & "聖誕快樂 01.mp4"
  27.     addList ThisWorkbook.Path & "\\" & "01_巴哈 G弦之歌 Air On the G string.mp3"
  28.     addList ThisWorkbook.Path & "\\" & "鐵達尼號 - My Heart Will Go On (我心永恆) 中英文字幕.mp4"
  29.     addList ThisWorkbook.Path & "\\" & "02_NOCTURNE.mp3"
  30.     addList ThisWorkbook.Path & "\\" & "03_SAULT D' AMOUR.mp3"
  31.     addList ThisWorkbook.Path & "\\" & "- Barcarolle in F sharp, Op.60.mp3"
  32. End Sub
複製代碼
[attach]23168[/attach]
作者: c_c_lai    時間: 2016-1-25 12:57

回復 1# lshsien
基本上,Windows Media Player 是無法撥放  *.rmvb 檔,
如果你想透過 Windows Media Player 的撥放器來撥放
*.rmvb 的影片檔,其實務應用範例如下,提供大家參考:
  1. Option Explicit

  2. Dim MyPlayer As WindowsMediaPlayer

  3. Public Function playRMVB(ByVal fileName As String)
  4.     MyPlayer.URL = fileName        '  需以 .URL 方式宣告
  5.     DoEvents
  6.     MyPlayer.Controls.Play
  7. End Function

  8. Public Function InitialList()
  9.     MyPlayer.Controls.stop
  10.    
  11.     DoEvents
  12.     If MyPlayer.playState = 0 Or MyPlayer.playState = 1 Or MyPlayer.playState = 10 Then
  13.         '  0 = "Undefined"  /  1 = "Stopped"  /  10 = "Ready"
  14.         ListIdx = 0
  15.         MyPlayer.currentPlaylist.Clear
  16.     End If
  17. End Function

  18. Sub Ex()
  19.     Set MyPlayer = 工作表1.WindowsMediaPlayer1
  20.     InitialList
  21.    
  22.     '  ******************************************************************
  23.     '  撥放 RMVB 影片檔需先安裝 Real_Alternative 的解析碼後, Windows
  24.     '  Media Player 才能進行解析 RMVB 影片檔,進而始能直接撥放它。
  25.     '  http://www.free-codecs.com/real_alternative_download.htm
  26.     '  以下三行係 .rmvb 影片之撥放程序  (*** 但只能放置在第一首撥放 ***)
  27.     '  1. MyPlayer.URL = ThisWorkbook.Path & "\\" & "xxxxxxxx.rmvb"
  28.     '  2. DoEvents
  29.     '  3. MyPlayer.Controls.Play
  30.     '  ******************************************************************
  31.     playRMVB ThisWorkbook.Path & "\\" & "xxxxxxxx.rmvb"
  32. End Sub
複製代碼

作者: lshsien    時間: 2016-1-26 09:24

本帖最後由 lshsien 於 2016-1-26 09:25 編輯

To Joforn 大大
經測試(NB、桌機)後,您的程式碼皆可順利執行,
並解說了相關code,非常感謝您。

To  c_c_lai 大大
您在5樓供的程式碼,經小弟測試(NB上),
會直接撥最後一首。目前還在努力測試中。
您在16樓供的程式碼,經小弟測試(NB上),
會跟小弟下面所述問題有重複撥放的狀況。

另告訴兩外大大一個有趣的測試問題,
先說明測試環境
1、小弟的NB:
ACER 5830T
WIN10
Office 2010
(系統及OFICE皆更新至最新)
2、公司電腦
ASUS桌機(型號未知)
WIN7
OFFICE2010
(系統及OFFICE可能未更新至最新)

如用小弟的原始程式碼進行測試 ,
在NB上是可以持續播完、但唯獨有幾個音檔會重複1次(目前測試數次發現music_path08、music_path09、music_path15),
經檢查code後未發現任何錯誤。

在公司電腦上依舊是平均4首就會停止撥放,但音檔list是有進WindowsMediaPlayer,
需再次按下WindowsMediaPlayer撥放鍵,始可繼續撥。
為何同樣的code在不同電腦有著不一樣結果,令人匪夷所思阿!

再次感謝兩位費心指導!
作者: lshsien    時間: 2016-1-26 10:10

To Joforn 大大
小弟想要控制音量,
在Public Sub StartPlay()中插入了音量控制code,
但無法控制音量大小,再請您解惑。

Public Sub StartPlay()

Dim V As Long
V = 1
mciSendString StrPtr("setaudio movie volume to " & V), &O0, 0, 0 'V是設置的音量值

  Dim I         As Long
  
  If waveOutGetNumDevs > 0 Then
    For I = 1 To ListCount Step 1
      With FileList(I)
        If Len(.ShortName) Then
          Select Case UCase$(ExtractFileExtension(.ShortName))
            Case ".WAV": mciSendString StrPtr("open " & .ShortName & " type waveaudio alias JofornMusic"), 0, 0, 0
            Case ".MDI": mciSendString StrPtr("open " & .ShortName & " type sequencer alias JofornMusic"), 0, 0, 0
            Case Else:   mciSendString StrPtr("open " & .ShortName & " alias JofornMusic"), 0, 0, 0
          End Select
          mciSendString StrPtr("play JofornMusic FROM 0"), 0&, 0, 0
          Do While Not IsEnd
            Sleep 50
            DoEvents
          Loop
          mciSendString StrPtr("close JofornMusic"), 0&, 0, 0
        End If
      End With
    Next I
  End If
End Sub




歡迎光臨 麻辣家族討論版版 (http://forum.twbts.com/)