gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 21:37:38 24 4
gpt4 key购买 nike

我正在尝试使用 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; }
}
}
}

这是我所做的:

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

我没有编译器错误,但代码无法正常工作,我不知道问题出在哪里。

我已经将 SharpDX.dll 和 SharpDX.XAudio2.dll 导入到我的项目中

这就是我在我的项目中使用这个类的方式:

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

我可以毫无错误地编译项目,但是当我点击按钮时它没有播放任何声音。谁能帮我?提前致谢。

最佳答案

您忘记在列表中添加您的元素:

C#

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

VB.Net

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

话虽如此,在调试器中很容易理解...

关于c# - 将 C# 代码转换为 vb.net 问题 (Windows RT),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20887955/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com