mediaSoundPlayer.Play() does not always play right!

This is supposed to play a click sound. It usually does, but it frequently plays a PFFFFFFT instead!mediaSoundPlayer.Stream = My.Resources.PodVwBy
mediaSoundPlayer.Load()
mediaSoundPlayer.Play()

I have other sounds in the program, and, they misfire occasionally, too!



Answer this question

mediaSoundPlayer.Play() does not always play right!

  • Page123

    The built in help suggests something similar to this:

    Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    MediaSoundPlayer.Stream = My.Resources.dizzymis

    MediaSoundPlayer.Load()

    End Sub

    Sub Player_LoadCompleted( _

    ByVal sender As Object, _

    ByVal e As _

    System.ComponentModel.AsyncCompletedEventArgs) _

    Handles MediaSoundPlayer.LoadCompleted

    If Me.MediaSoundPlayer.IsLoadCompleted Then

    Me.MediaSoundPlayer.Play()

    End If

    End Sub

    End Class

    Or help using a different approach:

    Sub PlaySoundFromResource()
    Dim sndPing as new SoundPlayer(SoundRes.GetType(), "Ping.wav")
    sndPing.Play()
    End Sub

    Or if you see my earlier post to you last question, you may need to save the System.IO.MemoryStream to variables which MAY load the sound faster. I have no idea on which memory management technique might work best.



  • vanRepin

    I would think that playing a click sound when a key is pressed would be a very common thing! Why is this so difficult Half of the time I get a PFFFFFFFFT instead of a click!

    Dim clickerx = My.Resources.clickerx

    PlaySound(clickerx)

    Private Sub PlaySound(ByVal work)
    mediaSoundPlayer.Stream = work
    mediaSoundPlayer.Load()
    Do Until Me.mediaSoundPlayer.IsLoadCompleted
    Loop
    Me.mediaSoundPlayer.Play()
    End Sub


  • NoteMe

    Assigning a diifferent player for each sound may help, as well as loading the streams during the form load. The following seems to work well except that if someone repeatedly clicks like mad, everything else will come to a stop until every pending sound has been played.

    The smaller the WAV file, the better the preceived 1 for 1 response is.

    (Tested with some XP system sounds imported to be resources, and renamed.)

    In form1.designer.vb

    ...

    Friend WithEvents Button1 As System.Windows.Forms.Button

    Private WithEvents Player As New Media.SoundPlayer

    Private WithEvents Player2 As New Media.SoundPlayer

    Friend WithEvents Button2 As System.Windows.Forms.Button

    In form1.vb

    Imports System.Media

    Public Class Form1

    Function GetSound(ByVal resname As String) As System.IO.MemoryStream

    Dim obj As Object = My.Resources.ResourceManager.GetObject(resname)

    Dim b As System.IO.MemoryStream = obj

    Return b

    End Function

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Player.PlaySync()

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

    Player2.PlaySync()

    End Sub

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    Player.Stream = GetSound("d1")

    Player2.Stream = GetSound("d2")

    End Sub

    Private Sub Button1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button1.MouseUp

    ' these STOP statements did not seem to have any effect

    My.Computer.Audio.Stop()

    Player.Stop()

    End Sub

    Private Sub Button2_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button2.MouseUp

    ' these STOP statements did not seem to have any effect

    My.Computer.Audio.Stop()

    Player2.Stop()

    End Sub

    End Class



  • mediaSoundPlayer.Play() does not always play right!