返回列表 上一主題 發帖

[發問] WindowsMediaPlayer播放音檔問題

回復 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
複製代碼
1

評分人數

TOP

回復 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
複製代碼

TOP

回復 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,才能夠執行?
謝謝你!

TOP

本帖最後由 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
複製代碼

TOP

回復 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
複製代碼

TOP

        靜思自在 : 【時間成就一切】時間可以造就人格,可以成就事業,也可以儲積功德。
返回列表 上一主題