VB中的Multimedia MCI控件 怎么获取歌曲的时长呢?

2024-12-26 21:22:11
推荐回答(1个)
回答1:

其实可以采用windows player 来实现单曲播放,就只需要用一句话就可以
Shell "C:\Program Files\Windows Media Player\wmplayer.exe D:\娱乐文件\Mp3\fated.wmv", 0 '0为隐藏播放,参数1,2为普通播放窗口;大于3为全屏播放

下面是采用Multimedia MCI控件来进行播放,其中MMControl1.Length 可以获取歌曲的长度

Private Sub MnuOpen_Click()
′ 设定打开文件类型
CommonDialog1.Filter = 〃MIDI音乐序器*.mid|*.mid|声音*.wav|*.wav|Widows视频*.avi|*.avi〃
CommonDialog1.ShowOpen
MMControl1.Notify = False
MMControl1.Wait = True
MMControl1.Shareable = False
′由系统默认设备类型
MMControl1.DeviceType = 〃〃
MMControl1.filename = CommonDialog1.filename
′ 打开 MCI 设备。
MMControl1.Command = 〃Open〃
MnuClose.Enabled = True
Slider1.Visible = True
End Sub
Private Sub MMControl1_StatusUpdate()
On Error GoTo Erropen
′设置文件长度以毫秒形式返回,
MMControl1.TimeFormat = 0
Slider1.Max = MMControl1.Length / 1000
If MMControl1.Position <> MMControl1.Length Then
Slider1.Value = MMControl1.Position / 1000
Else
MMControl1.Command = 〃Prev〃
Slider1.Value = 0
End If
′调用Tstring函数计算剩余时间和总时间
StatusBar1.Panels(2) = Tstring(MMControl1.Length - MMControl1.Position)
StatusBar1.Panels(4) = Tstring(MMControl1.Length)
Label1.Caption = 〃00:00〃
Label2.Caption = Tstring(MMControl1.Length / 2)
Label3.Caption = Tstring(MMControl1.Length)
′ 由Mode返回不同值并在状态栏经出相应的提示
Select Case MMControl1.Mode
Case 524
StatusBar1.Panels(5) = 〃 未打开设备……〃
Case 525
StatusBar1.Panels(5) = 〃停止状态……〃
Case 526
StatusBar1.Panels(5) = 〃正在播放……〃 & CommonDialog1.FileTitle
Case 527
StatusBar1.Panels(5) = 〃正在录音……〃
Case 528
StatusBar1.Panels(5) = 〃正在搜索……〃
Case 529
StatusBar1.Panels(5) = 〃暂停播放……〃
Case 530
StatusBar1.Panels(5) = 〃设备就绪……〃
End Select
Erropen:
′ 放弃打开文件
End Sub
′ 自定义将毫秒转化为分、秒Tstring函数
Private Function Tstring(sm As Long) As String
s = (sm / 1000 / 60) Mod 60
m = (sm / 1000) Mod 60
Tstring = Format(s, 〃00〃) & 〃;〃 & Format(m, 〃00〃)
End Function
Private Sub MMControl1_Done(NotifyCode As Integer)
MMControl1.UpdateInterval = 100 ′ 间隔100毫秒
End Sub
Private Sub MnuClose_Click()
MMControl1.Command = 〃Close〃
MnuClose.Enabled = False
End Sub
Private Sub MnuExit_Click()
MMControl1.Command = 〃Close〃
End
End Sub
Private Sub Check1_Click()
If Check1.Value = 1 Then
MMControl1.Command = 〃Play〃 ′ 继续播放
End If
End Sub