Tuesday, 13 March 2012

how to run a dynamically vedio in sliver light

design the page :
<UserControl x:Class="SilverlightApplication10.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="800">

    <Grid x:Name="LayoutRoot" Background="White" Width="900" HorizontalAlignment="Center">
        <StackPanel Orientation="Vertical">

            <MediaElement Height="249" HorizontalAlignment="Left" Margin="10,10,0,0" Name="mediaElement1" VerticalAlignment="Top" Width="378" />
            <StackPanel Orientation="Horizontal">
                <Button Content="Open" Height="23"  Name="button1"  Width="75" Click="button1_Click" />
                <Button Content="Play" Height="23"  Name="button6"  Width="75" Click="button6_Click" />
                <Button Content="Push" Height="23" Name="button2" Width="75" Click="button2_Click" />
                <Button Content="Stop" Height="23" Name="button3" Width="75" Click="button3_Click" />
                <Button Content="Vol(+)" Height="23" Name="button4" Width="75" Click="button4_Click" />
                <Button Content="Vol(-)" Height="23" Name="button5" Width="75" Click="button5_Click" />
            </StackPanel>
        </StackPanel>
       
       
    </Grid>
</UserControl>
page looks like
Code :
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;


namespace SilverlightApplication10
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
        }
        int i = 0;
        FileInfo fn;
        Stream s;
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog fileDialog = new OpenFileDialog();
            fileDialog.Filter = "Media WMV|*.wmv";
            if (fileDialog.ShowDialog() == true)
            {
                fn=fileDialog.File;
                s=fn.OpenRead();
            }
        }

        private void button4_Click(object sender, RoutedEventArgs e)
        {
            mediaElement1.Volume = 1;
        }

        private void button5_Click(object sender, RoutedEventArgs e)
        {
            mediaElement1.Volume = 0;
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            if (i == 1)
                mediaElement1.Pause();
        }

        private void button3_Click(object sender, RoutedEventArgs e)
        {
            i = 2;
            mediaElement1.Stop();
        }

        private void button6_Click(object sender, RoutedEventArgs e)
        {
            mediaElement1.SetSource(s);
            mediaElement1.AutoPlay = true;
            i = 1;
        }
    }
}



No comments:

Post a Comment