ASP. net mvc program to play H.264 videos,
Before this article, Insus. NET whether in ASP. NET or ASP. net mvc to achieve a lot of video playback, you can refer to this link: http://www.cnblogs.com/insus/category/465053.html.
This time, Insus. NET wants to play a video in H.264 format in ASP. net mvc, such as MP4 or MOV. The following two types of video files are presented respectively.
Create a Result class:
The core part above is to convert the file to binary output. Of course, there is another way to convert to binary. You can refer:
Hdotaskresult class code:
Public class hdotaskresult: ActionResult {public string FilePhysicalPath {private get; set;} public override void ExecuteResult (ControllerContext context) {var file = new FileInfo (FilePhysicalPath); if (file. exists) {context. httpContext. response. addHeader ("Content-Disposition", "attachment; filename =" + file. name); // var stream = file. openRead (); // var bytes = new byte [stream. length]; // stream. read (bytes, 0, (int) file. length); // context. httpContext. response. binaryWrite (bytes); FileStream fs = new FileStream (FilePhysicalPath, FileMode. open, FileAccess. read); BinaryReader br = new BinaryReader (fs); Byte [] bytes = br. readBytes (Int32) fs. length); br. close (); fs. close (); context. httpContext. response. outputStream. write (bytes, 0, bytes. length );}}}View Code
Prepare an mov video for Demonstration:
OK. I need to create a controller in the MVC program. Today is July, and a July15Controller is created. All the exercises in this month are stored in this controller.
In the controller, we have created two Action operations, one for the view and the other for the display video.
Next, create the View:
To see the effect:
Next, let's try out the video "pai.mp4:
The Controller slightly modifies the path:
The view does not need to be modified. Run the browser to see the effect:
Because the video is large, the loading time is longer.