简体   繁体   中英

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:

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

This is how I usage this class in my 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:

C#

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

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

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:

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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