简体   繁体   English

将C#代码转换为vb.net问题(Windows RT)

[英]Convert C# code to vb.net issue (Windows RT)

I'm trying to implements sound feature to a Windows RT app in vb.net with SharpDX, and I need to convert this class to vb.net: 我正在尝试使用SharpDX在vb.net中为Windows RT应用程序实现声音功能,并且需要将此类转换为vb.net:

using SharpDX.IO;
using SharpDX.Multimedia;
using SharpDX.XAudio2;
using System.Collections.Generic;
using System.Linq;

namespace PlayWaveRT {
public class WaveManager {
    private XAudio2 xAudio;
    private List<Wave> waves = new List<Wave>();

    public WaveManager() {
        xAudio = new XAudio2();
        var mastering = new MasteringVoice(xAudio);
        mastering.SetVolume(1, 0);
        xAudio.StartEngine();
    }

    public void LoadWave(string path, string key) {
        var buffer = GetBuffer(path);
        waves.Add(new Wave { Buffer = buffer, Key = key });
    }

    public void PlayWave(string key) {
        var wave = waves.FirstOrDefault(x => x.Key == key);
        if (wave != null) {
            var voice = new SourceVoice(xAudio, wave.Buffer.WaveFormat, true);
            voice.SubmitSourceBuffer(wave.Buffer, wave.Buffer.DecodedPacketsInfo);
            voice.Start();
        }
    }

    private AudioBufferAndMetaData GetBuffer(string soundfile) {
        var nativefilestream = new NativeFileStream(soundfile, NativeFileMode.Open, NativeFileAccess.Read, NativeFileShare.Read);
        var soundstream = new SoundStream(nativefilestream);
        var buffer = new AudioBufferAndMetaData {
            Stream = soundstream.ToDataStream(),
            AudioBytes = (int)soundstream.Length,
            Flags = BufferFlags.EndOfStream,
            WaveFormat = soundstream.Format,
            DecodedPacketsInfo = soundstream.DecodedPacketsInfo
        };
        return buffer;
    }

    private sealed class AudioBufferAndMetaData : AudioBuffer {
        public WaveFormat WaveFormat { get; set; }
        public uint[] DecodedPacketsInfo { get; set; }
    }

    private class Wave {
        public AudioBufferAndMetaData Buffer { get; set; }
        public string Key { get; set; }
    }
}
}

This is what I've done: 这是我所做的:

Imports SharpDX.IO
Imports SharpDX.Multimedia
Imports SharpDX.XAudio2
Imports System.Collections.Generic
Imports System.Linq

Namespace PlayWaveRT

Public Class WaveManager

    Private xAudio As XAudio2
    Private waves As New List(Of Wave)()

    Public Sub New()
        xAudio = New XAudio2()
        Dim mastering = New MasteringVoice(xAudio)
        mastering.SetVolume(1, 0)
        xAudio.StartEngine()
    End Sub

    Public Sub LoadWave(path As String, key1 As String)
        Dim buffer1 = GetBuffer(path)
        Dim wave As New Wave()
        wave.Buffer = buffer1
        wave.Key = key1
    End Sub

    Public Sub PlayWave(key1 As String)
        Dim wave = waves.FirstOrDefault(Function(x) x.Key = key1)
        If wave IsNot Nothing Then
            Dim voice = New SourceVoice(xAudio, wave.Buffer.WaveFormat, True)
            voice.SubmitSourceBuffer(wave.Buffer, wave.Buffer.DecodedPacketsInfo)
            voice.Start()
        End If
    End Sub

    Private Function GetBuffer(soundfile As String) As AudioBufferAndMetaData
        Dim nativefilestream = New NativeFileStream(soundfile, NativeFileMode.Open, NativeFileAccess.Read,  NativeFileShare.Read)
        Dim soundstream = New SoundStream(nativefilestream)
        Dim buffer = New AudioBufferAndMetaData()
        buffer.Stream = soundstream.ToDataStream()
        buffer.AudioBytes = CInt(soundstream.Length)
        buffer.Flags = BufferFlags.EndOfStream
        buffer.WaveFormat = soundstream.Format
        buffer.DecodedPacketsInfo = soundstream.DecodedPacketsInfo
        Return buffer
    End Function

    Private Class AudioBufferAndMetaData
        Inherits AudioBuffer

        Private m_WaveFormat As WaveFormat
        Private m_DecodedPacketsInfo As UInteger()

        Public Property WaveFormat As WaveFormat
            Get
                Return m_WaveFormat
            End Get
            Set(value As WaveFormat)
                m_WaveFormat = value
            End Set
        End Property

        Public Property DecodedPacketsInfo As UInteger()
            Get
                Return m_DecodedPacketsInfo
            End Get
            Set(value As UInteger())
                m_DecodedPacketsInfo = value
            End Set
        End Property

    End Class

    Private Class Wave
        Private m_Buffer As AudioBufferAndMetaData
        Private m_Key As String

        Public Property Buffer As AudioBufferAndMetaData
            Get
                Return m_Buffer
            End Get
            Set(value As AudioBufferAndMetaData)
                m_Buffer = value
            End Set
        End Property

        Public Property Key As String
            Get
                Return m_Key
            End Get
            Set(value As String)
                m_Key = value
            End Set
        End Property
    End Class

End Class

End Namespace

I have no compilator errors, but the code isn't working and I don't know where's the problem. 我没有编译器错误,但是代码无法正常工作,我也不知道问题出在哪里。

I've imported to my project SharpDX.dll and SharpDX.XAudio2.dll 我已经将SharpSharp.dll和SharpDX.XAudio2.dll导入到我的项目中

This is how I usage this class in my Project: 这是我在Project中使用此类的方式:

Imports The_Game_of_15.PlayWaveRT
Public NotInheritable Class SelectPage
Inherits Page

Private waveManager As New WaveManager()

Protected Overrides Sub OnNavigatedTo(e As Navigation.NavigationEventArgs)
    ..........
    ..........
    waveManager.LoadWave("select.wav", "select")
End Sub

Private Sub Button_Click(sender As Object, e As RoutedEventArgs)
    waveManager.PlayWave("select")
End Sub

End Class

I can compile the project with no errors, but no sound it's played when I click the button. 我可以正确编译项目,但是单击该按钮时不会播放任何声音。 Can anyone help me? 谁能帮我? Thanks in advance. 提前致谢。

You forget to Add your element in the list: 您忘记将元素Add到列表中:

C# C#

waves.Add(new Wave { Buffer = buffer, Key = key });

VB.Net VB.Net

Dim wave As New Wave()
wave.Buffer = buffer1
wave.Key = key1

That being said, it's easy to follow in the debugger... 话虽如此,在调试器中很容易遵循...

developerFusion C#<->VB.net complete developerFusion C#<-> VB.net 完成

Imports SharpDX.IO
Imports SharpDX.Multimedia
Imports SharpDX.XAudio2
Imports System.Collections.Generic
Imports System.Linq

Namespace PlayWaveRT
    Public Class WaveManager
        Private xAudio As XAudio2
        Private waves As New List(Of Wave)()

        Public Sub New()
            xAudio = New XAudio2()
            Dim mastering = New MasteringVoice(xAudio)
            mastering.SetVolume(1, 0)
            xAudio.StartEngine()
        End Sub

        Public Sub LoadWave(path As String, key As String)
            Dim buffer = GetBuffer(path)
            waves.Add(New Wave() With { _
                Key .Buffer = buffer, _
                Key .Key = key _
            })
        End Sub

        Public Sub PlayWave(key As String)
            Dim wave = waves.FirstOrDefault(Function(x) x.Key = key)
            If wave IsNot Nothing Then
                Dim voice = New SourceVoice(xAudio, wave.Buffer.WaveFormat, True)
                voice.SubmitSourceBuffer(wave.Buffer, wave.Buffer.DecodedPacketsInfo)
                voice.Start()
            End If
        End Sub

        Private Function GetBuffer(soundfile As String) As AudioBufferAndMetaData
            Dim nativefilestream = New NativeFileStream(soundfile, NativeFileMode.Open, NativeFileAccess.Read, NativeFileShare.Read)
            Dim soundstream = New SoundStream(nativefilestream)
            Dim buffer = New AudioBufferAndMetaData() With { _
                Key .Stream = soundstream.ToDataStream(), _
                Key .AudioBytes = CInt(soundstream.Length), _
                Key .Flags = BufferFlags.EndOfStream, _
                Key .WaveFormat = soundstream.Format, _
                Key .DecodedPacketsInfo = soundstream.DecodedPacketsInfo _
            }
            Return buffer
        End Function

        Private NotInheritable Class AudioBufferAndMetaData
            Inherits AudioBuffer
            Public Property WaveFormat() As WaveFormat
                Get
                    Return m_WaveFormat
                End Get
                Set
                    m_WaveFormat = Value
                End Set
            End Property
            Private m_WaveFormat As WaveFormat
            Public Property DecodedPacketsInfo() As UInteger()
                Get
                    Return m_DecodedPacketsInfo
                End Get
                Set
                    m_DecodedPacketsInfo = Value
                End Set
            End Property
            Private m_DecodedPacketsInfo As UInteger()
        End Class

        Private Class Wave
            Public Property Buffer() As AudioBufferAndMetaData
                Get
                    Return m_Buffer
                End Get
                Set
                    m_Buffer = Value
                End Set
            End Property
            Private m_Buffer As AudioBufferAndMetaData
            Public Property Key() As String
                Get
                    Return m_Key
                End Get
                Set
                    m_Key = Value
                End Set
            End Property
            Private m_Key As String
        End Class
    End Class
End Namespace

VB.Net inline: VB.Net内联:

Public Sub LoadWave(path As String, key1 As String)
    Dim buffer1 = GetBuffer(path)
    waves.Add(New Wave With {.Buffer = buffer,.Key = key})
End Sub

I also wrote a blog post on the VB Team Blog about how to play audio in Win8.1/WP8.1 apps using SharpDX: 我还在VB团队博客上写了一篇博客文章,内容涉及如何使用SharpDX在Win8.1 / WP8.1应用程序中播放音频:

http://blogs.msdn.com/b/vbteam/archive/2014/06/15/vb-universal-app-part-4-using-sharpdx-for-sound-effects-with-ioc-and-linkedfiles.aspx http://blogs.msdn.com/b/vbteam/archive/2014/06/15/vb-universal-app-part-4-using-sharpdx-for-sound-effects-with-ioc-and-linkedfiles。 aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM