关于用 vb 写音乐播放器,调用的系统 API 函数 mcisendstring 不能播放有带空格的文件名的 MP3 的问题
最近用mcisendstring函数写了一个音乐播放器,但是不能够播放文件名中带有空格的音乐,求大神解
有3种方法解决
1:分析文件名,只要含有空格则拷贝该文件到本目录的临时文件,该临时文件名不含空格,mcisendstring此时播放临时文件
2:首先备份现文件名字串,分析文件名并去掉空格,对该文件改名,播放完成或更换播放音乐时又将该文件名该还原即可。
3:获取含空格的文件短文件路径,代码如下(已调试通过)
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
Private Declare Function GetShortPathName Lib "kernel32" Alias "GetShortPathNameA" (ByVal lpszLongPath As String, ByVal lpszShortPath As String, ByVal cchBuffer As Long) As Long
Private Sub Command1_Click()
'播放
mciSendString "play music", 0&, 0, 0
End Sub
Private Sub Command2_Click()
'停止
mciSendString "stop music", 0&, 0, 0
End Sub
Private Sub Command3_Click()
'关闭
mciSendString "close music", 0&, 0, 0
End Sub
Private Sub Form_Load()
'打开音乐文件
Dim s As String, d As String * 255, l As Long, i As Integer
s = "e:\音乐\红 星 歌.mp3" '原带空格的音乐文件名,根据需要换
l = GetShortPathName(s, d, 255) '找到该文件对应的短文件名(实际长度255的字串)
s = ""
i = 1
While Abs(Asc(Mid(d, i, 1))) > 10
'该循环可以得到正确的文件名
s = s & Mid(d, i, 1)
i = i + 1
Wend
mciSendString "open " & s & " Alias music", 0&, 0, 0 '打开短文件名对应的mp3
End Sub
Private Sub Form_Unload(Cancel As Integer)
'退出也关闭,防止没有手动关闭
mciSendString "close music", 0&, 0, 0
End Sub