Play video songs, audio songs and other sound files in Asp.Net

In my previous article (Custom Controls in Asp.Net) I explain about how to create Custom Controls in Asp.Net.

In this article I am explaining about how to play sound files, audio songs, video songs and other sound files in .Net and Asp.Net.

To create Window media player control for Asp.Net or .Net
Open Visual studio Tool File-> New -> Project
Then select Class Library in the right side panel, mention folder name and Press Ok And change class name to MediaPlayer.

Before proceeding into the code, add reference to System.Web.
Right click on Solution Explorer and select Add Reference System.Web, press ok.

The code to create custom control with javascript look like below.



Imports System Imports System.Web.UI Imports System.Web.UI.WebControls Imports System.ComponentModel Imports System.Text Public Class MediaPlayer Inherits System.Web.UI.Control #Region "properties" Dim file_name As String 'Declare and set default values for width,height and auto_start Dim width_px As Integer = 100 Dim height_px As Integer = 100 Dim auto_start As Boolean = True Public Property Filename() As String Get Return file_name End Get Set(ByVal value As String) file_name = value End Set End Property Public Property Width() As Integer Get Return width_px End Get Set(ByVal value As Integer) width_px = value End Set End Property Public Property height() As Integer Get Return height_px End Get Set(ByVal value As Integer) height_px = value End Set End Property Public Property AutoStart() As Boolean Get Return auto_start End Get Set(ByVal value As Boolean) auto_start = value End Set End Property #End Region Public Overrides Sub RenderControl(ByVal writer As System.Web.UI.HtmlTextWriter) Dim sb_control As New StringBuilder sb_control.Append("<OBJECT ID='" & Me.ClientID & "'codebase= 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0' CLASSID='CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6' >") sb_control.Append(" height='") sb_control.Append(height_px & "' ") sb_control.Append(" width='") sb_control.Append(width_px & "' ") sb_control.Append("<PARAM name='URL' value='") sb_control.Append(file_name) sb_control.Append("'>") sb_control.Append("<PARAM name='AutoStart' value='") sb_control.Append(AutoStart & "'>") sb_control.Append("<PARAM name='volume' value='100'>") sb_control.Append("<embed src='") sb_control.Append(file_name & "' ") sb_control.Append("type='application/x-ms-wmp'") sb_control.Append(" height='") sb_control.Append(height_px & "' ") sb_control.Append(" width='") sb_control.Append(width_px & "' ") sb_control.Append("autostart='") sb_control.Append(AutoStart & "' ") sb_control.Append("volume='100'") sb_control.Append(">") writer.Write(sb_control.ToString) End Sub End Class

In the above code you observe, you can observe that we defined the three properties like file name, width and height for the media player.

To change control prefix, you need to register this control in AssemblyInfo.vb class. You can find this class under My Project Open AssemblyInfo.vb class and mention tageprefix for the control based on namespace.

          <Assembly: system.Web.UI.TagPrefix("WindowsMediaControl", "ASPMediaControl")>
 


Here I mention control prefix as ASPMediaControl for WindowsMediaControl namespace.

Build the project. For that Right click on SolutionExplorer and select Build option. Now your code is compiled into DLL file and your control is ready to use.

To test this custom control create new website and open ToolBox, on General Tab Right click and select Choose Items. Browse to our custom control DLL file and add that dll file to our ToolBox.

Now the Control is added to our ToolBox. You can Drag and drop that control to source code. Using the Custom control code look like below.

    
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %> <%@ Register Assembly="WindowsMediaControl" Namespace="WindowsMediaControl" TagPrefix="ASPMediaControl" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head id="Head2" runat="server"> <title>Asp.Net website with Media Control</title> </head> <body> <form id="form1" runat="server"> <div> <ASPMediaControl:MediaPlayer AutoStart="False" Width="200" height="200" Filename="sound1.wav" ID="MediaPlayer1" runat="server"> </ASPMediaControl:MediaPlayer> </div> </form> </body> </html>

After executeing and you can get the window media player on the page width given file.


To Create Window Media Control Download source code here

To Use Window Media Control Download source code here