Click here to Skip to main content
14,971,553 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
I am creating a an application in WPF and want to have a media player that plays audio and video files. I have created one. But the problem is that it compiles and executes properly but does not give me audio or video output.
I am also sending you the XAML and C# coding of it. Please help me if possible.
Thanks in advance...

XAML Coding:

<pre lang="xml"><Window x:Class="MediaPlayer.MainWindow"
        Title="MainWindow" Height="350" Width="525">
            <RowDefinition Height="226*" />
            <RowDefinition Height="36*" />
        <MediaElement x:Name="MediaEL" Grid.RowSpan="1" LoadedBehavior="Manual" UnloadedBehavior="Close" />
        <StackPanel Orientation="Horizontal" Grid.Row="1" HorizontalAlignment="Center">
            <Button x:Name="btnPlay" Content="Play" Click="btnPlay_Click"
                    Width="50" Height="25"/>
            <Button x:Name="btnStop" Content="Stop" Click="btnStop_Click"
                        Width="50" Height="25"/>
            <Button x:Name="btnMoveBackward" Content="Back" Click="btnMoveBackward_Click"
                        Width="50" Height="25"/>
            <Button x:Name="btnMoveForward" Content="Forward" Click="btnMoveForward_Click"
                    Width="50" Height="25"/>
            <Button x:Name="btnOpen" Content="Open" Click="btnOpen_Click"
                    Width="50" Height="25"/>


C# Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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;
using System.Windows.Forms;

namespace MediaPlayer
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
   public partial class MainWindow : Window
        #region Constructor
        public MainWindow()

    #region IsPlaying(bool)
	private void IsPlaying(bool bValue)
	btnStop.IsEnabled = bValue;
    btnMoveBackward.IsEnabled = bValue;
	btnMoveForward.IsEnabled = bValue;
    btnPlay.IsEnabled = bValue;
    #region Open Media
	private void btnOpen_Click(object sender, RoutedEventArgs e)
	    OpenFileDialog ofd = new OpenFileDialog();
        ofd.Filter = "Audio and Video (*.mp3,*.wav,*.mpeg,*.wmv,*.avi)|*.mp3;*.wav;*.mpeg;*.wmv;*.avi"; // Filter files by extension
	    if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
	        MediaEL.Source = new Uri(ofd.FileName);
            btnPlay.IsEnabled = true;


      private void btnPlay_Click(object sender, RoutedEventArgs e)
        if (btnPlay.Content.ToString() == "Play")
	        btnPlay.Content = "Pause";
	        btnPlay.Content = "Play";
    #region Stop
	private void btnStop_Click(object sender, RoutedEventArgs e)
	    btnPlay.Content = "Play";
	    btnPlay.IsEnabled = true;

        private void btnMoveBackward_Click(object sender, RoutedEventArgs e)
            MediaEL.Position = MediaEL.Position + TimeSpan.FromSeconds(10);

        private void btnMoveForward_Click(object sender, RoutedEventArgs e)
            MediaEL.Position = MediaEL.Position - TimeSpan.FromSeconds(10);



Further, after reading certain blogs, I have learnt that one has to set the loaded behavior property of the media element as manual. I have also incorporated these changes in it. But still it did not reap me any fruits. Just go through this code and guide me in this concern.

1 solution

There is a blog here which explained how to create a Media Player in WPF :[^]

Do check out its second part.

Also after having a quick review of the code that you provided,
Add these events too :
1) MediaOpened : to set the isPlaying to true
2) MediaEnded : to stop the MediaElement by providing MediaEL.stop()
3) Set the LoadBehavior to Manual
4) UnloadedBehavior to Stop

The blog has the complete solution, check out its second part too :[^]

All the best.
Nice answer, my 5.

Tarun, how come? It's 1st of April today; and you probably did not see my 1st of April post yet?!

Come on, have fun!
See this (with my "Asnwer" of course):

Cheers! :-)
RaviRanjanKr 2-Apr-11 7:25am
Nice answer Tarun! links are to good.
have a my 5. :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900