Sound doesn't play instantly...but wait for next operation to complete!!
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: Sound doesn't play instantly...but wait for next operation to complete!!

Hybrid View

  1. #1
    Join Date
    Apr 2011
    Posts
    15

    Sound doesn't play instantly...but wait for next operation to complete!!

    I've a big interrogation concerning sound playback (WMP.lib)!
    This code should play a sound, then compute...but it computes then play the sound!


    Code:
    Class MainWindow
        Dim writingPath As String = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) & "\_UI Sounds\"
        Dim uiPlayer As New WMPLib.WindowsMediaPlayer
        Private Sub MainWindow_Loaded(sender As Object, e As System.Windows.RoutedEventArgs) Handles Me.Loaded
            RunTest()
        End Sub
        Private Sub RunTest()
            Debug.Print("Started!")
            uiPlayer.URL = writingPath & "son1.wav"
            uiPlayer.controls.play()
            '
            Dim a As Integer = 0
            For i As Integer = 0 To 999999999
                a = i
            Next
            Debug.Print("Done!")
        End Sub
    End Class

    I thought that I perhaps should run the sound in a thread to be sure that it's played by its own!?
    But this gives the same result


    Code:
    Imports System.Threading
    Class MainWindow
        Dim writingPath As String = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) & "\_UI Sounds\"
        Dim uiPlayer As New WMPLib.WindowsMediaPlayer
        Dim uiThread As Thread = New Thread(AddressOf PlayFile)
        Private Sub MainWindow_Loaded(sender As Object, e As System.Windows.RoutedEventArgs) Handles Me.Loaded
            RunTest()
        End Sub
        Private Sub RunTest()
            uiThread = New Thread(AddressOf PlayFile)
            uiThread.Start()
            '
            Debug.Print("Started!")
            Dim a As Integer = 0
            For i As Integer = 0 To 999999999
                a = i
            Next
            Debug.Print("Done!")
        End Sub
        Private Sub PlayFile()
            uiPlayer.URL = writingPath & "son1.wav"
            uiPlayer.controls.play()
        End Sub
    End Class

    How should I please proceed to hear my sound 'instantly'?
    What's my mistake?

    Thanks for your help!!

  2. #2
    DataMiser is offline Super Moderator Power Poster
    Join Date
    Jul 2008
    Location
    WV
    Posts
    4,813

    Re: Sound doesn't play instantly...but wait for next operation to complete!!

    Why are you using a busy loop that basically does nothing other than consume processor?
    Always use [code][/code] tags when posting code.

  3. #3
    Join Date
    Apr 2011
    Posts
    15

    Re: Sound doesn't play instantly...but wait for next operation to complete!!

    Well...I thought it was clear!! This is to illustrate a consuming process and keep the code simple to ask my question !!
    Any idea?

  4. #4
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,090

    Re: Sound doesn't play instantly...but wait for next operation to complete!!

    No need for attitude...

    Anyways, there are other ways to play a sound, have a look at this FAQ :

    http://forums.codeguru.com/showthrea...75#post1728375

  5. #5
    Join Date
    Apr 2011
    Posts
    15

    Re: Sound doesn't play instantly...but wait for next operation to complete!!

    Well, I had posted a simple code, hoping that it would be more clear for users to help me...but I'm afraid that I'd better post something very similar to my actual code..if I want the problem to be clearly exposed...and help you to try to help me ;-)

    There's a sound class, called UISound:

    Code:
    Imports System.Threading
    Public Class UISound
        Dim writingPath As String = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) & "\_UI Sounds\"
        Public Shared WithEvents uiPlayer As New WMPLib.WindowsMediaPlayer
        Public Shared uiThread As Thread = New Thread(AddressOf ReceiveSound)
        Public Shared Sub ReceiveSound(ByVal url As String)
            uiThread = New Thread(AddressOf PlayFile)
            uiThread.Start(url)
        End Sub
        Public Shared Sub PlayFile(ByVal url As String)
            uiPlayer.URL = writingPath & url
            uiPlayer.controls.play()
        End Sub
    End Class
    The sound is called from a sub in the MainWindow...which itself fires other subs in cascade that involve a heavy compute time:

    Code:
    Sub one()
        UISound.ReceiveSound("sound1.wav")
        Dim a As Integer = 0
        For i As Integer = 0 To 999999999
            a = i
        Next
        Two()
    End Sub
    Sub two()
        Dim a As Integer = 0
        For i As Integer = 0 To 999999999
            a = i
        Next
        Three()
    End Sub
    Sub Three()
        Dim a As Integer = 0
        For i As Integer = 0 To 999999999
            a = i
        Next
    End Sub
    It appears that the sound is played after the whole "cascade" is runned, so after Sub Three() End.

    Thanks for your help!!
    Last edited by Jayme65; September 6th, 2013 at 02:37 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center