A simple Silverlight Video Player

Source: Internet
Author: User
Tags silverlight

The following is a simple Video Player:

1. Add a mediaelement to the Xmal page to accommodate video files.

2. Drag and Drop a slider to display the video playback progress in real time.

3. A textblock used to display the current playback time value.

4. A textblock is used to display the total video time.

5. These buttons are used for playback, pause, continue, stop, and full screen operations.

6. a checkbox is used to control the mute operation.

7. A slider used to control the media volume.

 

The following is the file code:

<Usercontrol <br/> xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation" <br/> xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml" <br/> X: class = "videopaly. mainpage "<br/> width =" 640 "Height =" 480 "> <br/> <grid X: name = "layoutroot" background = "gray"> <br/> <canvas margin = "34,23, 38,63"> <br/> <mediaelement X: name = "Media" Source = "http://download.microsoft.com/download/2/0/5/205d8c39-3d55-4032-8195-7b0e6eda4cb6/WinVideo-SL-InstallExperience.wmv" Height = "248" width = "542" canvas. left = "10" canvas. top = "8" bufferingprogresschanged = "media_bufferingprogresschanged" loaded = "media_loaded"/> <br/> <slider X: name = "slider1" Height = "21" width = "550" canvas. left = "10" canvas. top = "260" valuechanged = "slider_valuechanged" Maximum = "10" smallchange = "1"/> <br/> <button X: name = "btnplay" Height = "25" width = "57" canvas. left = "151" canvas. top = "324" content = "play" Click = "btnplay_click"/> <br/> <button X: name = "btnpause" Height = "25" width = "57" canvas. left = "212" canvas. top = "324" content = "pause" Click = "btnpause_click"/> <br/> <button X: name = "btnresume" Height = "25" width = "57" canvas. left = "273" canvas. top = "324" content = "resume" Click = "btnresume_click"/> <br/> <button X: name = "btnstop" Height = "25" width = "57" canvas. left = "334" canvas. top = "324" content = "stop" Click = "btnstop_click"/> <br/> <textblock X: name = "tbcurrenttime" Height = "25" width = "90" canvas. left = "22" canvas. top = "281" textwrapping = "Wrap"/> <br/> <textblock X: Name = "tbtotaltime" Height = "25" width = "91" canvas. left = "505" canvas. top = "281" textwrapping = "Wrap"/> <br/> <checkbox X: Name = "cbsilent" canvas. left = "526" canvas. top = "369" content = "mute" rendertransformorigin = "0.881, 0.235" Checked = "cbsilent_checked" unchecked = "cbsilent_unchecked"/> <br/> <textblock X: name = "tbbufferingdvalue" Height = "55" width = "171" canvas. left = "213" canvas. top = "111" textwrapping = "Wrap" fontsize = "18.667"/> <br/> <slider X: name = "slidervolumn" Height = "74" width = "36" canvas. left = "486" canvas. top = "312" orientation = "vertical" valuechanged = "slidervolumn_valuechanged" Maximum = "1" largechange = "0.2"/> <br/> <button X: name = "btnfullscreen" Height = "25" width = "57" canvas. left = "395" canvas. top = "324" content = "fullscreen" Click = "btnfullscreen_click"/> <br/> </canvas> <br/> </GRID> <br/> </usercontrol>
 

Using system; <br/> using system. windows; <br/> using system. windows. controls; <br/> using system. windows. documents; <br/> using system. windows. ink; <br/> using system. windows. input; <br/> using system. windows. media; <br/> using system. windows. media. animation; <br/> using system. windows. shapes; <br/> using system. windows. threading; <br/> namespace videopaly <br/>{< br/> Public partial class mainpage: usercon Trol <br/> {<br/> // use a timer object to update the playback progress. <br/> private dispatchertimer timer = new dispatchertimer (); <br/> // media duration <br/> private timespan duration; </P> <p> Public mainpage () <br/>{< br/> // required for initialization variables <br/> initializecomponent (); <br/>}< br/> private void btnplay_click (Object sender, system. windows. routedeventargs e) <br/>{< br/> // play <br/> media. play (); <br/>}< br/> private void btnpause_click (Object sender, S Ystem. windows. routedeventargs e) <br/>{< br/> // pause <br/> media. pause (); <br/>}< br/> private void btnresume_click (Object sender, system. windows. routedeventargs e) <br/>{< br/> // continue <br/> media. play (); <br/>}< br/> private void btnstop_click (Object sender, system. windows. routedeventargs e) <br/>{< br/> // stop <br/> media. stop (); <br/>}< br/> private void cbsilent_checked (Object sender, system. windows. routed Eventargs e) <br/>{< br/> // mute <br/> media. ismuted = true; <br/>}< br/> private void cbsilent_unchecked (Object sender, system. windows. routedeventargs e) <br/>{< br/> // do not mute <br/> media. ismuted = false; <br/>}< br/> private void slider_valuechanged (Object sender, system. windows. routedpropertychangedeventargs <double> E) <br/>{< br/> // determines whether the playback position of the multimedia object is manageable <br/> If (media. canseek) <br/>{< br/> // reset Medi Aelement playback position <br/> media. position = timespan. fromseconds (media. naturalduration. timespan. totalseconds * slider1.value)/10); <br/>}< br/> private void media_bufferingprogresschanged (Object sender, system. windows. routedeventargs e) <br/>{< br/> // video buffer event <br/> double bufferingvalue = This. media. bufferingprogress * 100; <br/> This. tbbufferingdvalue. TEXT = "buffer:" + bufferingvalue. tostring () + "% "; <Br/> If (this. tbbufferingdvalue. TEXT = "buffer: 100%") <br/>{< br/> // hide the buffer value <br/> This. tbbufferingdvalue. visibility = visibility. collapsed; <br/>}< br/> private void media_loaded (Object sender, system. windows. routedeventargs e) <br/>{< br/> // set the time interval caused by the event <br/> timer. interval = timespan. frommilliseconds (500); <br/> // timer object event <br/> timer. tick + = new eventhandler (timer_tick); <br/> // start timing <br/> time R. start (); </P> <p> // set the volume <br/> media. volume = 0.8; <br/> slidervolumn. value = 0.8; </P> <p >}< br/> private void timer_tick (Object sender, system. eventargs e) <br/>{< br/> // valid when playing <br/> If (this. media. currentstate = mediaelementstate. playing) <br/>{< br/> duration = media. naturalduration. hastimespan? Media. naturalduration. timespan: timespan. frommilliseconds (0); <br/> This. tbcurrenttime. TEXT = string. format ("{0 }:{ 1 }:{ 2}", media. position. hours <10? "0" + media. position. Hours. tostring (): media. position. Hours. tostring (), media. position. Minutes <10? "0" + media. position. Minutes. tostring (): media. position. Minutes. tostring (), media. position. Seconds <10? "0" + media. position. seconds. tostring (): media. position. seconds. tostring (); <br/> This. tbtotaltime. TEXT = string. format (<br/> "{0} {}:{}", "Duration:", <br/> duration. hours, <br/> duration. minutes, <br/> duration. seconds); <br/> // skip the event handler <br/> slider1.valuechanged-= new routedpropertychangedeventhandler <double> (slider_valuechanged ); <br/> // calculate and set the slider percentage <br/> slider1.value = (MEDI A. position. totalseconds/media. naturalduration. timespan. totalseconds) * 10; <br/> // redeclare the valuechanged event <br/> slider1.valuechanged + = new routedpropertychangedeventhandler <double> (slider_valuechanged ); <br/>}< br/> private void slidervolumn_valuechanged (Object sender, system. windows. routedpropertychangedeventargs <double> E) <br/> {<br/> // media. volume = slidervolumn. value; <B R/>}< br/> private void btnfullscreen_click (Object sender, system. windows. routedeventargs e) <br/>{< br/> // full screen operation <br/> application. current. host. content. isfullscreen =! Application. Current. Host. content. isfullscreen; <br/>}< br/>}

The above code can be copied and pasted and directly run. However, because the video file is downloaded from Microsoft, the video is invisible offline.

 

 

Run the following command:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.