api.video TypeScript Media Recorder
api.video is the video infrastructure for product builders. Lightning fast video APIs for integrating, scaling, and managing on-demand & low latency live streaming features in your app.
Project description
Typescript library to easily upload data from a MediaStream to api.video. It can be used to upload a video to api.video from the user's webcam with ease, as well as from a screen recording.
Getting started
Installation
Installation method #1: requirejs
If you use requirejs you can add the library as a dependency to your project with
You can then use the library in your script:
var = ;
var = new;
Installation method #2: typescript
If you use Typescript you can add the library as a dependency to your project with
You can then use the library in your script:
;
Simple include in a javascript project
Include the library in your HTML file like so:
...
Then, once the window.onload
event has been trigered, create your player using new ApiVideoMediaRecorder()
:
...
Documentation
Instantiation
Options
The media recorder object is instantiated using a MediaStream and an options
object. Options to provide depend on the way you want to authenticate to the API: either using a delegated upload token (recommanded), or using a usual access token.
Using a delegated upload token (recommended):
Using delegated upload tokens for authentication is best options when uploading from the client side. To know more about delegated upload token, read the dedicated article on api.video's blog: Delegated Uploads.
Option name | Mandatory | Type | Description |
---|---|---|---|
uploadToken | yes | string | your upload token |
videoId | no | string | id of an existing video |
common options (see bellow) |
Using an access token (discouraged):
Warning: be aware that exposing your access token client-side can lead to huge security issues. Use this method only if you know what you're doing :).
Option name | Mandatory | Type | Description |
---|---|---|---|
accessToken | yes | string | your access token |
videoId | yes | string | id of an existing video |
common options (see bellow) |
Common options
Option name | Mandatory | Type | Description |
---|---|---|---|
apiHost | no | string | api.video host (default: ws.api.video) |
retries | no | number | number of retries when an API call fails (default: 5) |
videoName | no | string | the name of your recorded video (overrides the default "file" name) |
Example
const = new;
Methods
start(options?: { timeslice?: number })
The start() method starts the upload of the content retrieved from the MediaStream. It takes an optionnal options
parameter.
Options
Option name | Mandatory | Type | Description |
---|---|---|---|
timeslice | no (default: 5000) | number | The number of milliseconds to record into each Blob. |
Example
// ... mediaRecorder instantiation
;
// or, with a 2 seconds timeslice:
// mediaRecorder.start({ timeslice: 2000 });
stop(): Promise<VideoUploadResponse>
The start() method stops the media recording. It upload the last part of content retrieved from the MediaStream (this will start the aggregation of the video parts on the api.video side). It takes no parameter. It returns a Promise that resolves with the newly created video.
addEventListener(event: string, listener: Function)
Define an event listener for the media recorder. The following events are available:
"error"
: when an error occurs"recordingStopped"
: when the recording is stopped"videoPlayable"
: when the video is playable
Example
// ... mediaRecorder instantiation
;
getMediaRecorderState(): RecordingState
Return the state of the underlaying MediaRecorder. The state can be one of the following: inactive
, paused
, recording
.
Example
// ... mediaRecorder instantiation
.;
Full example
start recording
stop recording
Video link: will be displayed when the recording is finished
Was this page helpful?