This is a program sample that you should go through at the intermediate level of your knowledge about the .Net platform by Microsoft. The entire program is written in C#, you may also use VB.NET if you are familiar with it. The logic behind the program is same, just coding syntax will vary. Well, all the best! let’s start with a quick glance at our objective :
i. We shall construct a media player.
ii. It shall be capable of playing media files of any kind, say a music file of .mp3 or .ogg format, a picture of .jpg or .png format as well as a video file of .wmv or .avi format.
iii. We shall be able to add files, play them, pause them and stop them.
For, our purpose we will use WPF(Windows Presentation Foundation) standard form in Visual Studio 2010 edition.
STEPS:
1. Create New Project.
2. Choose WPF, give a project name and add it. A workspace is created with a default Form loaded with the XAML code engraved in it.
3. Open toolbox and choose rather drag and drop a media element on the workspace.
4. Add 4 buttons name them as ‘PLAY’,’PAUSE’,STOP’,’ADD MEDIA’ respectively and a textbox to display the filename being played. A screenshot is given below:
Screenshot of basic layout of WPF form. |
5. Double-click on ‘ADD MEDIA’ button and it shall take you to the MainWindow.xaml.cs file where we write our code. Now, a default method is called as we are double clicking the ‘button4’(just an index), that is, button4_click. Now I shall write the entire code.
using system; using system.LINQ; using System.Text; using System.Windows.Forms; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace mediaplayer { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void button4_Click(object sender, RoutedEventArgs e) { OpenFileDialog ofd = new OpenFileDialog(); ofd.AddExtension = true; ofd.DefaultExt = "*.*"; ofd.Filter = "Media(*.*)|*.*"; ofd.ShowDialog(); mediaElement1.MediaOpened += new RoutedEventHandler(mediaElement1_MediaOpened); mediaElement1.Source = new Uri(ofd.FileName); } private void button1_Click(object sender, RoutedEventArgs e) { mediaElement1.Play(); } private void button3_Click(object sender, RoutedEventArgs e) { mediaElement1.Stop(); } private void button2_Click(object sender, RoutedEventArgs e) { mediaElement1.Pause(); } void mediaElement1_MediaOpened(object sender, RoutedEventArgs e) { label1.Content = mediaElement1.Source.ToString(); } } } 6. Press CTRL+F5 for the code to run. And there you go! Click on ‘Add Media’ button and choose media and watch it play! Enjoy! Have Fun! This link might be useful for you: http://www.youtube.com/watch?v=f_bBWHCoKpk
Thanks for your code! but it faces with an error >> “Can’t load media unless LoadBehaviour or UnloadedBehaviour is set to manual”
How shall I handle the “Unloaded” event that it play the video ?
In the properties window of the media element you have a property called ‘loadedBehavior’ kindly select it as MANUAL.
THANK YOU.
The type or namespace name ‘OpenFileDialog’ could not be found (are you missing a using directive or an assembly reference?) C:\Users\user\Documents\Visual Studio 2008\Projects\mediaplayer\mediaplayer\Window1.xaml.cs this the error i encountered after turning the loadedbehavior to manual
Use these references:
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
Else the OpenFileDialog won’t work.
Just right click on the OpenFileDialog from the popup goto – resolve–>using Microsoft.Win32; select it , this will add the reference of OpenFileDialog.
Hope this will work for you.
using Microsoft.Win32 will allow you to use OpenFileDialogue
Follow this: http://stackoverflow.com/a/2276355/2245646
mediaElement1.MediaOpened += new RoutedEventHandler(mediaElemen
In the Above Line U are asking for an EVENT but, i can’t find any.
And, Loaded Behavior is not in action.
// What To doo..
That’ll be “mediaElement1.MediaOpened += new RoutedEventHandler(mediaElement1)”. Set Loaded Behaviour to “manual” in properties for MediaElement.
Do watch the video also.
The type or namespace name ‘Forms’ does not exist in the namespace ‘System.Windows’ (are you missing an assembly reference?
Please do mention ‘using System.Windows.Forms’ as an assembly reference right at the beginning of the program.
Error 2 The type or namespace name ‘OpenFileDialog’ could not be found (are you missing a using directive or an assembly reference?) c:\users\hafiz\documents\visual studio 2012\Projects\WpfMEDIA\WpfMEDIA\MainWindow.xaml.cs 53 38 WpfMEDIA
what sall i do ?
Use these references:
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
Else the OpenFileDialog won’t work.
Error 1 The end of the text block was not found. The character data must be included in an element
I don’t get your error @govind. Kindly send me your code , preferably screenshot at my e-mail : abhishekde@hotmail.com
In Win XP he same code doesnot work .Is there any specific video file format we have to give.I am not getting any kind of error but some how its not loading the video.
Are you sure that you’re using .NET 4.0? And it loads all formats for which you have a suitable codec for. Maybe .mkv or .mov will be a problem in XP but else it’d run absolutely fine.
Yes I am using .net 4.0.I tried with .3gp format video also.the problem is still the same.
Try with a .avi file format and let me know.
I tried with .avi format it still doenot work.
Update your system codecs, specially MPEG 4 plugins from http://free-codecs.com/.
Alternatively, you try the code on a Linux system using Monodevelop or on another Windows PC.
I am not able to play mjpg and some of .mpg video.Is there any limitation?
Actually there should not be any restrictions. But, if you are facing a problem, and keeping in mind if you’re using Microsoft XP, please tell me the error.
MediaElement1.MediaOpend+=new RoutedEventhandler(MediaElement1_MediaOpend);
In above line”MediaElement_MediaOpened” does not exist in the current context
What i doooo??????
the particular event ‘media_opened’ has been deleted from the .cs file but the media element 1 still has it. so, go to xaml view and click on media element 1 and delete the mediaopened event call from there.
When the application is running the Play Pause and Stop button is not working 😦 What will i do??????
private void button1_Click(object sender, RoutedEventArgs e)
{
mediaElement1.LoadedBehavior = MediaState.Play;
}
private void btnPause_Click(object sender, RoutedEventArgs e)
{
mediaElement1.LoadedBehavior = MediaState.Pause;
}
private void btnStop_Click(object sender, RoutedEventArgs e)
{
mediaElement1.LoadedBehavior = MediaState.Stop;
}
what is passed to the RoutedEventHandler Constructor?
The event: mediaElement1_MediaOpened is passed as an argument in the constructor.
please help me some error is display for this line
mediaElement1.MediaOpened += new RoutedEventHandler(mediaElement1.MediaOpened);
ERROR:
Error 1 The event ‘System.Windows.Controls.MediaElement.MediaOpened’ can only appear on the left hand side of += or -=
please help me how to solve the problem……..
Error 1 The type or namespace name ‘OpenFileDialog’ could not be found (are you missing a using directive or an assembly reference?)
wt i do?
what features can i add to it using .net ?
You can add a whole lot of rendering capabilities and features using Direct 3D and .net
Add a meadia player butto4 is could not fount a ‘openFileDialog’ qury please help me just pleas
Can you please explain with a screenshot? It’d be easier to understand.
hi,
my media player play media files well,thanks for share your knowledge with world,
but in this code you use play(),pause() & stop() methods,that is not working….
media files play but can’t respond on play,pause and stop button,
plz check what is missing?
thanks for this post…Awesome
This was just a skeleton tutorial. Try finding msdn reference for pause() and other methods. It helps you build your skills as well. Thanks.☺
private void button1_Click(object sender, RoutedEventArgs e)
{
mediaElement1.LoadedBehavior = MediaState.Play;
}
private void btnPause_Click(object sender, RoutedEventArgs e)
{
mediaElement1.LoadedBehavior = MediaState.Pause;
}
private void btnStop_Click(object sender, RoutedEventArgs e)
{
mediaElement1.LoadedBehavior = MediaState.Stop;
}
easy
media element plays some videos with distortion, has some kind of green layering over it. The same video works fine in windows media player. Any documentation you can point me towards?
You can try DIrectX or Direct3D rendering as per your video adapter capabilities. A good head start will be from here: http://www.codeproject.com/Articles/32493/Play-your-AVI-files-with-this-DirectX-video-player
I need to show only the name of the music or video playing, in the textbox and when playing a .mp3 file i need to play a visualizer effect in the media element what can I do to accomplish these
Hi this is anjireddy nice article….. i want more information about video player Like Screenshot of basic layout of WPF form. more screenshots ..if it possible give me please.
I can’t get my app to load Media 😦
What is the exact error code? Post a screenshot.