VB中添加什么控件可播放音乐文件,添加的代码是什么?

我已写好一小软件,想加点音乐,但对控件不熟,请高手帮忙,
2024-12-01 22:43:58
推荐回答(3个)
回答1:

有四种方法:

第一:如果你仅仅是想播放WAV这样的背景音乐的话,可以使用API函数,不用添加控件,这样可以减少系统资源,方法如下:
在通用中声明:
Private Declare Function PlaySound Lib "winmm.dll" Alias "PlaySoundA" (ByVal lpszName As String, ByVal hModule As Long, ByVal dwFlags As Long) As Long

接着在form 的 load 事件中写如下代码:
Call PlaySound(App.Path + "音乐名字.wav", 0&, &H1)//必须在当前文件夹下

第二:添加Windows Media Player 控件
把Windows Media Player 控件visible 属性设置为false
url 属性设置为 音乐文件的绝对路径并且加上音乐文件名字
或者在load 事件中设置此属性

第三:添加 real player 控件
visible 属性设置为 false
注意,此时设置的是 source 属性为音乐文件而不是url属性

第四:microsoft multimedia control 6.0控件
设置相关属性

回答2:

声明:
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
代码:
Dim returnstring As String
FileName As String
returnstring = Space(127)
FileName = "C:\A1.avi"
erg = mciSendString("open " & Chr$(34) & FileName & Chr$(34) & " type avivideo alias video", returnstring, 127, 0)
erg = mciSendString("set video time format ms", returnstring, 127, 0)
erg = mciSendString("play video from 0", returnstring, 127, 0)
'记着关闭!
erg = mciSendString("close video", returnstring, 127, 0)

如果声音在资源文件内
假设这个声音文件的关键字为"HAHA",先定义一个API函数:
Declare Function sndPlaySound Lib "winmm.dll" Alias "sndPlaySoundA" (lpszSoundName As Any, Byval uFlags As Long) As Long
注意:必须把的lpszSoundName类型设成Any,不能按照win32api.txt文件中的定义
Const SND_ASYNC = &H1
Const SND_MEMORY = &H4
再输入如下代码:
Dim Toms() As Byte
Toms=LoadResData("HAHA","WAVE")
sndPlaySound Toms(0), SND_MEMORY Or SND_ASYNC
这样就OK了!

回答3:

Windows Media Player 控件

WindowsMediaPlayer1.Url = "D:\音乐.mp3"
WindowsMediaPlayer1.Controls.Play