I'm not expecting much considering that my last post is the 3nd to last post made, there were 0 replies, and it's been since November of last year since I asked it. However this is the only place I know to go to. I'm currently using Visual Basic 2010 Express and I'm trying to play a WAV file using XNA. I don't have access to the Content Pipeline, so that is out of the question. Here is what I have so far:
Option Strict On |
Option Explicit On |
|
'Add the Microsoft.XNA.Framework reference |
Public Class Form1 |
'Controls |
Private lblFile As Label |
Private btnLoad, btnPlay As Button |
|
'Variable |
Private path As String |
Private sound As Microsoft.Xna.Framework.Audio.SoundEffect |
|
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load |
lblFile = New Label With {.AutoSize = True, .Location = New Point(5, 5), .Text = "File: "} |
btnLoad = New Button With {.Left = lblFile.Left, .Text = "Load", .Top = lblFile.Bottom + 5} |
btnPlay = New Button With {.Left = btnLoad.Right + 5, .Enabled = False, .Text = "Play", .Top = btnLoad.Top} |
|
AddHandler btnLoad.Click, AddressOf btnLoad_Click |
AddHandler btnPlay.Click, AddressOf btnPlay_Click |
|
Me.Controls.AddRange({lblFile, btnLoad, btnPlay}) |
|
End Sub |
|
Private Sub btnLoad_Click(ByVal sender As Object, ByVal e As EventArgs) |
Using ofd As OpenFileDialog = New OpenFileDialog |
With ofd |
.FileName = String.Empty |
.Filter = "MP3|*.mp3|WAV|*.wav" |
.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyMusic) |
.Multiselect = False |
.RestoreDirectory = True |
.Title = "Load Sound" |
End With |
|
If ofd.ShowDialog = DialogResult.OK Then |
path = ofd.FileName |
|
lblFile.Text = "File: " & ofd.SafeFileName |
|
btnPlay.Enabled = True |
End If |
End Using |
End Sub |
|
Private Sub btnPlay_Click(ByVal sender As Object, ByVal e As EventArgs) |
Dim firstException As String = String.Empty |
|
Try |
sound = Microsoft.Xna.Framework.Audio.SoundEffect.FromStream(IO.File.OpenRead(path)) |
sound.Play() |
Catch ex As Exception |
firstException = ex.ToString |
End Try |
|
Dim secondException As String = String.Empty |
If Not String.IsNullOrWhiteSpace(firstException) Then |
Try |
sound = New Microsoft.Xna.Framework.Audio.SoundEffect(IO.File.ReadAllBytes(path), 11025, Microsoft.Xna.Framework.Audio.AudioChannels.Mono) |
sound.Play() |
Catch ex As Exception |
secondException = ex.ToString |
End Try |
End If |
|
If Not String.IsNullOrWhiteSpace(firstException) Then |
Console.WriteLine(firstException) |
End If |
|
If Not String.IsNullOrWhiteSpace(secondException) Then |
Console.WriteLine(secondException) |
End If |
End Sub |
|
End Class |
Whenever I try those to methods of playing the file, I get these exceptions:
Exception 1:
System.InvalidOperationException: Operation is not valid due to the current state of the object.
at Microsoft.Xna.Framework.Audio.WavFile.ParseWavHeader()
at Microsoft.Xna.Framework.Audio.WavFile..ctor(Stream source)
at Microsoft.Xna.Framework.Audio.WavFile.Open(Stream stream)
at Microsoft.Xna.Framework.Audio.SoundEffect..ctor(Stream stream)
at Microsoft.Xna.Framework.Audio.SoundEffect.FromStream(Stream stream)
at WindowsApplication1.Form1.btnPlay_Click(Object sender, EventArgs e) in C:\Users\pc1\AppData\Local\Temporary Projects\WindowsApplication1\Form1.vb:line 51
Exception 2:
System.ArgumentException: Ensure that the buffer length is non-zero and meets the block alignment requirements for the audio format.
at Microsoft.Xna.Framework.Audio.SoundEffect.FromBuffer(Byte[] buffer, Int32 offset, Int32 count, Int32 sampleRate, AudioChannels channels, Int32 loopStart, Int32 loopLength)
at Microsoft.Xna.Framework.Audio.SoundEffect..ctor(Byte[] buffer, Int32 sampleRate, AudioChannels channels)
at WindowsApplication1.Form1.btnPlay_Click(Object sender, EventArgs e) in C:\Users\pc1\AppData\Local\Temporary Projects\WindowsApplication1\Form1.vb:line 60
Line 51 in exception 1 is when I initialize my sound variable using FromStream. Line 60 in exception 2 is when I try to set sound to a new instance.