> ## 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](/zh-TW/reference/system/xappcapture/functions/xappcapturerecordtimespan) 所產生之 .mp4 檔案的內容。

## 語法

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

### 參數

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

先前由 [XAppCaptureRecordTimespan](/zh-TW/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 \*

函式完成時，包含寫入所提供緩衝區的位元組數。

### 傳回值

類型：HRESULT

函式結果。

## 備註

同時最多只能存在兩個錄製。串流關閉時，檔案會被刪除。遊戲結束時，將會自動刪除錄製，以避免留下過時的檔案。

如需如何取得 [XAppCaptureLocalResult](/zh-TW/reference/system/xappcapture/structs/xappcapturelocalresult)，請參閱 [XAppCaptureRecordTimespan](/zh-TW/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);

```

## 需求

**標頭：** XAppCapture.h

**程式庫：** xgameruntime.lib

**支援的平台：** Windows、XBOX One 系列主機和 XBOX Series 主機

## 另請參閱

[XAppCapture 成員](/zh-TW/reference/system/xappcapture/xappcapture_members)


## Related topics

- [XAppCaptureReadLocalStream](/reference/system/xappcapture/functions/xappcapturereadlocalstream.md)
- [XAppCaptureRecordTimespan](/reference/system/xappcapture/functions/xappcapturerecordtimespan.md)
- [XAppCaptureLocalResult](/reference/system/xappcapture/structs/xappcapturelocalresult.md)
