Как установить длительность видео при потоковой передаче в ASP.NET Core? - PullRequest
0 голосов
/ 22 декабря 2018

У меня проблема при потоковой передаче видео в ASP.NET Core

Мое видео имеет продолжительность: 1:04 При потоковой передаче этого видео я хочу показать пользователю 26 секунд (40% - длинаvideo)

using (var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite, 65536, FileOptions.Asynchronous | FileOptions.SequentialScan))
            {
                int totalSize = (int)(fileStream.Length*0.4);
                //here we are saying read bytes from file as long as total size of file 

                //is greater then 0
                while (totalSize > 0)
                {
                    int count = totalSize > bufferSize ? bufferSize : totalSize;
                    //here we are reading the buffer from orginal file  
                    int sizeOfReadedBuffer = fileStream.Read(buffer, 0, count);
                    //here we are writing the readed buffer to output//  
                    await outputStream.WriteAsync(buffer, 0, sizeOfReadedBuffer);
                    //and finally after writing to output stream decrementing it to total size of file.  
                    totalSize -= sizeOfReadedBuffer;
                }
                //outputStream.Position = 0;
            }

outputtream возвращает поток с помощью Response.Body

var stream = context.HttpContext.Response.Body;

Но при ответе на HTML-тег видео всегда отображается продолжительность 1:04.Я хочу установить продолжительность этого видео на 26 секунд (40%)

Пожалуйста, помогите мне !!!

1 Ответ

0 голосов
/ 22 декабря 2018

Используйте вместо этого решения, оно будет передавать 40% вашего видео / аудио:

Контроллер API :

[ApiController]
[Route("api/[controller]")]
public class StreamerController : ControllerBase
{
    const string Filename = @"C:\PathToYourAudio\Stream.mp3";

    [HttpGet("Stream")]
    public IActionResult Stream()
    {
        byte[] fileData;

        using (FileStream fs = System.IO.File.OpenRead(Filename))
        {
            using (BinaryReader binaryReader = new BinaryReader(fs))
            {
                fileData = binaryReader.ReadBytes((int)(fs.Length * 0.4));
            }
        }

        MemoryStream stream = new MemoryStream(fileData);
        return new FileStreamResult(stream, new MediaTypeHeaderValue("audio/mpeg").MediaType);
    }
}

Просмотр

1011

Код образца

...