我正在嘗試在Windows Phone 8.1上爲設定的頻率和持續時間生成一個音調。繼續介紹這裏的主題:Playing a sound from a generated buffer in a Windows 8 app,這裏是我嘗試的Windows Phone 8.1解決方案,在Visual Studio 2015中的模擬器中運行,在VB.NET中嘗試實現SharpDX.XAudio2。沒有聲音出現,但我認爲是正確的。有任何想法嗎?在Windows Phone 8.1應用程序中生成音調或聲音
' Initialization phase, keep this buffer during the life of your application
' Allocate 10s at 44.1Khz of stereo 16bit signals
Dim myBufferOfSamples = New Short(44100 * 10 * 2 - 1) {}
' Create a DataStream with pinned managed buffer
Dim ds = SharpDX.DataStream.Create(myBufferOfSamples, True, True)
Dim bu As New SharpDX.XAudio2.AudioBuffer
bu.Stream = ds
bu.AudioBytes = ds.Length
bu.Flags = SharpDX.XAudio2.BufferFlags.EndOfStream
'Fill myBufferOfSamples
Dim sampleBuffer() As Short = myBufferOfSamples
Dim sampleRate As Integer = 44100
Dim frequency As Double = 440
'
Dim totalTime As Double = 0
For i As Integer = 0 To sampleBuffer.Length - 2 Step 2
Dim sampleTime As Double = totalTime/sampleRate
Dim currentSample As Short
currentSample = Math.Sin(2 * Math.PI * frequency * sampleTime) * Short.MaxValue
sampleBuffer(i) = currentSample
sampleBuffer(i + 1) = currentSample
totalTime += 1
Next
' PCM 44.1Khz stereo 16 bit format
Dim waveFormat = New SharpDX.Multimedia.WaveFormat()
Dim xaudio As New SharpDX.XAudio2.XAudio2()
Dim masteringVoice As New SharpDX.XAudio2.MasteringVoice(xaudio)
Dim sourceVoice = New SharpDX.XAudio2.SourceVoice(xaudio, waveFormat, True)
' Submit the buffer
sourceVoice.SubmitSourceBuffer(bu, Nothing)
你介意分享改變後的內容嗎 ? – John
@John:請參閱下面的其他答案。 – swabygw
好的,明白。你能接受這個答案嗎? – John