重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
用API mciSendString可以同时播放多个声音,需要提供路径。
10年积累的网站设计制作、网站建设经验,可以快速应对客户对网站的新想法和需求。提供各种问题对应的解决方案。让选择我们的客户得到更好、更有力的网络服务。我虽然不认识你,你也不认识我。但先网站设计后付款的网站建设流程,更有平乡免费网站建设让你可以放心的选择与我们合作。
资源文件很麻烦,要用反射读取出来,再用IO写到磁盘路径。
你是做游戏把?软糖建议是不要使用资源文件,而使用本地文件。
用个BGM文件夹保存背景音乐,用SE文件夹保存音效(学习RPG Maker的做法)
API代码奉上,具体实现模块字数超过最大值,请下载附件
Imports System.Text
''' summary
''' 可播放MPEG,AVI,WAV,MP3,MID,WMA等格式的音频文件,*不支持OGG
''' /summary
Public Class 音频设备
'声明API函数: mciSendString
System.Runtime.InteropServices.DllImport("winmm.dll", EntryPoint:="mciSendString")
Public Shared Function mciSendString(strCommand As String,
strReturn As StringBuilder,
iReturnLength As Integer,
hwndCallback As Integer) As Integer
End Function
'声明API函数: mciGetErrorString
System.Runtime.InteropServices.DllImport("winmm.dll", EntryPoint:="mciSendString")
Public Shared Function mciGetErrorString(errCode As Integer,
errMsg As StringBuilder,
buflen As Integer) As Integer
调用方法
Public Class Form1
Dim 音频设备 As New 音频设备
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim 路径 = "F:\音乐文件名.mp3"
音频设备.打开("自定义名称1", 路径)
音频设备.播放("自定义名称1")
'音频设备.暂停("自定义名称1")
'音频设备.继续("自定义名称1")
'音频设备.关闭("自定义名称1")
End Sub
End Class
如满意,请采纳,还有不懂的请追问,谢谢。
1.用Win32 API PlaySound
PlaySound位于"winmm.dll"中,可以根据输入参数的不同,播放WAV类型的音乐。在VB.NET中调用跟VB6中差不多:
先声明:
Private Declare Auto Function PlaySound Lib "winmm.dll" (ByVal lpszSoundName As String, ByVal hModule As Integer, ByVal dwFlags As Integer) As Integer
Const SND_FILENAME As Integer = H20000
Const SND_ALIAS As Integer = H10000
Const SND_SYNC As Integer = H0
具体还有很多常量,不一一列举,下面是播放一个wav文件的例子
Dim mstrfileName As String = "c:\eagle2.wav"
PlaySound(mstrfileName, 0, SND_FILENAME)
注意上面的播放是非同步的,就是说它并不会播放完毕才结束。如果把上面的代码写在一个Button.Click的事件处理程序中,第二次点击的时候它会打断第一次的播放,重新开始。
当然我们也可以调用系统的声音:
PlaySound("SystemStart", 0, SND_ALIAS Or SND_SYNC)
' 以同步的方式调用系统启动时候的声音。
2. 调用媒体播放器控件
VB.NET中并没有提供媒体播放器的.NET组件,没办法我们还是调用以前Com组件Windows Media Player。当然除了声音外,还可以播放视频文件。
首先把媒体播放器控件加到工具栏中,调用还是简单的写一下吧:)
Private Sub playMediaFile(ByVal mediaFileName As String)
With MediaPlayer1
.Stop()
.FileName = "c:\mp3\爱不爱我.mp3"
.Play()
End With
End Sub
My.Computer.Audio.Play("SoundFile.wav")
SoundFile.wav是你要播放的声音文件的路径
A simple Sound Recorder~
1. Add Three buttons (button1, button2, button3) and a label (label1) to the form.
2. Set the Text of Button1 to Start, Button2 to Stop, and Button3 to Play.
3. Add this code:
Public Class Form1
Private Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" (ByVal lpstrCommand As String, ByVal lpstrReturnString As String, ByVal uReturnLength As Integer, ByVal hwndCallback As Integer) As Integer
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Button1.Enabled = False
Button2.Enabled = True
mciSendString("open new Type waveaudio Alias recsound", "", 0, 0)
mciSendString("record recsound", "", 0, 0)
Label1.Text = "Recording..."
Label1.Visible = True
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Button1.Enabled = True
Button2.Enabled = False
Button3.Enabled = True
mciSendString("save recsound c:\recsound.wav", "", 0, 0)
mciSendString("close recsound", "", 0, 0)
MsgBox("File Created: C:\recsound.wav")
Label1.Text = "Stopped..."
Label1.Visible = False
My.Computer.Audio.Stop()
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Label1.Text = "Playing..."
Label1.Visible = True
My.Computer.Audio.Play("c:\recsound.wav", AudioPlayMode.Background)
End Sub
End Class