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

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 749 Views - Last Post: 06 September 2013 - 11:18 PM Rate Topic: -----

#1 Jayme65  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 19-September 12

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

Posted 03 September 2013 - 06:44 AM

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

Is This A Good Question/Topic? 0
  • +

Replies To: Sound doesn't play instantly...but wait for next operation to comp

#2 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

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

Posted 03 September 2013 - 10:18 AM

View PostJayme65, on 03 September 2013 - 07:44 AM, said:

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

Thanks for your help!!

I tried your threaded code, and it would not let me compile. It did not recognize the e As System.Windows.RoutedEventArgs part of the Form load event. I changed it to the default args...

    Private Sub MainWindow_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        RunTest()
    End Sub


This runs, and works. One comment though, is that it does take some time for the Windows Media Player and the sound to load, and it would appear that the computation afterward is competeing for CPU cycles with the loading process. Try replacing the a = 1 statement with Debug.Print(i). You'll see that the computation is actually going on, and it will count up for a long time before the sound plays.

If you are not interested in songs, but rather in short sounds, you might want to have a look at the SoundPlayer Class.
Was This Post Helpful? 0
  • +
  • -

#3 Jayme65  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 19-September 12

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

Posted 03 September 2013 - 11:16 AM

Quote

You'll see that the computation is actually going on, and it will count up for a long time before the sound plays.

lar3ry,
Thanks for your reply...but this is exactly what I want to avoid...and the subject of my question!
Why does the (very short) sound wouldn't play immediately!!
The code you (kindly) gives me make no change :(
Was This Post Helpful? 0
  • +
  • -

#4 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

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

Posted 03 September 2013 - 12:42 PM

View PostJayme65, on 03 September 2013 - 12:16 PM, said:

Thanks for your reply...but this is exactly what I want to avoid...and the subject of my question!
Why does the (very short) sound wouldn't play immediately!!
The code you (kindly) gives me make no change :(/>/>

It wasn't meant to make a change. It was meant to show you that it worked, in that your first post assumed that the computation preceded the playing of the sound, when in fact, the sound was in the process of being played while the calculations were proceeding.

Did you try the Debug.Print(i) in place of the a = 1? Do you understand what it signifies?

Let's try again.

When you create a New WindowsMediaPlayer object, and give it a URL, then tell it to play, it has to load the WindowsMediaPlayer, then load the sound, and then start playing it. This is a fair bit of work for the CPU, and while it is going on, your calculations ARE proceeding. In fact, your calculations are slowing down the loading of the WindowsMediaPlayer and the loading of the song, which is already slow enough as it is. In turn, the loading is slowing down the calculations.

Again, you might want to investigate the SoundPlayer Class, and see if you can pre-load your sounds.

As an example, the first entry in the SoundPlayer Class docs is entitled How to: Play a Sound from a Windows Form and the example given is

        My.Computer.Audio.Play("c:\Windows\Media\chimes.wav")


There's more to the Class, of course, so it bears looking at to see what you can do with it.
Using WindowsMediaPlayer to play a short sound is rather like traveling through Texas to go from Seattle to Portland.

This post has been edited by lar3ry: 03 September 2013 - 12:46 PM

Was This Post Helpful? 0
  • +
  • -

#5 Jayme65  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 19-September 12

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

Posted 03 September 2013 - 03:06 PM

Quote

It wasn't meant to make a change. It was meant to show you that it worked, in that your first post assumed that the computation preceded the playing of the sound, when in fact, the sound was in the process of being played while the calculations were proceeding.

No, no...it's clear in my mind that line 10 (play sound) is processed before a line 12 ;-)
...and that, due to the fact that WMPLib plays asynchronously, the code following is processed while the player try to play the sound!

But I thought that launching it in another thread would give the sound a chance to be played "directly"...but it doesn't seem to be the case!?

Unfortunately the sound format is .mp3...so it cannot be played with My.Computer.Audio.Play, so I'm left in the same situation.

Thanks for your help and patience ;-)
Was This Post Helpful? 0
  • +
  • -

#6 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

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

Posted 03 September 2013 - 05:53 PM

View PostJayme65, on 03 September 2013 - 04:06 PM, said:

But I thought that launching it in another thread would give the sound a chance to be played "directly"...but it doesn't seem to be the case!?

Unfortunately the sound format is .mp3...so it cannot be played with My.Computer.Audio.Play, so I'm left in the same situation.

If the sounds are fixed (some number of sounds that are always played), you could change them to .wav format. It's easy enough to do, using a freeware converter (Any Audio Converter for example).

You might also want to see about putting a WindowsMediaPlayer on your form (you can always hide it), pre-loading your sounds while doing other tasks, and then playing them. Sounds may be loaded in a separate thread, if you have other things to do, and you can play with the priorities to fine-tune the operations to make it appear seamless to the user.
Was This Post Helpful? 0
  • +
  • -

#7 Jayme65  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 19-September 12

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

Posted 06 September 2013 - 12:19 AM

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
    Call Two()
End Sub
Sub Two()
    Dim a As Integer = 0
    For i As Integer = 0 To 999999999
        a = i
    Next
    Call 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!!

This post has been edited by andrewsw: 06 September 2013 - 01:47 AM
Reason for edit:: Added Calls

Was This Post Helpful? 0
  • +
  • -

#8 Jayme65  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 19-September 12

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

Posted 06 September 2013 - 12:45 AM

Ooops..typing error! Call to subs should of course be Two() and Three()
Was This Post Helpful? 0
  • +
  • -

#9 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3253
  • View blog
  • Posts: 10,908
  • Joined: 12-December 12

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

Posted 06 September 2013 - 01:49 AM

View PostJayme65, on 06 September 2013 - 07:45 AM, said:

Ooops..typing error! Call to subs should of course be Two() and Three()

I have corrected the Calls but why have you re-typed this code and not simply copied and pasted it?

BTW Integers don't get that big Oops, thought there was one more 9, an optical illusion!

This post has been edited by andrewsw: 06 September 2013 - 05:20 AM

Was This Post Helpful? 0
  • +
  • -

#10 Michael26  Icon User is offline

  • DIC-head, major DIC-head
  • member icon

Reputation: 353
  • View blog
  • Posts: 1,508
  • Joined: 08-April 09

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

Posted 06 September 2013 - 04:47 AM

Integer Data Type

Quote

Holds signed 32-bit (4-byte) integers that range in value from -2,147,483,648 through 2,147,483,647.

Was This Post Helpful? 1
  • +
  • -

#11 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3253
  • View blog
  • Posts: 10,908
  • Joined: 12-December 12

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

Posted 06 September 2013 - 05:22 AM

View PostMichael26, on 06 September 2013 - 11:47 AM, said:

Integer Data Type

Quote

Holds signed 32-bit (4-byte) integers that range in value from -2,147,483,648 through 2,147,483,647.

Oops, an optical illusion, thought there were more nines.
Was This Post Helpful? 0
  • +
  • -

#12 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

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

Posted 06 September 2013 - 02:15 PM

I haven't been having any luck with your code. I could not even get it to compile, as I get an error on line 11, something to the effect that I canot refer to a member of a Class without an instance of the Class.

Anyway, I was playing around, trying to get something loaded into a Resource to avoid having to load it from a file. Here's a result for you that I think you'll be happy with.

First, load some files as resources. Make them Audio type. , and as you make them, in the Properties window, select "embedded in .resx" If you're unsure on how to do this, let us know.

Here's a quick example, where I don't specify the sound to be played in the call to playIt(), so it only plays the one sound. But you can make parameters and pass them in. I made a form with one button and used the button to call One(). And of course you can have more than 1 sound resource.

Option Strict On

Imports System.Media
Imports Microsoft.VisualBasic.Devices

Public Class MainWindow

    Dim player As New Audio

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

    Sub One()
        playIt()
        Dim a As Integer = 0
        For i As Integer = 0 To 999999999
            a = i
        Next
        Call Two()
    End Sub

    Private Sub playIt()
        My.Computer.Audio.Play(My.Resources.Windows_Shutdown, AudioPlayMode.WaitToComplete)
    End Sub

    Sub Two()
        Dim a As Integer = 0
        For i As Integer = 0 To 999999999
            a = i
        Next
        Call Three()
    End Sub

    Sub Three()
        Dim a As Integer = 0
        For i As Integer = 0 To 999999999
            a = i
        Next
    End Sub
End Class


Was This Post Helpful? 0
  • +
  • -

#13 Jayme65  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 19-September 12

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

Posted 06 September 2013 - 02:33 PM

Thanks lar3ry,

But...I wouldn't have any probelm with "My.Computer.Audio.Play"...the problem comes with WindowsMediaPlayer (wmplib)..which plays sound async!!
Was This Post Helpful? 0
  • +
  • -

#14 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

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

Posted 06 September 2013 - 03:09 PM

View PostJayme65, on 06 September 2013 - 03:33 PM, said:

But...I wouldn't have any probelm with "My.Computer.Audio.Play"...the problem comes with WindowsMediaPlayer (wmplib)..which plays sound async!!

Not sure what you mean here. Are you saying you want the sound to start immediately, but play async (in the backgroud?), perhaps because it's a long .wav file?

If so, you might want to try replacing AudioPlayMode.WaitToComplete with AudioPlayMode.Background.

The main thing this does away with is the long instantiation of WindowsMediaPlayer and the load time of the .wav file. Including it as a resource means that you get it immediately, and without having to make a New WindowsMediaPlayer.
Was This Post Helpful? 0
  • +
  • -

#15 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2241
  • View blog
  • Posts: 9,418
  • Joined: 29-May 08

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

Posted 06 September 2013 - 10:22 PM

OP could be using WPF / Silverlight because that use RoutedEventArgs
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2