Click here to Skip to main content
15,879,326 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have converted my Video file into stream and ran the video in browser using HTML5 Video tag.

Here is code behind file code: StreamVideo.aspx.vb
VB
Imports System.IO

Public Class StreamVideo
    Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    Context.Response.Buffer = False
    Context.Response.ContentType = "video/mp4"
    Dim path As String = Context.Server.MapPath("Resources\small.mp4")
    Dim fileinfo As FileInfo = New FileInfo(path)
    fileinfo.Refresh()
    If fileinfo.Exists Then
        Dim len As Integer = fileinfo.Length
        Dim bytes As Integer
        Context.Response.AppendHeader("content-length", len.ToString())
        Dim buffer(1024) As Byte
        Dim outstream As System.IO.Stream = Context.Response.OutputStream
        Using stream As System.IO.Stream = File.OpenRead(path)
            bytes = stream.Read(buffer, 0, buffer.Length)
            While len > 0 And bytes > 0
                outstream.Write(buffer, 0, bytes)
                bytes = stream.Read(buffer, 0, buffer.Length)
                len -= bytes
            End While
            'context.Response.Write(bytes)
        End Using

    Else
        Context.Response.Write("404: file not found")
    End If
    Context.Response.Flush()
    Context.Response.Close()
    Context.Response.End()
End Sub

End Class


And I have another .aspx page that contains html5 video tag in which src of video tag is set to StreamVideo.aspx which I mentioned above.

HTML
<body>
  <div>
    <video width="400" controls ="controls" id="videoDemo">
        <source src="StreamVideo.aspx" type="video/mp4"/>
            Your browser does not support HTML5 video.
    </video>
  </div>
</body>


The issue is, video run only once. When I set src direct to file path instead of Stream, then video tag works and can be replayed but in case of Stream, it run only once. What changes I need to commit?
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900