|
Sound player (Sound Location,Load Time Out. Load) in VB and C# |
|
In this tutorial you will learn that how we can make sound player and can perform different operations using "sound player class" in VB and C# .net.
|
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(); |
|
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.Net |
|
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.Net |
|
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.Net |
|
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
|
|
|
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.Net |
|
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.Net |
|
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
|
|
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.Net
|
|
Private Sub btn_Load_Click(ByVal sender As Object,ByVal e As EventArgs)
obj.Load()
MessageBox.Show("File Loaded")
End Sub
|