Xbox LIVE Indie Games
Sort Discussions: Previous Discussion Next Discussion
Page 1 of 1 (3 posts)

Play Audio

Last post 11/3/2017 8:54 AM by CheeseJournal. 2 replies.
  • 6/4/2014 4:32 PM

    Play Audio

    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  
        Private lblFile As Label  
        Private btnLoad, btnPlay As Button  
        Private path As String 
        Private sound As Microsoft.Xna.Framework.Audio.SoundEffect  
        Private Sub Form1_Load(ByVal sender As System.ObjectByVal 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 ObjectByVal 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 ObjectByVal e As EventArgs)  
            Dim firstException As String = String.Empty  
                sound = Microsoft.Xna.Framework.Audio.SoundEffect.FromStream(IO.File.OpenRead(path))  
            Catch ex As Exception  
                firstException = ex.ToString  
            End Try 
            Dim secondException As String = String.Empty  
            If Not String.IsNullOrWhiteSpace(firstException) Then 
                    sound = New Microsoft.Xna.Framework.Audio.SoundEffect(IO.File.ReadAllBytes(path), 11025, Microsoft.Xna.Framework.Audio.AudioChannels.Mono)  
                Catch ex As Exception  
                    secondException = ex.ToString  
                End Try 
            End If 
            If Not String.IsNullOrWhiteSpace(firstException) Then 
            End If 
            If Not String.IsNullOrWhiteSpace(secondException) Then 
            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.
  • 6/25/2014 8:21 PM In reply to

    Re: Play Audio

    The exception happens when there's a problem with the data. The most likely problem is that it isn't in the correct format. To use SoundEffect.FromStream, the stream must be a PCM file in RIFF bitstream format. It must be mono or stereo. It must be 8-bit or 16-bit. And the sample rate must be between 8000 Hz and 48000 Hz. (It's all here: ).
  • 11/3/2017 8:54 AM In reply to

    Re: Play Audio

    I know this is a pretty old thread but share some information about how to convert Dailymotion(as game audio or others), first recommend this tutorial( with this one, you can download any Dailymotion audio and play them or convert them. Cheers!
Page 1 of 1 (3 posts) Previous Discussion Next Discussion
var gDomain=''; var gDcsId='dcschd84w10000w4lw9hcqmsz_8n3x'; var gTrackEvents=1; var gFpc='WT_FPC'; /*<\/scr"+"ipt>");} /*]]>*/