Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET Core| MVC pass audio file to html5 player. Enable seeking

Hi i have problem with html5 player and seeking.

This is my action method:

public IActionResult GetAudio(string url)
    {
        var file = System.IO.File.ReadAllBytes(url);
        long fSize = file.Length;
        long startbyte = 0;
        long endbyte = fSize - 1;
        int statusCode = 200;
        var rangeRequest = Request.Headers["Range"].ToString();

        if (rangeRequest != "")
        {
            string[] range = Request.Headers["Range"].ToString().Split(new char[] { '=', '-' });
            startbyte = Convert.ToInt64(range[1]);
            if (range.Length > 2 && range[2] != "") endbyte = Convert.ToInt64(range[2]);
            if (startbyte != 0 || endbyte != fSize - 1 || range.Length > 2 && range[2] == "")
            { statusCode = 206; }
        }

        long desSize = endbyte - startbyte + 1;
        Response.StatusCode = statusCode;
        Response.ContentType = "audio/wav";
        Response.Headers.Add("Content-Accept", Response.ContentType);
        Response.Headers.Add("Content-Length", desSize.ToString());
        Response.Headers.Add("Content-Range", string.Format("bytes {0}-{1}/{2}", startbyte, endbyte, fSize));

        var stream = new MemoryStream(file, (int)startbyte, (int)desSize);

        return File(stream, Response.ContentType);
    }

Audio player working properly, but i cant seeking throw file. Progres bar also working properly(moving). When i try to seek audio stop for a while and continue playing with the same moment.

Probably i must add so more Headers right?

like image 424
Cezary Rynkowski Avatar asked Mar 13 '23 21:03

Cezary Rynkowski


1 Answers

Ok, this:

Response.Headers.Add("Accept-Ranges", "bytes");
Response.Headers.Remove("Cache-Control");

Resolved my problem.

like image 77
Cezary Rynkowski Avatar answered Mar 23 '23 09:03

Cezary Rynkowski