c# Stream.ReadAsync 读取零字节,尽管未到达流的末尾

c# Stream.ReadAsync reads zero bytes, although the end of the stream was not reached

我正在尝试使用 http 发送 mp4 流,但每次我的循环在第一次迭代期间中断,因为 ReadAsync returns 0。我不明白为什么,因为我检查流是否为空当然不是。

这是我目前得到的:

while (camera.VideoStream.AsStream().Length == 0);

byte[] buffer = new byte[1024];
while (true)
{
    int bytesRead = await camera.VideoStream.AsStream().ReadAsync(buffer,0,buffer.Length);
    if (bytesRead == 0)
    {
        Debug.WriteLine("End of Stream");
        break;
    }
    await output.WriteAsync(buffer, 0, bytesRead);
    await output.FlushAsync();
}

camera.VideoStream 是一个 IRandomAccessStream。

感谢您的帮助。

当您尝试阅读时,流的位置是什么?

您可能需要.Seek(0, SeekOrigin.Begin).