Here, I will explain how to store audio and video files in, and how to restore them from, a local database then how to play them in the Windows Media Player. I used a COM component, in other words the built-in media player in Windows. I will explain the following points:
Step 1: Go to "New Project" -> "Windows Forms application" then click on "Ok".
Step 2: Add two Buttons, one ComboBox and an OpenFileDialouge from the Toolbox.
Step 3: Now add a COM Component to the form.
Open the ToolBox then Right-click then choose "Items" -> "COM components" then select "Windows Media Player" then click "Ok". Next it will add something from the ToolBox then drag it from the Toolbox to the form.
Finally your form will look like this.
Your form is now designed. Next is the operations for the buttons. Here the Upload Button saves the file in the local database; see the following code.
Button Save
private void btnSave_Click(object sender, EventArgs e)
{
var file = GetFile();
var lst = new string[] { ".mp3", ".avi", ".mp4", ".wmv" };
if (!lst.Contains(Path.GetExtension(file)))
{
MessageBox.Show("Please select proper file.");
}
else
{
var result = SaveToDataBase(Path.GetFileName(file), GetCompressedData(file, ConvertFileToByteData(file)));
if (result)
{
cmbPlayList.Items.Add(Path.GetFileName(file));
MessageBox.Show("!! File Saved Successfully !!");
}
}
}
Here is the code to save the file in the database:
private bool SaveToDataBase(string fileName, byte[] data)
{
try
{
var ds = new DataSet();
SqlCommand cmd = new SqlCommand("insert into MyPlay values('" + Guid.NewGuid() + "','" + fileName + "',@content)");
SqlParameter param = cmd.Parameters.Add("@content", SqlDbType.VarBinary);
param.Value = data;
cmd.Connection = new SqlConnection(ConnectionString);
cmd.CommandTimeout = 0;
cmd.Connection.Open();
cmd.ExecuteNonQuery();
return true;
}
catch (Exception)
{
throw;
}
return false;
}
The following is the code for GetFile for getting the file from the database and play it:
private void btnPlay_Click(object sender, EventArgs e)
{
if (cmbPlayList.SelectedItem == null)
{
MessageBox.Show("Please select file to play."); return;
}
axWindowsMediaPlayer1.URL = GetFromDataBase(cmbPlayList.SelectedItem.ToString());
axWindowsMediaPlayer1.settings.autoStart = true;
}
private string GetFromDataBase(string fileName)
{
try
{
SqlConnection myConnection = new SqlConnection(ConnectionString);
String Query1 = "SELECT FileData FROM [Test].[dbo].[MyPlay] where FileName = '" + fileName + "'";
SqlDataAdapter adapter = new SqlDataAdapter(Query1, ConnectionString);
DataSet Ds = new DataSet();
adapter.Fill(Ds, "MyPlay");
if (Ds.Tables[0].Rows.Count == 0)
{
MessageBox.Show("No data Found");
return string.Empty;
}
return ConvertByteDataToFile(fileName, GetUnCompressedData((byte[])Ds.Tables[0].Rows[0]["FileData"]));
}
catch (Exception)
{
throw;
}
return string.Empty;
}
How to ues it
- Run the application.
- Your applicaton will look like this.
- Select "File" to save.
- A message "File saved" will pop-up.
- Select from the ComboBox a file to be played.
- The click the "Play" button.
The file will play...
Do the same for a Video File.
Conclusion
In this article we have learned to work with audio and video files and how to play them in a Windows Forms application. I hope this demo will help you to understand some real-time scenario where we can implement COM Components in our application. Please download the attachment for the complete demo. Just change the connection string and restore the database file in your SQL Server.