Click here to Skip to main content
11,483,983 members (69,803 online)
Click here to Skip to main content
Add your own
alternative version

An Introduction to the Silverlight samples in the All-In-One Framework

, 12 Dec 2009 Ms-PL 35.4K 2.1K 57
This article introduces several Silverlight samples in the All-In-One Framework.
SilverlightIntroduction_src.zip
CSSL3LocalMessage
Properties
CSSL3MediaElement
CSSL3MediaElement
Properties
CSSL3MediaElement.Web
App_Data
ClientBin
CSSL3MediaElement.xap
Properties
ReadMe.mht
CSSL3PixelShader
cyclewatery.ps
Humpback Whale.jpg
Properties
CSSL3WriteableBitmap
Heart Empty.png
Properties
VBSL3Animation
My Project
VBSL3DeepZoom
VBSL3DeepZoom
images
home_hover.png
home_pressed.png
home_rest.png
My Project
Service References
DeepZoomServiceReference
configuration.svcinfo
configuration91.svcinfo
GenerateDeepZoomService.disco
GenerateDeepZoomService.wsdl
Reference.svcmap
ServiceReferences.ClientConfig
VBSL3DeepZoom.Web
App_Code
App_Data
Bin
DeepZoomTools.DLL
DeepZoomTools.DLL.refresh
ClientBin
GeneratedImages
GenerateDeepZoomService.svc
SourceImages
atom.png
background.jpg
cell.png
dna.png
Molecular.png
neutron.png
seed.png
wrap in bamboo.png
VBSL3HTMLBridge
VBSL3HTMLBridge
My Project
VBSL3HTMLBridge.Web
App_Data
ClientBin
VBSL3HTMLBridge.xap
My Project
Application.myapp
MyExtensions
Settings.settings
VBSL3Input
My Project
VBSL3LocalMessage
My Project
VBSL3MediaElement
ReadMe.mht
VBSL3MediaElement
My Project
VBSL3MediaElement.Web
App_Data
ClientBin
VBSL3MediaElement.xap
My Project
Application.myapp
MyExtensions
Settings.settings
VBSL3PixelShader
cyclewatery.ps
Humpback Whale.jpg
My Project
CSSL3Animation
Properties
CSSL3DeepZoom
CSSL3DeepZoom
images
home_hover.png
home_pressed.png
home_rest.png
Properties
Service References
DeepZoomServiceReference
configuration.svcinfo
configuration91.svcinfo
GenerateDeepZoomService.disco
GenerateDeepZoomService.wsdl
GenerateDeepZoomService1.wsdl
Reference.svcmap
ServiceReferences.ClientConfig
CSSL3DeepZoom.Web
App_Code
Bin
DeepZoomTools.DLL
DeepZoomTools.DLL.refresh
ClientBin
GeneratedImages
GenerateDeepZoomService.svc
SourceImages
atom.png
background.jpg
cell.png
dna.png
Molecular.png
neutron.png
seed.png
wrap in bamboo.png
ReadMe.mht
CSSL3HTMLBridge
CSSL3HTMLBridge
Properties
CSSL3HTMLBridge.Web
App_Data
ClientBin
CSSL3HTMLBridge.xap
Properties
CSSL3Input
Properties
/****************************** Module Header ******************************\
* Module Name:	MainPage.xaml.cs
* Project:		CSSL3MediaElement
* Copyright (c) Microsoft Corporation.
* 
* This example illustrates basic usage of MediaElement.
* 
* This source is subject to the Microsoft Public License.
* See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
* All other rights reserved.
* 
* History:
* * 10/7/2009 05:00 PM Allen Chen Created
\***************************************************************************/


using System;
using System.Collections.Generic;
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.Shapes;
using System.Windows.Threading;

namespace CSSL3MediaElement
{
    public partial class MainPage : UserControl
    {
        DispatcherTimer _timer = new DispatcherTimer();

        public MainPage()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(MainPage_Loaded);
            _timer.Interval = TimeSpan.FromMilliseconds(100);
            _timer.Tick += new EventHandler(_timer_Tick);
        }


        void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            // Attach events of SilverlightHost to subscribe the 
            // FullScreenChanged event.
            App.Current.Host.Content.FullScreenChanged += new EventHandler(Content_FullScreenChanged);
        }


        void Content_FullScreenChanged(object sender, EventArgs e)
        {
            if (!App.Current.Host.Content.IsFullScreen)
            {
                // When shifting back to normal mode, reset width/height of MediaElement.
                this.MyMediaElement.Width = double.NaN;
                this.MyMediaElement.Height = double.NaN;
                // When shifting back to normal mode, hide FullSreenPanel and show MenuPanel
                this.MenuPanel.Visibility = Visibility.Visible;
                this.FullScreenPanel.Visibility = Visibility.Collapsed;
            }
            else
            {
                // In full-screen mode, set width/height of MediaElement to a big value
                this.MyMediaElement.Width = App.Current.Host.Content.ActualWidth;
                this.MyMediaElement.Height = App.Current.Host.Content.ActualHeight;
                // In full-screen mode, hide MenuPanel and show FullSreenPanel
                this.MenuPanel.Visibility = Visibility.Collapsed;
                this.FullScreenPanel.Visibility = Visibility.Visible;
            }
        }

        void _timer_Tick(object sender, EventArgs e)
        {
            if (this.MyMediaElement.CurrentState ==MediaElementState.Playing)
            {
                // Update Status of controls
                TimeSpan ts = this.MyMediaElement.Position;
                this.CurrentPositionTextBlock.Text = string.Format("{0}:{1}:{2}/", ts.Hours, ts.Minutes, ts.Seconds);
                this.CurrentPositionSlider.Value = ts.TotalMilliseconds;
            }
            else if(this.MyMediaElement.CurrentState==MediaElementState.Stopped)
            {
                // Reset status of controls
                this.CurrentPositionTextBlock.Text = "0:0:0/";
                this.CaptionTextBlock.Text = string.Empty;
                this.CurrentPositionSlider.Value = 0;
            }
        }

        private void PlayButton_Click(object sender, RoutedEventArgs e)
        {
            this.MyMediaElement.Play();
            this._timer.Start();
        }

        private void PauseButton_Click(object sender, RoutedEventArgs e)
        {
            this.MyMediaElement.Pause();
        }

        private void StopButton_Click(object sender, RoutedEventArgs e)
        {
            this.MyMediaElement.Stop();
        }

        private void MyMediaElement_CurrentStateChanged(object sender, RoutedEventArgs e)
        {
            // Show current state of MediaElement. Note only 4 states are catched here.
            // You can add more when creating your own media player.
            switch (this.MyMediaElement.CurrentState) 
            {
                case MediaElementState.Stopped: 
                    {
                        this.StatusTextBlock.Text = "Stopped";
                        break;
                    }
                case MediaElementState.Paused: 
                    {
                        this.StatusTextBlock.Text = "Paused";
                        break;
                    }
                case MediaElementState.Playing:
                    {
                        this.StatusTextBlock.Text = "Playing";
                        break;
                    }
                case MediaElementState.Buffering:
                    {
                        this.StatusTextBlock.Text = "Buffering";
                        break;
                    }
            }
        }

        private void MyMediaElement_MediaOpened(object sender, RoutedEventArgs e)
        {
          TimeSpan ts=  this.MyMediaElement.NaturalDuration.TimeSpan;
          this.DurationTextBlock.Text = string.Format("{0}:{1}:{2}",ts.Hours,ts.Minutes,ts.Seconds);
          // Init CurrentPosition Slider
          this.CurrentPositionSlider.Maximum = ts.TotalMilliseconds;
          this.CurrentPositionSlider.Minimum=0;
          // Init VolumeSlider
          this.VolumeSlider.Value = this.MyMediaElement.Volume;
          // Add a marker by code
          this.MyMediaElement.Markers.Add(new TimelineMarker()
          {
              Text = "This marker is added by code!",
              Time = TimeSpan.FromSeconds(8),
              Type = "MyMarker"
          });
          // Enable progress bar
          this.CurrentPositionSlider.IsEnabled = true;
        }

        private void MyMediaElement_MediaEnded(object sender, RoutedEventArgs e)
        {
            this.MyMediaElement.Stop();
        }

        private void HorizontalThumb_DragStarted(object sender, System.Windows.Controls.Primitives.DragStartedEventArgs e)
        {
            this.MyMediaElement.Pause();
        }

        private void HorizontalThumb_DragCompleted(object sender, System.Windows.Controls.Primitives.DragCompletedEventArgs e)
        {
            this.MyMediaElement.Position = TimeSpan.FromMilliseconds(this.CurrentPositionSlider.Value);
            this.MyMediaElement.Play();
        }

        private void MyMediaElement_MarkerReached(object sender, TimelineMarkerRoutedEventArgs e)
        {
            if (e.Marker.Type == "MyMarker")
            {
                // Show marker text made by code
                this.CaptionTextBlock.FontSize = 22;
                this.CaptionTextBlock.Text = e.Marker.Text;
            }

            else
            {
                // Show marker text made by Expression Blend 3
                // Because the caption is "out of" the video you get more flexibility to customizing it
                this.CaptionTextBlock.FontSize = 12;
                this.CaptionTextBlock.Text = e.Marker.Text;
            }
        }

        private void FullScreenButton_Click(object sender, RoutedEventArgs e)
        {   
            // Shift to full-screen mode
            App.Current.Host.Content.IsFullScreen =!App.Current.Host.Content.IsFullScreen;
        }

        private void VolumeSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            this.MyMediaElement.Volume=this.VolumeSlider.Value;
        }

        private void MyMediaElement_MediaFailed(object sender, ExceptionRoutedEventArgs e)
        {
            this.CurrentPositionSlider.IsEnabled = false;
            MessageBox.Show(e.ErrorException.Message);

        }

     

    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)

Share

About the Author

Microsoft All-In-One Code Framework delineates the framework and skeleton of Microsoft development techniques through typical sample codes in three popular programming languages (Visual C#, VB.NET, Visual C++). Each sample is elaborately selected, composed, and documented to demonstrate one frequently-asked, tested or used coding scenario based on our support experience in MSDN newsgroups and forums. If you are a software developer, you can fill the skeleton with blood, muscle and soul. If you are a software tester or a support engineer like us, you may extend the sample codes a little to fit your specific test scenario or refer your customer to this project if the customer's question coincides with what we collected.
http://cfx.codeplex.com/

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150520.1 | Last Updated 12 Dec 2009
Article Copyright 2009 by All-In-One Code Framework
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid