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 (Sound Location, Load Time Out. Load, is load completely) in VB and C# (.net 2.0).Part1

Author: Zunnair
Download Source Code : 919_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.

 

Sound player:
Sound player class provides us a facility to make our sound player but it supports wav files. Means we can play only wav files by using this class.
Object creation:
To create an object of sound class write following code.
C#
 System.Media.SoundPlayer obj = new System.Media.SoundPlayer();
 
VB
 Dim obj As New System.Media.SoundPlayer()
You can also give us an overloaded constructor to pass the path of wave file which you want to play.
Write following code.
C#
 System.Media.SoundPlayer obj = new System.Media.SoundPlayer(“path”);
 
VB
 Dim obj As New System.Media.SoundPlayer(“path”)
Sound Location:
If you do not want to give the path using overloaded constructor then sound player class provides us a property named sound location. You can get or set the path of your wav file which you want to play. Sound location property takes and returns string.
To demonstrate make a window application. Drag text box and one button on form. Write the path in text box and press button to save it.
Now write the following code on Button click event:
Set:
C#
private void btn_savePath_Click(object sender, EventArgs e)
        {
            if (txt_Path.Text != string.Empty)
            {
                string str;
                str = txt_Path.Text;
                obj.SoundLocation = txt_Path.Text;
                MessageBox.Show("Path saved");
            }
            else
                MessageBox.Show("No Path Given");
        }
VB
Private Sub btn_savePath_Click(ByVal sender As Object, ByVal e As EventArgs)
        If txt_Path.Text != String.Empty Then
            Dim str As String
            str = txt_Path.Text
            obj.SoundLocation = txt_Path.Text
            MessageBox.Show("Path saved")
        Else
            MessageBox.Show("No Path Given")
        End If
    End Sub
This simple code sets the path of wav file.
Get:
C#
private void btn_getPath_Click(object sender, EventArgs e)
        {
            string str;
            str = obj.SoundLocation;
            MessageBox.Show("The path is " + str);
        }
VB
Private Sub btn_getPath_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim str As String
        str = obj.SoundLocation
        MessageBox.Show("The path is " & str)
    End Sub
This simple code gets the path of wav file.
You can also provide online path of wav file by writing url.
Load Time Out:
If you want to set or get load time of file for this purpose we have a property named load time out. This property takes and returns integer. This property is used to set load time out means after that time duration file will not load or loading will get stop. It takes or returns milliseconds.
To demonstrate make a window application. Drag text box and one button on form. Write time in text box and press button to save it.
Now write the following code on Button click event:
Set:
C#
private void btn_LoadTime_Click(object sender, EventArgs e)
        {
            if (txt_LoadTime.Text != string.Empty)
            {
                string str;
                str = txt_LoadTime.Text;
                int a;
                a = Convert.ToInt16(str);
                obj.LoadTimeout=a;
            }
            else
                MessageBox.Show("enter value");
        }
VB
Private Sub btn_LoadTime_Click(ByVal sender As Object, ByVal e As EventArgs)
    If txt_LoadTime.Text <> String.Empty Then
        Dim str As String
        str = txt_LoadTime.Text
        Dim a As Integer
        a = Convert.ToInt16(str)
        obj.LoadTimeout = a
    Else
        MessageBox.Show("enter value")
    End If
End Sub
This simple code sets the load time.
Get:
C#
private void tbn_GetLoad_Click(object sender, EventArgs e)
        {
            int a;
            a = obj.LoadTimeout;
            string str;
            str = a.ToString();
            MessageBox.Show(str);
        }
VB
Private Sub tbn_GetLoad_Click(ByVal sender As Object, ByVal e As EventArgs)
    Dim a As Integer
    a = obj.LoadTimeout
    Dim str As String
    str = a.ToString()
    MessageBox.Show(str)
End Sub
This simple code gets the load time of wav file.
Load:
Load is a function which sound player class provides us to load the file. To play the file it is necessary to load it then play otherwise it will not play. Load function does not return or take anything.
To demonstrate make a window application. Drag button on form. Press button to load.
Now write the following code on Button click event:
C#
private void btn_Load_Click(object sender, EventArgs e)
        {
            obj.Load();
            MessageBox.Show("File Loaded");
        }
VB
Private Sub btn_Load_Click(ByVal sender As Object, ByVal e As EventArgs)
    obj.Load()
    MessageBox.Show("File Loaded")
End Sub
This is simple code to load the file.
Is Load completed:
 Sound player class provides us a property named is load completely. This property returns a Boolean value which tells that wav file is completely loaded or not. If it returns true means file loaded else not loaded.
To demonstrate make a window application. Drag button on form. Press button to load.
Now write the following code on Button click event:
C#
private void btn_Load_Click(object sender, EventArgs e)
        {
            obj.Load();
            if(obj.IsLoadCompleted(
            MessageBox.Show("File Loaded");
            else
                MessageBox.Show("file not loaded");
        }
VB
Private Sub btn_Load_Click(ByVal sender As Object, ByVal e As EventArgs)
    obj.Load()
    If obj.IsLoadCompleted Then
        MessageBox.Show("File Loaded")
    Else
        MessageBox.Show("file not loaded")
    End If
End Sub
This is simple code to load the file and check file is loaded or not.
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.
   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Stack operations (clear, count, to array) in VB and C#.

Disclaimer - Privacy
© 2002-2017 DevASP.net