Wednesday 11 January 2012

wpf media player code

<Window x:Class="media_Play.Window2"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="" Height="643" Width="484" >
    <StackPanel Background="Black">

        <!-- To interactively stop, pause, and play the media, the LoadedBehavior
           property of the MediaElement must be set to "Manual". -->
       
                            <MediaElement Name="myMediaElement"
     Width="450" Height="250" LoadedBehavior="Manual" UnloadedBehavior="Stop" Stretch="Fill"
     MediaOpened="Element_MediaOpened" MediaEnded="Element_MediaEnded"/>
                      
        <StackPanel HorizontalAlignment="Center" Width="450" Orientation="Horizontal">

            <!-- Play button. -->
            <Image  MouseDown="OnMouseDownPlayMedia" Margin="5" />

            <!-- Pause button. -->
            <Image  MouseDown="OnMouseDownPauseMedia" Margin="5" />

            <!-- Stop button. -->
            <Image MouseDown="OnMouseDownStopMedia" Margin="5" />

            <!-- Volume slider. This slider allows a Volume range between 0 and 1. -->
            <TextBlock Foreground="White" VerticalAlignment="Center" Margin="5"  >Volume</TextBlock>
            <Slider Name="volumeSlider" VerticalAlignment="Center" ValueChanged="ChangeMediaVolume"
       Minimum="0" Maximum="1" Value="0.1" Width="70"/>

            <!-- Volume slider. This slider allows you to change the speed of the media playback. -->
            <TextBlock Foreground="White" Margin="5"  VerticalAlignment="Center">Speed</TextBlock>
            <Slider Name="speedRatioSlider" VerticalAlignment="Center" ValueChanged="ChangeMediaSpeedRatio"
       Value="1" Width="70" />

            <!-- Seek to slider. Ths slider allows you to jump to different parts of the media playback. -->
            <TextBlock Foreground="White" Margin="5"  VerticalAlignment="Center">Seek To</TextBlock>
            <Slider Name="timelineSlider" Margin="5" ValueChanged="SeekToMediaPosition" Width="70"/>
        </StackPanel>
        <StackPanel Width="443" Orientation="Horizontal">
         
            <Label Content="" Height="28" Name="label1" Width="446" FontStyle="Italic" Foreground="Aqua" />
        </StackPanel>
        <StackPanel Width="443" Orientation="Horizontal">
          
            <Button Content="Browse.." Height="24" Name="button1" Width="44" Click="button1_Click" />
            <Button Content="Play" Height="23" Name="button2" Width="75" Click="button2_Click" />
            <Button Content="Pause" Height="23" Name="button3" Width="75" Click="button3_Click" />
        </StackPanel>
        <StackPanel Width="450" Orientation="vertical" Height="301" VerticalAlignment="Top">
            <ListView Margin="1,30,30,1" Name="songList" ItemsSource="{Binding}" Background="Black" Foreground="Snow" BorderBrush="Red" Height="230" Width="441" SelectionChanged="songList_SelectionChanged">
                <ListView.BitmapEffect>
                    <BevelBitmapEffect EdgeProfile="CurvedOut" />
                </ListView.BitmapEffect>
                <ListView.View>
                    <GridView> 
                        <GridViewColumn Width="40.029" Header="Sno" DisplayMemberBinding="{Binding Path=Sno}"></GridViewColumn>
                        <GridViewColumn Width="450.029" Header="Song" DisplayMemberBinding="{Binding Path=Song}"></GridViewColumn>
                    </GridView>
                </ListView.View>
            </ListView>
        </StackPanel>
    </StackPanel>

</Window>
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.Shapes;
using System.IO;
using System.Windows.Forms;
using System.Windows.Navigation;
using System.Data;
using System.Data.SqlClient;




namespace media_Play
{
    /// <summary>
    /// Interaction logic for Window2.xaml
    /// </summary>
    public partial class Window2 : Window
    {
        DataTable songTable;
      
        public Window2()
        {
            InitializeComponent();
             songTable = CreateDataTable();
        }
        OpenFileDialog dlg = new OpenFileDialog();
        void OnMouseDownPlayMedia(object sender, MouseButtonEventArgs args)
        {

            // The Play method will begin the media if it is not currently active or
            // resume media if it is paused. This has no effect if the media is
            // already running.
            myMediaElement.Play();

            // Initialize the MediaElement property values.
            InitializePropertyValues();



        }

        // Pause the media.
        void OnMouseDownPauseMedia(object sender, MouseButtonEventArgs args)
        {

            // The Pause method pauses the media if it is currently running.
            // The Play method can be used to resume.
            myMediaElement.Pause();

        }

        // Stop the media.
        void OnMouseDownStopMedia(object sender, MouseButtonEventArgs args)
        {

            // The Stop method stops and resets the media to be played from
            // the beginning.
            myMediaElement.Stop();

        }

        // Change the volume of the media.
        private void ChangeMediaVolume(object sender, RoutedPropertyChangedEventArgs<double> args)
        {
            myMediaElement.Volume = (double)volumeSlider.Value;
        }

        // Change the speed of the media.
        private void ChangeMediaSpeedRatio(object sender, RoutedPropertyChangedEventArgs<double> args)
        {
            myMediaElement.SpeedRatio = (double)speedRatioSlider.Value;
        }

        // When the media opens, initialize the "Seek To" slider maximum value
        // to the total number of miliseconds in the length of the media clip.
        private void Element_MediaOpened(object sender, EventArgs e)
        {
            timelineSlider.Maximum = myMediaElement.NaturalDuration.TimeSpan.TotalMilliseconds;
        }

        // When the media playback is finished. Stop() the media to seek to media start.
        private void Element_MediaEnded(object sender, EventArgs e)
        {
            myMediaElement.Stop();
        }

        // Jump to different parts of the media (seek to).
        private void SeekToMediaPosition(object sender, RoutedPropertyChangedEventArgs<double> args)
        {
            int SliderValue = (int)timelineSlider.Value;

            // Overloaded constructor takes the arguments days, hours, minutes, seconds, miniseconds.
            // Create a TimeSpan with miliseconds equal to the slider value.
            TimeSpan ts = new TimeSpan(0, 0, 0, 0, SliderValue);
            myMediaElement.Position = ts;
        }

        void InitializePropertyValues()
        {
            // Set the media's starting Volume and SpeedRatio to the current value of the
            // their respective slider controls.
            myMediaElement.Volume = (double)volumeSlider.Value;
            myMediaElement.SpeedRatio = (double)speedRatioSlider.Value;
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                OpenFileDialog dlg = new OpenFileDialog();

                //  dlg.InitialDirectory = "c:\\";

                dlg.Filter = "Media files (*.wmv)|*.wmv|All Files (*.*)|*.*";
                dlg.Multiselect = true;
                dlg.RestoreDirectory = true;
                if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                   string selectedFileName1 = dlg.FileName;
                  
                    string [] selectedFileName = dlg.FileNames;
                    for (int i = 0; i < selectedFileName.Count(); i++)
                    {
                        SOngList(selectedFileName[i]);
                    }

                    label1.Content = selectedFileName1.ToString();
                    //  FileNameLabel.Content = selectedFileName;
                    CurrentPlay(selectedFileName1);
                }
            }
            catch (Exception)
            {

                // throw;
            }


        }
        private DataTable CreateDataTable()
        {//---Create datatable with datatype
            DataTable myDataTable = new DataTable();

            DataColumn myDataColumn;
            ///id//Specification/SpId/SelectBK/QtyGWt/Unit//TotalLess/Netwt
            myDataColumn = new DataColumn();
            myDataColumn.DataType = Type.GetType("System.Int32");
            myDataColumn.ColumnName = "Sno";
            myDataTable.Columns.Add(myDataColumn);

            myDataColumn = new DataColumn();
            myDataColumn.DataType = Type.GetType("System.String");
            myDataColumn.ColumnName = "Song";
            myDataTable.Columns.Add(myDataColumn);
            return myDataTable;
        }
     
        private void SOngList(string Fname)
        {
          //  string Fname = dlg.FileName;
            AddDataToTable(Fname, songTable);
            songList.DataContext = songTable.DefaultView;

        }
        private void AddDataToTable(string Song, DataTable myTable)
        {
            try
            {
                DataRow row;
                row = myTable.NewRow();
                //row["id"] = Guid.NewGuid().ToString();

                row["Sno"] = myTable.Rows.Count + 1;
                row["Song"] = Song;
              
                myTable.Rows.Add(row);
            }
            catch (Exception)
            {

                // throw;
            }
        }
        private void button2_Click(object sender, RoutedEventArgs e)
        {
            myMediaElement.Play();
        }

        private void button3_Click(object sender, RoutedEventArgs e)
        {
            myMediaElement.Pause();
        }

        private void songList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            DataRowView drv = (DataRowView)songList.SelectedItem;
           // string id = drv.Row[0].ToString();
            string CurrentSong = drv.Row[1].ToString();

            CurrentPlay(CurrentSong);
          
        }
        private void CurrentPlay(string CurrentSong)
        {
            myMediaElement.Source = new Uri(CurrentSong, UriKind.RelativeOrAbsolute);
            myMediaElement.Play();
            this.Title = "HD meida Player";
         

    
        }


    }
}


No comments:

Post a Comment