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

  • (2 Pages)
  • +
  • 1
  • 2

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

#16 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 - 11:18 PM

Just for kicks, I tried my own suggestion, to play the soound in the background. For this, I imported a song that is about 3.5 minutes long. I then added a StopWatch and some Debug.Print() statements. Here's the code

Option Strict On
Imports System.Media
Imports Microsoft.VisualBasic.Devices

Public Class MainWindow
    Dim player As New Audio
    Dim sw As New Stopwatch

    Sub One()
        sw.Start()
        playIt()
        Debug.Print("One    " & sw.Elapsed.ToString)
        Dim a As Integer = 0
        For i As Integer = 0 To 999999999
            a = i
        Next
        Call Two()
    End Sub

    Private Sub playIt()
        Debug.Print("Play   " & sw.Elapsed.ToString)
        My.Computer.Audio.Play(My.Resources.Love_is_Alright, AudioPlayMode.Background)
    End Sub

    Sub Two()
        Dim a As Integer = 0
        Debug.Print("Two    " & sw.Elapsed.ToString)
        For i As Integer = 0 To 999999999
            a = i
        Next
        Call Three()
    End Sub

    Sub Three()
        Dim a As Integer = 0
        Debug.Print("Three  " & sw.Elapsed.ToString)
        For i As Integer = 0 To 999999999
            a = i
        Next
        Debug.Print("End    " & sw.Elapsed.ToString)
    End Sub

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


The start of the song and the printing of "One..." was visually close to simultaneous. The stopwatch tells the story though. All the time, right up to past the end of the calculations, the song was playing without any dropouts or distortion. Here are the times:


'    Play   00:00:00.0003422
'    One    00:00:00.3218991
'    Two    00:00:03.8127344
'    Three  00:00:07.2065736
'    End    00:00:10.5993163


Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2