播放Wav文件过快会导致混乱。

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

每次添加字符时,我希望它发出那种小小的哔声。我有一个包含短暂“嘀嗒”声的wav文件,听起来很合适,但问题是,每次进行哔声时,它通常会出现问题。

它要么:

  • 跳过逐个字符的过程,只显示完整单词并哔声一次
  • 延迟并正确地执行几次bip,然后执行上面列出的事物

这是复杂的地方。我在使用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中播放声音的片段:

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

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

原文链接 https://stackoverflow.com/questions/8469111

点赞
stackoverflow用户495455
stackoverflow用户495455

我使用反射和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()

如果您使用的是 .Net Framework 2.0 或更高版本,则只需要使用 Beep() 就可以了。

我没有深入反射,但是检查一下SoundPlayer是否使用了PlaySound API也是值得一试的:

    <DllImport("coredll.dll")> _
Public Shared Function PlaySound(szSound As String, hModule As IntPtr, flags As Integer) As Integer
End Function
2011-12-13 02:52:26