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

# my.createVideoContext

> Create a context to control a video component.

Creates and returns a `videoContext` object for the `<video>` component whose `id` is passed in. Use it to control video playback.

## Before You Begin

* The GriverSDK version must be at least 2.48.0; earlier versions can crash the app.
* The Mini Program core library (Appx) must be version 2.0.
* CSS animations are not supported.
* Supported format: MP4 with H.264 encoding (iOS and Android).

## Parameters

| Property | Type | Required | Description |
| - | - | - | - |
| videoId | String | Yes | The `id` attribute of the `<video>` component |

## videoContext Methods

| Method | Description |
| - | - |
| play() | Play the video |
| pause() | Pause the video |
| stop() | Stop the video |
| seek(position) | Jump to a position, in seconds |
| requestFullScreen({ direction }) | Enter full screen. `direction`: `0` (vertical), `90` (rotated 90° anticlockwise), `-90` (rotated 90° clockwise) |
| exitFullScreen() | Exit full screen |
| mute(muted) | Mute or unmute |

## video Component Attributes

| Attribute | Type | Description |
| - | - | - |
| id | String | Component ID used by `my.createVideoContext` |
| src | String | Video URL (HTTPS only) |
| poster | String | Poster image URL. Default: the first frame of the video |
| poster-size | String | Poster sizing when its aspect ratio differs from the video (same rules as CSS `background-size`). Default: `contain` |
| initial-time | String | Start position, in seconds |
| controls | Boolean | Show the default playback controls. Default: `true` |
| autoplay | Boolean | Play automatically. Default: `false` |
| direction | Number | Full-screen direction: `0`, `90`, `-90`. Default: based on the video's aspect ratio |
| loop | Boolean | Loop the video. Default: `false` |
| muted | Boolean | Play muted. Default: `false` |
| onPlay | EventHandler | Video starts or resumes playing |
| onPause | EventHandler | Video pauses |
| onEnded | EventHandler | Video finishes playing |
| onTimeUpdate | EventHandler | Playback progress: `event.detail = { currentTime, userPlayDuration, videoDuration }` |
| onLoading | EventHandler | Video is loading |
| onError | EventHandler | Playback error (see Error Codes) |
| onFullScreenChange | EventHandler | Full-screen mode entered or exited: `event.detail = { fullScreen, direction }` |
| onUserAction | EventHandler | User interaction: `event.detail = { tag, value }`, `tag` is `play`, `centerplay`, `mute` or `fullscreen` |
| onStop | EventHandler | Video stops |
| onRenderStart | EventHandler | Video loaded and starts rendering |

## Error Codes

| Code | Description | Action | Android | iOS |
| - | - | - | - | - |
| 1 | Unknown exception | Try again | ✅ | ✅ |
| 1002 | Player system error | Check the player system | ✅ | ✅ |
| 1005 | Network error | Check the network | ✅ | — |
| 1006 | Metadata error | Check the metadata | ✅ | ✅ |
| 1007 | Player initialization error | Initialize the player again | ✅ | — |
| 3001 | Audio rendering error | Check the audio resource | ✅ | — |
| 3002 | Hardware decoding error | Check hardware decoding | ✅ | — |

## Code Example

```xml theme={null}
<video id="myVideo" src="{{src}}" controls="{{true}}" onPlay="onPlay" onError="onError"></video>
<button onTap="play">Play</button>
<button onTap="seek">Seek to 15 s</button>
```

```javascript theme={null}
Page({
  data: { src: 'https://example.com/video.mp4' },
  onLoad() {
    this.videoContext = my.createVideoContext('myVideo');
  },
  play() {
    this.videoContext.play();
  },
  seek() {
    this.videoContext.seek(15);
  },
  onPlay() {
    console.log('video: onPlay');
  },
  onError(e) {
    console.log('video error', JSON.stringify(e));
  }
});
```

## FAQ

> [accordion] Is a watched video loaded again?
>
> If the video loops, it is played from the cache; otherwise it is loaded again. The cache is cleared when the page or the Mini Program is closed.

> [accordion] How do I get the video length?
>
> Read `videoDuration` from the `onTimeUpdate` event.

> [accordion] The video context doesn't work or the video is misplaced
>
> Make sure the `<video>` has a unique `id` made only of letters, `-` and `_`, that is not too long and is not a reserved word (e.g. `content-root`, `RenderView`). Avoid IDs that are prefixes of other IDs (e.g. `main-video` and `main-video-wrapper`). If the component is positioned on the wrong layer, try the `raw-controls` property.

## Related APIs

> [card] [my.chooseVideo](https://docs.rebellapp.com/jsapi/media/video/my.chooseVideo.md)
>
> Choose or record a video


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.