点击vb菜单-工具-资源编辑器,用鼠标拉宽,倒数第二个按钮,把你的wav文件添加进去,类型名为"WAVE",标识号为"MySound",注意都要带上双引号,然后保存资源文件,例子代码如下:
Private Declare Function PlaySound Lib "winmmdll" Alias "PlaySoundA" _
(ByVal lpszName As String, ByVal hModule As Long, ByVal dwFlags As Long) As Long
Private Const SND_ASYNC& = &H1
Private Const SND_NODEFAULT& = &H2
Private Const SND_RESOURCE& = &H40004
Dim hInst As Long
Dim sSoundName As String
Dim lFlags As Long
Dim lRet As Long
Private Sub Command1_Click()
hInst = ApphInstance
sSoundName = "MySound"
lFlags = SND_RESOURCE + SND_ASYNC + SND_NODEFAULT
lRet = PlaySound(sSoundName, hInst, lFlags)
End Sub
我回答过N次这样的问题,难道你搜不到么
其实不难。呃,建议原创一个界面。
先用XML>
这个可以的,加入常用控件的最后一个,OLe控件。添加后,点自己要添加的音乐,然后用下面的代码就行,哦再加个命令控件。而且添加之后不能再移动删除那个音乐文件,动了位置就不行了,音乐不行了。拷到别人机子上也要一样的音乐位置。明白不知道这是不是你想要的
Private Sub Command1_Click()
OLE1Action = 7
End Sub
你可以用API函数来实现播放声音,举个例子:
假设你要播放D盘下的soundwav
在模块中输入以下代码
Public Declare Function sndPlaySound Lib "winmm" Alias _
"sndPlaySoundA" (ByVal lpszSoundName As String, _
ByVal uFlags As Long) As Long
' flag uitzetten
Public Const SND_SYNC = &H0
Public Const SND_ASYNC = &H1
Public Const SND_NODEFAULT = &H2
Public Const SND_MEMORY = &H4
Public Const SND_LOOP = &H8
Public Const SND_NOSTOP = &H10
Public Sub PlaySound(strSound As String)
sndPlaySound strSound, SND_ASYNC Or SND_NODEFAULT
End Sub
在窗体中输入以下代码
Private Sub Form_Load()
PlaySound "D:\soundwav"
End Sub
PS你还可以用MMControl控件来播放WAV格式的音频,不过反应时间比较慢,或者是WMP(Windows Media Player)控件也行,都很好用的。两三行代码就能搞定!
不用ActiveX控件也能播放声音文件
讲一下代码保存至module
Option Explicit
Public Declare Function sndPlaySound Lib "winmmdll" Alias _
"sndPlaySoundA" (ByVal lpszSoundName As String, _
ByVal uFlags As Long) As Long
Const SND_SYNC = &H0
Const SND_ASYNC = &H1
Const SND_NODEFAULT = &H2
Const SND_LOOP = &H8
Const SND_NOSTOP = &H10
接下来把下面的代码粘贴到你想播放声音文件的地方,比如放在命令按钮的Click事件中
Dim sFlags As Long
sFlags = SND_ASYNC Or SND_NODEFAULT
sndPlaySound "FileNameWav", sFlags
把上面的FileNameWav换成你想要播放的声音文件的完整路径及文件名就行了。
如果你仅仅是想播放一些Windows常用的声音,比如说退出Windows的声音,有一种更简单的写法
sndPlaySound "SystemExit",sFlags
将SystemExit换成下面这些参数,试试是什么声音?
SystemStart
SystemExit
SystemDefault
SystemQuestion
SystemAsterisk
SystemExclamation
SystemHand
下面是sFlags所用参数的说明:
参数 说明
SND_SYNC Plays the WAV file specified and returns only when the sound has stopped playing
SND_ASYNC Plays the WAV file an continues after the sound has started playing
SND_NODEFAULT Do not play the default sound if the if the WAV file is not found
SND_LOOP Plays the WAV file continuously until sndPlaySound is called again Remember to put SND_ASYNC in the sFlag variable as well Else your application will lock-up To stop playing the wave file set the "FileNameWav" to Null instead
SND_NOSTOP Return to the beginning of the WAV file if it's already playing
播放背景音乐
声明:
Declare Function MCISendString& Lib -MMSYSTEM- (ByVal LPSTRCOMMAND
$, ByVal LPSTRRETURNSTR As Any, ByVal WRETURNLEN%, ByVal HCALLBACK%)
开始播放:
R% = MCISendString&(-OPEN EXAMPLEMID TYPE SEQUENCER ALIAS NN-, 0&,
0, 0)
R% = MCISendString&(-PLAY NN FROM 0-, 0&, 0, 0)
R% = MCISendString&(-CLOSE ANIMATION-, 0&, 0, 0)
停止:
R% = MCISendString&(-OPEN EXAMPLEMID TYPE SEQUENCER ALIAS NN-, 0&,
0, 0)
R% = MCISendString&(-STOP NN-, 0&, 0, 0)
R% = MCISendString&(-CLOSE ANIMATION-, 0&, 0, 0)
其中EXAMPLEMID 为播放的文件,NN为自定义名称标志。
api实现背景音乐方法: 在窗体上加入一个按钮,一个时间空间,输入如下代码: Option Explicit Private Declare Function mciSendString Lib "winmmdll" Alias "mciSendStringA" (ByVal lpstrCommand As String, ByVal lpstrReturnString As String, ByVal uReturnLength As Long, ByVal hwndCallback As Long) As Long Private Sub Command1_Click()'这段代码是开始播放音乐,具体路径、歌名你可以选择你需要的,也可以放到初始事件里面 mciSendString "close F:\恢复\lwp\歌曲\mp3\歌曲\一万个理由wma", vbNullString, 0, 0 mciSendString "open F:\恢复\lwp\歌曲\mp3\歌曲\一万个理由wma", vbNullString, 0, 0 mciSendString "play F:\恢复\lwp\歌曲\mp3\歌曲\一万个理由wma", vbNullString, 0, 0 End Sub Private Sub Form_Unload(Cancel As Integer)'这段代码是卸载窗体的时候,关掉音乐 mciSendString "close F:\恢复\lwp\歌曲\mp3\歌曲\一万个理由wma", vbNullString, 0, 0 End Sub Private Sub Timer1_Timer()'这段代码是检测歌曲有没有播放完毕,如果播放完毕,则重复播放。如果你不需要重复播放,可不要这段代码 Dim S As String S = String(256, Chr(0)) mciSendString "status F:\恢复\lwp\歌曲\mp3\歌曲\一万个理由wma mode", S, Len(S), 0 If Left(S, 7) = "stopped" Or Left(S, 2) = "停止" Then mciSendString "seek F:\恢复\lwp\歌曲\mp3\歌曲\一万个理由wma to start", vbNullString, 0, 0 mciSendString "play F:\恢复\lwp\歌曲\mp3\歌曲\一万个理由wma", vbNullString, 0, 0 End If End Sub '以上代码只是大概,具体的你可以根据自己需要编写,但愿对你有帮助
以上就是关于vb中怎样添加背景音乐(运行之后可自动播放)全部的内容,包括:vb中怎样添加背景音乐(运行之后可自动播放)、如何在VB中添加音乐、在VB做的程序中加入音乐等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)