- VBAのアプリを終わらせてもサウンドは鳴り続けるので、終了処理において必ず停止させる必要がある。
- 再生できるフォーマットはmp3、wav、mid
- mp3については再生できない場合がある
最後のmp3についてですが、詳細はわかりませんが、OSの何かの設定の違いにより、mp3を読み込ませるとフリーズすることがあります。同一のプログラムを異なるPCで実行させた時に、片方は再生成功、片方は再生失敗(フリーズ)という状況が起こりました。そのPC自体がmp3を再生できないというわけではないので、何かの設定の問題でしょう。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
'停止 | |
Sub stopSound(soundFile As String) | |
'ファイルの有無を確認 | |
If Dir(soundFile) = "" Then | |
MsgBox soundFile & vbCrLf & "がありません。", vbExclamation | |
Exit Sub | |
End If | |
mciSendString "Stop " & soundFile, "", 0, 0 | |
End Sub | |
'再生 | |
Sub playSound(soundFile As String) | |
'ファイルの有無を確認 | |
If Dir(soundFile) = "" Then | |
MsgBox soundFile & vbCrLf & "がありません。", vbExclamation | |
Exit Sub | |
End If | |
rc = mciSendString("Play " & soundFile, "", 0, 0) | |
End Sub |