【问题标题】:How to set duration video when streaming in ASP.NET Core?在 ASP.NET Core 中流式传输时如何设置持续时间视频?
【发布时间】:2018-12-22 03:29:45
【问题描述】:

在 ASP.NET Core 中流式传输视频时遇到问题

我的视频时长:1:04 当我流式传输此视频时,我想向用户显示 26 秒(40% - 视频长度)

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;
            }

outputstream 通过 Response.Body 返回流

var stream = context.HttpContext.Response.Body;

但是当响应 html video 标签时,它总是显示持续时间 1:04。我想将此视频的持续时间设置为 26 秒 (40%)

请帮帮我!!!

【问题讨论】:

    标签: html asp.net-core video-streaming html5-video


    【解决方案1】:

    改用此解决方案,这将流式传输 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);
        }
    }
    

    查看

    <audio controls="controls" autoplay="autoplay">
        <source src="/api/Streamer/Stream" type="audio/mpeg">
    </audio>
    

    Sample Code

    【讨论】:

    • 我之前使用 MemoryStream 尝试过这种方式,但结果相同。因为我需要将视频的持续时间设置为 0:26(1:04 的 40%),而现在,它总是在进度条结束时显示 1:04
    • @user3836200 我在回答时得到了与该代码相同的结果,您可以使用我上面发送的代码重试吗?
    • 是的,我会尝试上面的代码,但不会更改结果。这里的结果uphinhnhanh.com/images/2018/12/23/… 总是在显示 1:04 :(
    • @user3836200 好的,我在原始答案中添加了示例代码;)
    • @user3836200 这是我的结果,原始音频持续时间是 2:043 秒,但是使用上面的代码,我只流式传输了 1:04 的 40%。删除您的浏览器历史记录或尝试使用其他浏览器查看结果。 imgur.com/a/nnUFZjB
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-22
    • 2021-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多