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