ARTICLE

Playing Audio and Video Files Using C#

Posted by Prashanth Chindam Articles | Windows Forms C# November 03, 2010
This article shows to develop a windows application for playing audio and video files using c#.net.
Reader Level:
Download Files:
 

Objective:

To develop a windows application for playing audio and video files using c#.net.

Design:

1.gif

Design the form as above with an OpenFileDialog contol, one Button and 'Windows Media Player' contol (COM component).

Note that OpenFileDialog control appears below the form(not on the form), which is used in our application for browsing audio/video files.

Steps for adding 'Windows Media Player' control (COM component) into Toolbox:

By default, 'Windows Media Player' control is not provided in the toolbox, we have to add it into the toolbox if required.

Inorder to add 'Windows Media Player' control into toolbox

Right click on 'General' tab (or anyother tab) in toolbox ->select 'Choose Items...' ->select 'COM Components' tab ->select 'Windows Media Player' ->click on 'OK' button.

2.gif

3.gif
 
'Windows Media Player' control will appear in the toolbox.

4.gif

Now, drag 'Windows Media Player' control on to the form and place a button on it with text as 'Browse..' as shown in the design.

Code:

using System;
using System.Windows.Forms;

namespace mymediaplayer
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void btnBrowse_Click(object sender, EventArgs e)
        {
            openFileDialog1.Filter = "(mp3,wav,mp4,mov,wmv,mpg)|*.mp3;*.wav;*.mp4;*.mov;*.wmv;*.mpg|all files|*.*";
            if(openFileDialog1.ShowDialog()==DialogResult.OK)
                axWindowsMediaPlayer1.URL = openFileDialog1.FileName;
        }
    }
}

Output:

5.gif 

Login to add your contents and source code to this article
post comment
     

Hi All,I am playing videos using axWindowsMediaPlayer in forms. My problem is after playing the video or while playing video we can not enter any text in Textbox. Please let me know how to resolve this issue.Thanks Mani

Posted by manivel p Dec 03, 2012

this is very usefull for me and other learner thanks for posting article

Posted by jitendra prajapat Oct 20, 2012

this code suits only for files with .wmv,.avi extensions.i cant play the files with .mp3,.mp4 extensions.give some information about that.. thanks in advance

Posted by lokesh j Mar 27, 2012

Hi Prashanth Chindam, Thanks for this post, please specify that it will work on web application or not, Thanks.............

Posted by A K Das Mar 26, 2012

hello want to know how can I play a video from a streamreader, and he's normal http bloquiado with basic authentication. part of the code: "String result; HttpWebRequest request = (HttpWebRequest) WebRequest.Create ("http://urldosite/meuvideo.mp4"); request.Credentials = new NetworkCredential ("User", "senha000"); request.PreAuthenticate = true; / / Get response HttpWebResponse response = (HttpWebResponse) request.GetResponse (); StreamReader sr = new StreamReader (response.GetResponseStream ()); result = sr.ReadToEnd (); sr.Close (); " I want to know how to put windows media player to view the video which will result in the variable? example: axwindowsmediaplayer = result;

Posted by igor splynde Mar 05, 2012
COMMENT USING
PREMIUM SPONSORS
Over-C is a holistic consortium of communications and technology specialists. We build, deploy and market both business as well as consumer products and solutions.
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.