> ## Documentation Index
> Fetch the complete documentation index at: https://devdocs.xbox.com/llms.txt
> Use this file to discover all available pages before exploring further.

# XAppCaptureReadLocalStream

> XAppCaptureReadLocalStream

# XAppCaptureReadLocalStream

读取先前调用 [XAppCaptureRecordTimespan](/reference/system/xappcapture/functions/xappcapturerecordtimespan) 生成的 .mp4 文件的内容。

## Syntax

```cpp theme={null}
HRESULT XAppCaptureReadLocalStream(
  XAppCaptureLocalStreamHandle handle,
  size_t startPosition,
  uint32_t bytesToRead,
  uint8_t * buffer,
  uint32_t * bytesWritten
)
```

### Parameters

*handle* \_In\_\
类型：XAppCaptureLocalStreamHandle

先前由 [XAppCaptureRecordTimespan](/reference/system/xappcapture/functions/xappcapturerecordtimespan) 创建的本地录制文件的句柄。

*startPosition* \_In\_\
类型：size\_t

流中的起始位置。

*bytesToRead* \_In\_\
类型：uint32\_t

要读取的字节数。

*buffer* \_Out\_writes\_to\_(bytesToRead, \*bytesWritten)\
类型：uint8\_t \*

用于写入流数据的缓冲区。

*bytesWritten* \_Out\_\
类型：uint32\_t \*

在函数完成时，包含已写入到提供的缓冲区的字节数。

### Return value

类型：HRESULT

函数结果。

## Remarks

同时最多可以存在两个录制。关闭流时，文件会被删除。当游戏退出时，录制会自动删除，以防止过期文件残留。

有关如何获取 [XAppCaptureLocalResult](/reference/system/xappcapture/structs/xappcapturelocalresult)，请参阅 [XAppCaptureRecordTimespan](/reference/system/xappcapture/functions/xappcapturerecordtimespan)。

```cpp theme={null}
const int MAX_DATA = 1024;

XAppCaptureLocalResult localResult = {0};
XAppCaptureLocalStreamHandle localHandle = nullptr;
HANDLE file = INVALID_HANDLE_VALUE;

uint8_t buffer[MAX_DATA];
/* 5 seconds, for example. You should call XAppCaptureGetVideoCaptureSettings() to ensure there's sufficient duration available */
uint64_t durationInMs = 5000;
size_t totalBytesRead = 0;
size_t fileSize = 0;

XAppCaptureRecordTimespan(nullptr, durationInMs, &localResult);

localHandle = localResult.clipHandle;
fileSize = localResult.fileSizeInBytes;

/* T:\ is one example of a writeable local directory. Be aware that the T:\ drive can be invalidated on suspend or resume, and as such it's better to use PLS */ 
file = CreateFileA("T:\\MyFile.mp4", GENERIC_READ | GENERIC_WRITE, 0, 0, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);

while (totalBytesRead < fileSize)
{
    uint32_t bytesRead = 0;
    uint32_t bytesWritten = 0;
    if (SUCCEEDED(XAppCaptureReadLocalStream(localHandle, totalBytesRead, sizeof(buffer), buffer, &bytesRead)))
    {
        WriteFile(file, buffer, bytesRead, &bytesWritten, NULL);

        totalBytesRead += bytesRead;
    }
    else
    {
        break;
    }
}

/* You must always close a local stream handle even if nothing was read in */
XAppCaptureCloseLocalStream(localHandle);

```

## Requirements

**头文件：** XAppCapture.h

**库：** xgameruntime.lib

**受支持的平台：** Windows、XBOX One 系列主机和 XBOX Series 主机

## See also

[XAppCapture members](/reference/system/xappcapture/xappcapture_members)


## Related topics

- [XAppCaptureLocalResult](/zh-CN/reference/system/xappcapture/structs/xappcapturelocalresult.md)
- [XAppCaptureRecordTimespan](/zh-CN/reference/system/xappcapture/functions/xappcapturerecordtimespan.md)
- [XAppCapture](/zh-CN/reference/system/xappcapture/xappcapture_members.md)
- [面向 GDK 的 Unity C# API 包装器](/zh-CN/build/gdk-and-engines/unity/unity-api-wrappers.md)
- [XAppCaptureReadScreenshotStream](/zh-CN/reference/system/xappcapture/functions/xappcapturereadscreenshotstream.md)
