我正在尝试创建一个包含 HTML5 VIDEO 标签的测试页面,该标签将允许播放转换后的视频。我能够成功地转换视频并将它们本地存储在服务器上,但我希望能够通过另一个 .aspx 页面流式传输所有视频。
假设我有一个 player.aspx 页面,其中包含 HTML 代码和 getvideo.aspx 页面,除了提供视频二进制文件之外什么都不做,我认为以下代码在我的 player.aspx 页面中可以正常工作:
<div style="text-align:center">
<video controls autoplay id="video1" width="920">
<source src="http://www.mywebsite.com/getvideo.aspx?getvideo=1" type="video/mp4">
Your browser does not support HTML5 video.
</video>
getvideo.aspx 页面包含以下 vb.net 代码:
Response.clearheaders
Response.AddHeader("Content-Type", "video/mp4")
Response.AddHeader("Content-Disposition", "inline;filename=""newvideo.mp4""")
dim Err as string = ""
Dim iStream As System.IO.Stream
' Buffer to read 10K bytes in chunk:
Dim buffer(buffersize) As Byte
' Length of the file:
Dim length As Integer
' Total bytes to read:
Dim dataToRead As Long
' Identify the file to download including its path.
Dim filepath As String = "./outout/videos/newvideo.mp4"
' Identify the file name.
Dim filename As String = System.IO.Path.GetFileName(filepath)
' Open the file.
try
iStream = New System.IO.FileStream(filepath, System.IO.FileMode.Open, IO.FileAccess.Read, IO.FileShare.Read)
catch ex as exception
throw new exception("Could not create FileStream for [" & filepath & "], error follows." & vbcrlf & ex.toString)
end try
Try
' Total bytes to read:
dataToRead = iStream.Length
' Read the bytes.
While dataToRead > 0
' Verify that the client is connected.
If system.web.httpcontext.current.Response.IsClientConnected Then
' Read the data in buffer
length = iStream.Read(buffer, 0, buffersize)
' Write the data to the current output stream.
system.web.httpcontext.current.Response.OutputStream.Write(buffer, 0, length)
' Flush the data to the HTML output.
system.web.httpcontext.current.Response.Flush()
ReDim buffer(buffersize) ' Clear the buffer
dataToRead = dataToRead - length
Else
'prevent infinite loop if user disconnects
dataToRead = -1
End If
End While
Catch ex As Exception
' Trap the error, if any.
err = "Error accessing " & filepath & " : " & ex.tostring
Finally
If IsNothing(iStream) = False Then
' Close the file.
iStream.Close()
End If
End Try
if err<>"" then throw new exception( err )
我在页面输出上得到的只是一个 HTML 视频播放器(chrome 的基本播放器),它似乎超时并且“播放”按钮变灰。Chrome 开发者工具中的网络工具显示它正在下载 45mb 并获得 200 响应代码。这向我表明它工作正常。尽管我收到了第二个状态为“已取消”的 GET 请求?
如果我访问 www.mywebsite.com/output/videos/myvideo.mp4,那么它可以在浏览器中正常播放,所以我知道 IIS 已配置为正确流式传输视频。
此外,如果我将响应内容配置更改为“附件”,则浏览器在转到我的 ASPX 页面时会正确强制下载视频,但这也无法在 HTML 播放器上正确播放。HTML5 VIDEO 标签是否有一些“聪明”的东西正在阻止 .aspx 文件通过 .net 提供视频?还是我缺少响应标头?
谢谢!