Click here to Skip to main content
14,299,986 members
Rate this:
Please Sign up or sign in to vote.
Hi,

I want to have a media player in my application.

It should play audio and video files.
I have already created one.

But it does not play audio or video.
It complies perfectly and also executes,but I get no output.
Can any one help me? I am also posting my code.
Tell me what changes I need to incorporate in my current project so that it works fine.


Further in some blogs I have found that I require to have media player 10 and above.
I have MP 11 installed on my system. But still it is not giving me any output.
Thanks in advance.
C# Coding

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()
        {
            InitializeComponent();
            IsPlaying(false);
        }
        #endregion



    //****************************************** 
    #region IsPlaying(bool)
	private void IsPlaying(bool bValue)
	{
	btnStop.IsEnabled = bValue;
    btnMoveBackward.IsEnabled = bValue;
	btnMoveForward.IsEnabled = bValue;
    btnPlay.IsEnabled = bValue;
    }
    #endregion
    //***************************************
    
    #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;
	    }
	}
	#endregion

    //***************************************

      private void btnPlay_Click(object sender, RoutedEventArgs e)
      {
        IsPlaying(true);
        if (btnPlay.Content.ToString() == "Play")
	    {
	        MediaEL.Play();
	        btnPlay.Content = "Pause";
	    }
	    else
	    {
	        MediaEL.Pause();
	        btnPlay.Content = "Play";
	    }
     }
    
    //***************************************
    #region Stop
	private void btnStop_Click(object sender, RoutedEventArgs e)
	{
	    MediaEL.Stop();
	    btnPlay.Content = "Play";
	    IsPlaying(false);
	    btnPlay.IsEnabled = true;
	}
	#endregion
    //***************************************

        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);
        }


    }
}



XAML Coding
<pre lang="xml"><Window x:Class="MediaPlayer.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="226*" />
            <RowDefinition Height="36*" />
        </Grid.RowDefinitions>
        <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"/>
        </StackPanel>
    </Grid>
</Window

>

Posted
Updated 2-Apr-11 2:01am
v2
Comments
Dalek Dave 2-Apr-11 8:01am
   
Edited for Readability

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

Hi aditya I found same question there-CPAnswer[^] in CP having good solution. you can navigate this link[^] which also suggest in suggested answer. it will cover you step by step procedure to create Media player in C# and WPF.
   
Comments
Dalek Dave 2-Apr-11 8:01am
   
Good Call
aditya behara 2-Apr-11 9:19am
   
Thats true sir.. I have also seen your answer. Further I have also downloaded the project from the url have given. Unfortunately I still do not get any audio or video as output.
Tarun.K.S 3-Apr-11 13:54pm
   
Check if Windows Media Player 9.0 or higher is installed in your machine or not.
aditya behara 4-Apr-11 6:36am
   
I have media player 11 installed on my system. I have Windows XP as operating System and I am using .net Platform 3.5 with VS 2008. Yet the problem persists. Cn you help me ?

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100