【发布时间】:2012-03-14 15:43:03
【问题描述】:
好的,所以我使用 mciSendString 创建了一个 SOUND 类。我正在使用它来播放我正在制作的 RPG 风格游戏的音乐和其他声音文件。它有效,而且效果很好……但是。我的问题是它需要声音文件的完整路径才能正常工作(不确定这是否属实)。
以下是 mcisendstring 的一些怪癖:
- mcisendstring 讨厌空格,空格会导致崩溃(无法加载文件) - 添加引号
- 路径中的点会导致加载失败(仅点应用于扩展)
- 非常长的路径会导致加载失败(允许的最大字符数为 255)
- 相对路径会导致加载失败...(不能使用像 chicken.mp3 这样的东西...必须使用 C:\chicken.mp3)
- 如果repeat = true,.wav 文件将无法加载
我尝试将文件添加到我的资源并尝试设置资源的路径,但它显示:Value of type '1-dimensional array of Byte' cannot be converted to 'String'。
非常感谢任何帮助:)
这是我的代码:
Public Class SOUND
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
'Command String VERY IMPORTANT
Private oName As String = Nothing
Public Property Name As String
Set(value As String)
oName = value
End Set
Get
Return oName
End Get
End Property
Public Sub Play(ByVal id As Integer, ByVal repeat As Boolean, Optional volume As Integer = 1000)
If repeat = True Then
mciSendString("Open " & GetFile(id) & " alias " & oName, CStr(0), 0, 0) 'Open the file
mciSendString("play " & oName & " repeat ", CStr(0), 0, 0) 'then play with repeating value
Else
mciSendString("Open " & GetFile(id) & " alias " & oName, CStr(0), 0, 0)
mciSendString("play " & oName, CStr(0), 0, 0)
End If
'Optionally Set Volume
mciSendString("setaudio " & oName & " volume to " & volume, CStr(0), 0, 0)
End Sub
Public Sub Kill(ByVal song As String)
mciSendString("close " & song, CStr(0), 0, 0)
oName = Nothing
End Sub
' Media Library
Private Function GetFile(ByVal id As Integer) As String
Dim path As String = ""
'mcisendstring Hates spaces, spaces will cause crash (failure to load file) - ADD QUOTES
'Dots in path can cause failure to load (only dot should be for the extention)
'Very Long Paths will cause failure to load (Max Characters allowed is 255)
'Relative paths will cause failure to load...(cannot use something like chicken.mp3 ... must use ex:[C:\chicken.mp3])
'.wav files will fail to load if repeat = true
Select Case id
Case 0 'Game Over
path = "C:\FinalFantasy7-SendaDreamIntoTheUniverse.mp3"
Case 1 'Battle Sequence
path = "C:\FinalFantasyTactics-Garland_Magic.mp3"
Case 2 'Battle Victory
path = "C:\FinalFantasyLegend2-Victory.mp3"
Case 3 'Mission Time
path = "C:\FinalFantasyAdventure-Mission.mp3"
End Select
path = Chr(34) & path & Chr(34)
'Chr(34) is quotes...cannot just use "" because it will think that it is part of the string/path itself.
Return path
End Function
结束类
我需要什么:
- 我需要以某种方式将 .mp3 文件作为可用于路径的资源,或者我需要其他方法,而不是让这些文件位于根驱动器或其他完整的计算机路径中。
我需要的东西的例子
改变
Select Case id
Case 0 'Game Over
path = "C:\FinalFantasy7-SendaDreamIntoTheUniverse.mp3"
End Select
到
Select Case id
Case 0 'Game Over
path = My.Resources.FinalFantasyAdventure_Mission
End Select
我如何播放声音:
(在另一个测试表上)
Dim intSound As Integer = 0
Dim snd As New SOUND 'SOUND is the sound class with mcisendstring
Private Sub PlaySoundBtn_Click(sender As System.Object, e As System.EventArgs) Handles PlaySoundBtn.Click
intSound += 1
With snd
.Name = "SOUND" & intSound 'SOUND is the alias along with the number (this one is SOUND1)
.Play(3, True, 500)
'3 is the song ID, True is for the repeat, 500 is for the optional volume (Max = 1000)
End With
End Sub
附:我在我的项目中使用 XNA Graphics,而且我还有 NuGet 的 BizArk Core。因此,如果我可以使用其中任何一个,请告诉我如何使用其中任何一个来完成我需要做的事情。提前感谢您的帮助。
【问题讨论】:
-
这对于 mciSendString 是不可能的。但当然可以使用 XNA:msdn.microsoft.com/en-us/library/bb195053.aspx
-
好的,我查看了页面,但是,它对我没有任何帮助,哈哈。我将如何在 .net 中执行此操作(不是 C# 或 C++)
标签: .net audio audio-streaming embedded-resource mcisendstring