Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

Sound player (play, play loop, stop) in VB and C# (.net 2.0).Part2

Author: Zunnair
Download Source Code : 920_code.zip

In this simple article you will learn that how we can make sound player and can perform different operations using “sound player class” in VB and C# .net 2.0.

 

Read first part of this article to understand more.
Play:
To play the sound we have a function named play (). Play function takes and returns nothing. It plays the file whose path is saved in sound location property. Play only plays this one time.
To demonstrate make a window application. Drag text box and button on form. Write path in text box and press button.
Now write the following code on Button click event:
C#
private void btn_Play_Click(object sender, EventArgs e)
        {
            string str;
            str = txt_Path.Text;
            if (str != string.Empty)
            {
                obj.SoundLocation = str;
                obj.Load();
                if (obj.IsLoadCompleted)
                {
                    obj.Play();
                }
                else
                    MessageBox.Show("file not loaded");
            }
            else
                MessageBox.Show("enter path");
        }    
VB
Private Sub btn_Play_Click(ByVal sender As Object, ByVal e As EventArgs)
    Dim str As String
    str = txt_Path.Text
    If str <> String.Empty Then
        obj.SoundLocation = str
        obj.Load()
        If obj.IsLoadCompleted Then
            obj.Play()
        Else
            MessageBox.Show("file not loaded")
        End If
    Else
        MessageBox.Show("enter path")
    End If
End Sub
This is simple code to play a wav file.
Play Loop:
Play function plays file only once. If you want to play a file again and again till you do not stop it, for this purpose we have a function named play looping. This function does not take any parameter and returns nothing.
To demonstrate make a window application. Drag text box and button on form. Write path in text box and press button.
Now write the following code on Button click event:
C#
private void btn_PlayLoop_Click(object sender, EventArgs e)
        {
            string str;
            str = txt_Path.Text;
            if (str != string.Empty)
            {
                obj.SoundLocation = str;
                obj.Load();
                if (obj.IsLoadCompleted)
                {
                    obj.PlayLooping();
                }
                else
                    MessageBox.Show("file not loaded");
            }
            else
                MessageBox.Show("enter path");
       }      
VB
Private Sub btn_PlayLoop_Click(ByVal sender As Object, ByVal e As EventArgs)
    Dim str As String
    str = txt_Path.Text
    If str <> String.Empty Then
        obj.SoundLocation = str
        obj.Load()
        If obj.IsLoadCompleted Then
            obj.PlayLooping()
        Else
            MessageBox.Show("file not loaded")
        End If
    Else
        MessageBox.Show("enter path")
    End If
End Sub
This is simple code to play a wav file again and again.
Stop:
To stop the wave file which is playing we have a function named stop. Stop function does not take any parameter and does not return anything. Wave is playing gets stop does not get pause. Means if you will play it again after stop then it will play from start not from the previous location.
To demonstrate make a window application. Drag button on form. Press button.
Now write the following code on Button click event:
C#
private void btn_Stop_Click(object sender, EventArgs e)
        {
            obj.Stop();
            MessageBox.Show("Sound Playing stop");
        }      
VB
Private Sub btn_Stop_Click(ByVal sender As Object, ByVal e As EventArgs)
    obj.[Stop]()
    MessageBox.Show("Sound Playing stop")
End Sub
This is simple code to stop the wave file.
Now write the following code on FORM LOAD event:
C#
private void Form1_Load(object sender, EventArgs e)
        {
            this.Text = "Devasp Sound Player application";
        }
VB
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)
    Me.Text = "Devasp Sound Player application"
End Sub
 
This simple article tells that how we can make sound player and can perform different operations using “sound player class” in VB and C# .net 2.0.
Article Comments
Dear sir,

Please help me. I am doing a program. I need to find how to play wav or mp3 in visual basic.
when i click button on the form when ever it starts playing the sound the same time i want to see the time of the played sound in "Text"
Thanks.

Posted on 1/31/2010 4:04:26 AM by David Song

I m B.sc-it student so i want more information about C# with initial info.. thank you!

Posted on 3/20/2010 8:31:36 AM by Bhaveesh kumar

how to do c# codding.

Posted on 4/23/2010 7:22:39 AM by Bhaveesh

dear sir
I am B.C.A. Final year Student so i want more information about play code in vb.net and c# with initial infomation........ thank you

Posted on 6/25/2010 5:13:26 AM by bharat patel

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Sound player (Sound Location, Load Time Out. Load, is load completely) in VB and C# (.net 2.0).Part1

Disclaimer - Privacy
© 2002-2017 DevASP.net