我正在尝试使用Public函数在应用程序上添加多个声音...当我使用绝对路径获取我的声音时,一切都运行正常,但是当我尝试从资源中加载它们时我没有得到任何声音输出..任何想法有什么不对?
Public Class Form1
Public 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
Dim musicAlias As String = "myAudio"
'Dim musicPath As String = "C:\Users\Teo\Documents\Visual Studio 2015\Projects\test\test\Resources\sound.mp3"
'Dim musicPath As String = "Resources\sound.mp3"
'Dim musicPath As String = My.Resources.ResourceManager.GetObject("sound.mp3")
Dim musicPath2 As String = "C:\Users\Teo\Desktop\sound.mp3"
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
mciSendString("Open " & musicPath & " alias " & musicAlias, CStr(0), 0, 0)
mciSendString("play " & musicAlias, CStr(0), 0, 0)
End Sub
End Class
最后一个完美无缺,我尝试了上述每一个......上面的三条评论是我试图让它发挥作用的不同方式,但都失败了......
答案 0 :(得分:1)
您可以从资源加载文件并在本地缓存它们并播放它们。
在Resources.resx
下打开My Project
文件。然后选择YourFileName.mp3
将您的文件Add Existing File...
添加到资源中,然后当您要播放该文件时,请使用以下代码:
Dim file As String = System.IO.Path.Combine(Application.StartupPath, "YourFileName.mp3")
If (Not System.IO.File.Exists(file)) Then
System.IO.File.WriteAllBytes(file, My.Resources.YourFileName)
End If
'Now the file exists locally
'Play the file here
答案 1 :(得分:1)
我知道这有一个标记正确的答案,但是一旦你的资源中有一个声音文件,这样就容易多了:
My.Computer.Audio.Play(My.Resources.MyCoolSound, AudioPlayMode.Background)
没理由使用API调用。