從尋找位置傳輸mp4視頻時,我面臨問題。它從頭開始正確流式傳輸。在ASP.NET中尋求位置的流MP4視頻
第一個問題是,雖然mp4視頻是通過jw播放器流媒體播放器。當時間條用戶點擊開始流從視頻中的任何其他部分MP4視頻,JW播放器會發出啓動PARAM隨着時間信息e.g
http://[url]/stream/mp4.ashx?file=Madagascar3-trailer-48861c.mp4&start=53.71
所以jwplayer發送時間間隔,以尋求從MP4流。
我正在使用以下代碼將大約時間間隔轉換爲字節,因爲seek將通過字節開始。
double total_duration = Convert.ToDouble(context.Request.Params["d"]);
double startduration = Convert.ToDouble(context.Request.Params["start"]);
double length_sec = (double)size/total_duration; // total length per second
start = (long)(length_sec * startduration);
下面是完整的示例代碼,我使用,開始從尋求位置e.g 53.71
private void ChunkDownload(string fullpath, HttpContext context)
{
long size, start, end, length, fp = 0;
using (StreamReader reader = new StreamReader(fullpath))
{
size = reader.BaseStream.Length;
start = 0;
end = size - 1;
length = size;
double total_duration = Convert.ToDouble(context.Request.Params["d"]);
double startduration = Convert.ToDouble(context.Request.Params["start"]);
double length_sec = (double)size/total_duration; // total length per second
start = (long)(length_sec * startduration);
context.Response.AddHeader("Accept-Ranges", "0-" + size);
long anotherStart = start;
long anotherEnd = end;
// End bytes can not be larger than $end.
anotherEnd = (anotherEnd > end) ? end : anotherEnd;
// Validate the requested range and return an error if it's not correct.
if (anotherStart > anotherEnd || anotherStart > size - 1 || anotherEnd >= size)
{
context.Response.AddHeader("Content-Range", "bytes " + start + "-" + end + "/" + size);
throw new HttpException(416, "Requested Range Not Satisfiable");
}
start = anotherStart;
end = anotherEnd;
length = end - start + 1; // Calculate new content length
fp = reader.BaseStream.Seek(start, SeekOrigin.Begin);
context.Response.StatusCode = 206;
}
// Notify the client the byte range we'll be outputting
context.Response.AddHeader("Content-Range", "bytes " + start + "-" + end + "/" + size);
context.Response.AddHeader("Content-Length", length.ToString());
context.Response.WriteFile(fullpath, fp, length);
context.Response.End();
}
流,但它未能無論是球員或直接檢查直播網址識別爲有效的MP4流。
任何人都可以幫助我解決這個問題。
我認爲你必須重新發送文件頭,然後請求的位置。無論如何,解碼器無法識別壓縮設置並且將無法處理流。只是我的觀點。 – ppumkin 2012-09-14 12:07:33