玩太快的Wav会搞砸

时间:2011-12-12 01:50:37

标签: .net vb.net audio lua

我正在重新创造一个旧的16位游戏。我正在创建通常显示在底部的聊天。每个句子都按字符转换。

每次添加一个角色时,我都想让它产生那么小的哔哔声。我有一个wav文件,其中包含一个听起来恰到好处的简短'blip',问题是,当我每次都有它时,它通常会混乱。

要么:

  • 跳过逐个字符的过程,只显示完整的单词,然后再次显示
  • 正确地抓住并做几个bips,然后做上面列出的事情

这是复杂的地方。我正在使用Lua和VB.net。引擎,我用VB.net写的,而实际的游戏机制和故事线由Lua控制。

以下是Lua的基本信息:

RegisterEvent("ready", function()
    _G.Chat={}
    _G.Chat["busy"]=false
    _G.Chat.Say=(function(from,msg,done)
        if _G.Chat["busy"] then return end
        _G.Chat["busy"]=true
        local x,y=getRelativePositionOpposite(1024,192)
        --Draw
        local chatPanel=engine:AddSprite("chat.png",0,0,1024,192,x,y,1024,192,5)
        local fromText=engine:AddString(from..":",x+25,y+25,"#FFFFFF",16,0,0)
        local msgText=nil
        local mx=string.len(msg)
        --Chat Cleanup
        setupCleanup=(function()
            local g=true
            RegisterEvent("keyup", function(key)
                if not g then return end
                if key=="Space" then
                    engine:RemoveSprite(chatPanel)
                    engine:RemoveString(fromText)
                    engine:RemoveString(msgText)
                    _G.Chat["busy"]=false
                    done()
                    g=false
                end
            end)
        end)
        doText=(function(i)
            if msgText then
                engine:RemoveString(msgText)
            end
            msgText=engine:AddString(string.sub(msg,1,i),x,y+75,"#FFFFFF",14,1,0)
            engine:PlaySound("chatblip.wav")
            if i>=mx then setupCleanup() return end
            pause(.75,(function() doText(i+1) end))
        end)
        doText(1)
    end)
end)

这是暂停功能,仅供参考(在Lua中):

_G.pause=(function(t,f)
    if t and f then
        local tt=engine.timer.ElapsedMilliseconds/1000+t 
        local lcc=true
        engine.event_tick:add(function(nt)
            if nt>=tt and lcc then
                f()
                lcc=false
            end
        end)
    end
end)

这是在VB.net中实际播放噪音的snippit:

Public Sub PlaySound(ByVal fileFullPath As String)
    My.Computer.Audio.Play(My.Computer.FileSystem.CurrentDirectory & "\bin\sounds\" & fileFullPath, AudioPlayMode.Background)
End Sub

谢谢,如果你能提供帮助!如果您需要任何澄清,我非常愿意提供帮助!

1 个答案:

答案 0 :(得分:1)

我使用了反射器,Audio.Play的内部实现使用了SoundPlayer:

Public Sub Play(ByVal location As String, ByVal playMode As AudioPlayMode)
    Me.ValidateAudioPlayModeEnum(playMode, "playMode")
    Dim sound As New SoundPlayer(Me.ValidateFilename(location))
    Me.Play(sound, playMode)
End Sub

读取每个角色的声音文件对IO非常密集。

克服性能。您可以尝试添加对Microsoft.VisualBasic.dll程序集的引用并使用:

<强> Microsoft.VisualBasic.Interaction.Beep()

或者只是Beep()如果您使用.Net Framework 2.0和/或更高。

我没有在反射器中钻得很远,但是如果SoundPlayer使用PlaySound API并且如果它也没有提供该方法,也可能值得检查:

    <DllImport("coredll.dll")> _
Public Shared Function PlaySound(szSound As String, hModule As IntPtr, flags As Integer) As Integer
End Function