Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 然后播放mp4(流)下载流 .NET Core 3.1/WebAPI(ControllerBase)_C#_Amazon S3_Asp.net Web Api - Fatal编程技术网

C# 然后播放mp4(流)下载流 .NET Core 3.1/WebAPI(ControllerBase)

C# 然后播放mp4(流)下载流 .NET Core 3.1/WebAPI(ControllerBase),c#,amazon-s3,asp.net-web-api,C#,Amazon S3,Asp.net Web Api,我正在尝试从我们的S3 AWS bucket流式传输MP4(我们不能使用预先签名的url) 我的代码: public async Task Stream([FromRoute] int id) { var response = CaseArtifactService.StreamArtifactTest(new CaseArtifactRequest() { CaseArtifactId = id }

我正在尝试从我们的S3 AWS bucket流式传输MP4(我们不能使用预先签名的url)

我的代码:

        public async Task Stream([FromRoute] int id)
    {
        var response = CaseArtifactService.StreamArtifactTest(new CaseArtifactRequest()
        {
            CaseArtifactId = id
        });

        this.Response.StatusCode = 200;

        this.Response.Headers.Add(HeaderNames.ContentType,
            response.Result.Key.ToLower().EndsWith(".png") ? ContentType.Png : ContentType.VideoMp4);
        this.Response.Headers.Add(HeaderNames.ContentDisposition, "inline");

        this.Response.Headers.Add(HeaderNames.AcceptRanges, "bytes");

        var inputStream = response.Result.ResponseStream;
        
        await using var outputStream = this.Response.Body;

        const int bufferSize = 1 << 10;
        var buffer = new byte[bufferSize];

        try
        {
            this.Response.Headers.Add(HeaderNames.ContentLength, inputStream.Length.ToString());
            this.Response.Headers.Add(HeaderNames.ContentRange,
                $"{0}-{inputStream.Length - 1}/{inputStream.Length}");
            Debug.WriteLine($"File Can Seek: {inputStream.CanSeek}");
            
        }
        catch
        {
            // ignored
        }
        finally
        {
            while (true)
            {
                var bytesRead = await inputStream.ReadAsync(buffer, 0, bufferSize);

                if (bytesRead == 0) break;
                await outputStream.WriteAsync(buffer, 0, bytesRead);
            }
        }
    }
public异步任务流([FromRoute]int-id)
{
var response=CaseArtifactService.StreamArtifactTest(新的CaseArtifactRequest()
{
caseArtfactid=id
});
this.Response.StatusCode=200;
this.Response.Headers.Add(HeaderNames.ContentType,
response.Result.Key.ToLower().EndsWith(“.png”)?ContentType.png:ContentType.VideoMp4);
this.Response.Headers.Add(HeaderNames.ContentDisposition,“inline”);
this.Response.Headers.Add(HeaderNames.AcceptRanges,“字节”);
var inputStream=response.Result.ResponseStream;
使用var outputStream=this.Response.Body等待;

const int bufferSize=1更新:我现在可以搜索,但它仍然可以下载,然后流式播放。如果你不需要实时视频,最好将其转换为m3u8或mpeg Dash。如果你需要实时视频,你需要自己的播放器,websocketsAmazon也可以流式播放视频。也许我没有正确解释,我们有一个UI前端wi它有自己的视频播放器。我正在尝试从S3流到S3流的更大的mp4,然后交付。与其等待500+mb的下载,我希望它立即启动(ish)。然后你需要使用206部分内容