Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Sound doesn't play instantly...but wait for next operation to complete!!

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

Scheduled Pinned Locked Moved Visual Basic
debugginghelpquestionworkspace
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Jayme65
    wrote on last edited by
    #1

    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!

    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

    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!!

    Richard DeemingR B 2 Replies Last reply
    0
    • J Jayme65

      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!

      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

      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!!

      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      You've moved the wrong bit to the background thread.

      Dim computeThread As Thread

      Private Sub RunTest()
      uiPlayer.URL = writingPath & "son1.wav"
      uiPlayer.controls.play()

      computeThread = new Thread(AddressOf Compute)
      computeThread.Start()
      

      End Sub

      Private Sub Compute()
      Debug.Print("Started!")

      Dim a As Integer = 0
      For i As Integer = 0 To 999999999
          a = i
      Next
      
      Debug.Print("Done!")
      

      End Sub


      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      1 Reply Last reply
      0
      • J Jayme65

        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!

        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

        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!!

        B Offline
        B Offline
        Bernhard Hiller
        wrote on last edited by
        #3

        There are two possibilities to handle audio playing: one involves the message pump, the other one involves callback methods. I guess the WMPLib.WindowsMediaPlayer uses the message pump, and runs in the GUI thread of your application (as you explicitly call it uiPlayer). In that case, the messages can only be dealt with when the GUI of your apllication is ready to handle them. You may test that hypotheses by adding an Application.DoEvents() to your for loop (do not do that for production code): playing should now be possible.

        J 1 Reply Last reply
        0
        • B Bernhard Hiller

          There are two possibilities to handle audio playing: one involves the message pump, the other one involves callback methods. I guess the WMPLib.WindowsMediaPlayer uses the message pump, and runs in the GUI thread of your application (as you explicitly call it uiPlayer). In that case, the messages can only be dealt with when the GUI of your apllication is ready to handle them. You may test that hypotheses by adding an Application.DoEvents() to your for loop (do not do that for production code): playing should now be possible.

          J Offline
          J Offline
          Jayme65
          wrote on last edited by
          #4

          It doesn't work better. 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:

          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:

          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. If you try this code you will verify that inserting

          System.Windows.Forms.Application.DoEvents()

          after uiPlayer.controls.play()...doesn't work better Thanks for your help!!

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups