2012-03-14 60 views
0

好吧,我創建了一個使用mciSendString的SOUND類。我正在使用它來播放我正在製作的RPG風格遊戲的音樂和其他聲音文件。它的工作原理,它運作良好......但。我的問題是,它需要聲音文件的完整路徑才能工作(不確定這是否爲真)。.net mcisendstring - 使用資源而不是完整路徑

下面是一些mcisendstring的querks的:

  • mcisendstring恨空格,空格會造成死機(未能加載文件) - 添加引號路徑
  • 點可以導致失敗加載(只點應該是爲了延伸)
  • 非常長的路徑將導致加載失敗(允許的最大字符數爲255)
  • 相對路徑將導致加載失敗...(不能使用類似chicken.mp3的東西...必須使用C:\ chicken.mp3)
  • .wav文件將無法加載如果重複= TRUE

我嘗試添加文件到我的資源,並試圖路徑設置到資源,但它說: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 

P.S.我在我的項目中使用XNA Graphics,我也有NuGet的BizArk Core。因此,如果我可以使用其中任何一種,請告訴我如何使用這兩種方法來做我需要做的事情。預先感謝您的幫助。

+0

這是不可能與mciSendString。但當然可以使用XNA:http://msdn.microsoft.com/en-us/library/bb195053.aspx – 2012-03-14 16:46:45

+0

好吧,我看了看頁面,但是,它並沒有幫助我什麼lol。我將如何去做這個.net(不是C#或C++) – 2012-03-14 17:18:00

回答

0

我知道這是一個超級老的線程,但如果它幫助任何人,這是一個解決方案。

你將不得不使用的完整路徑,但假設你的資源都存儲在/projectfolder/resources/可以使用

Dim filePath As String = Chr(34) & Application.StartupPath & "/../../Resources/myMusic.mp3" & Chr(34) 
mciSendString("open " & filePath & " alias myMusic", CStr(0), 0, 0) 

在VB。NET項目,Application.StartupPath是存儲在.exe中的文件夾的完整路徑。通常位於path/projectfolder/bin/debug/path/projectfolder/bin/release/。追加/../../resources/到那會帶給你/projectfolder/resources/,讓你不知道你的文件夾的完整路徑,因爲VB.NET發現它自己。

0

如上所述,這是一個古老的線索,但我認爲谷歌仍然會有人找到它(就像我今天所做的那樣)。

只是一個額外的建議,有時你不會在輸出目錄中有你的音頻文件或可能希望分發一個獨立的可執行文件。還有可能從輸出目錄中刪除該文件的風險,並且能夠重新創建該文件很方便。此外,具有資源的可執行文件的一部分,並在輸出目錄佔用不僅僅有它的可執行文件的部分更多的空間,所以有時它的價值只具有盤暫時上的文件(以後將它刪除),而不是有一個永久的副本,所以你可以訪問它。

在這種情況下,它是有用的,以便能夠採取從資源文件在運行時將其保存到磁盤某處(如果以後要刪除它可能是一個臨時文件位置),您可以訪問。

下面的非常簡單的代碼可以做到這一點:

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load 

    SaveResourceObject("FinalFantasyAdventure_Mission", "C:\FinalFantasyAdventure_Mission.mp3") 

End Sub 

Sub SaveResourceObject(Resource as String, FileName as String) 
    Dim myMemStream As New System.IO.MemoryStream 
    My.Resources.ResourceManager.GetObject(Resource).CopyTo(myMemStream) 
    Dim myBytes() As Byte = myMemStream.ToArray 
    My.Computer.FileSystem.WriteAllBytes(FileName, myBytes, False) 
End Sub 

注意,在上面的代碼資源變量實際上是資源的名稱,而不是資源本身。所以,如果你嘗試做以下,你會得到一個錯誤:

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load 
    SaveResourceObject(My.Resources.FinalFantasyAdventure_Mission, "C:\FinalFantasyAdventure_Mission.mp3") 
End Sub 

你可以修改代碼,很輕鬆地接受實際資源作爲參數,如果你喜歡。

請注意,我用「C:\」在上面的例子,但我建議你在應用程序數據文件夾中創建自己的文件夾,你可以找到使用:

Dim AppDataPath as String = System.Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData) 

,或者更只要你已經導入System.Environment:

Dim AppDataPath as String = GetFolderPath(SpecialFolder.ApplicationData)