The Silverlight mediaelement control reads FLV. MP4 videos

Source: Internet
Author: User

 

Recently, I encountered a problem in the project, that is, mediaelemen in Silverlight can only read videos in WMV format, but I cannot read videos in FLV format, later, I found a lot of information on the Internet and learned a lot about it.

Main Code:

  <Grid x:Name="LayoutRoot" Background="White">        <MediaElement Name="MediaRenderer" MediaOpened="MediaRenderer_MediaOpened" MediaFailed="MediaRenderer_MediaFailed" Width="800" Height="480"></MediaElement>        <Button Name="OpenButton" Width="75" Height="23" Content="Open" Click="OpenButton_Click"></Button>    </Grid>

Background:

Private void openbutton_click (Object sender, routedeventargs E) {# region === directly open the stream here and select the file to directly browse the file from the client for playback ====/openfiledialog openfiledialog = new openfiledialog (); // openfiledialog. filter = "file (*. MP4 ;*. FLV) | *. MP4 ;*. FLV "; // openfiledialog. multiselect = false; // If (openfiledialog. showdialog () = true) // {// fileinfo file = openfiledialog. file; // mediastreamsource = NULL; // try // {// If (file. extension. equals (". MP4 ", stringcomparison. ordinalignorecase) // mediastreamsource = new mp4mediastreamsource (file. openread (); // else // mediastreamsource = new flvmediastreamsource (file. openread (); // This. mediarenderer. setsource (mediastreamsource); //} // catch (exception ex) // {// MessageBox. show (ex. message ); ///} //} # endregion # region ==== here, you can directly obtain the video from the server based on the displayed absolute path and view the file playback at the specified file price === getl (); // # endregion} private void getl () {// file address my file address is HTTP published by IIS: // 192.168.3.222/fwtp/1.flv URI fileuri = new uri ("http: // 192.168.3.222/fwtp/1.flv", urikind. absolute); httpwebrequest request = webrequest. create (fileuri) as httpwebrequest; request. method = "get"; request. begingetresponse (New asynccallback (responsecallback), request);} private void responsecallback (iasyncresult result) {// iasyncresult. httpwebrequest request = result. asyncstate as httpwebrequest; webresponse response = NULL; try {// httpwebrequest. endgetresponse (iasyncresult)-end asynchronous request to the specified URI resource // The returned value is webresponse object response = request. endgetresponse (result) as httpwebresponse; stream responsestream = response. getresponsestream (); mediastreamsource = NULL; try {mediastreamsource = new flvmediastreamsource (responsestream); // The implementation thread calls dispatcher. begininvoke (new action <mediastreamsource> (mstream) =>{ this. mediarenderer. setsource (mstream) ;}, mediastreamsource);} catch (exception ex) {MessageBox. show (ex. message) ;}} catch (exception ex ){}}

The above code is mainly implemented in mainpage. Here are two methods for implementation.

1. view the file directly from the client.

2. Play the video from the folder specified by the server.

Playback effect:

The problem of no sound after format conversion has not been solved. If any expert can leave a message for me.

Source code download: http://download.csdn.net/detail/njxiaogui/4828572

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.