Skip to main content

LensStudio:RemoteMediaModule.js.RemoteMediaModule

Class

Static utility class for loading various types of media from URLs.

Static utility class for loading various types of media from URLs. All methods are asynchronous and return Promises.

Index

Remarks

This module requires:

  • LensStudio:Network - For HTTP requests
  • LensStudio:Ui - For Pixmap and Movie types
  • IStorage from FileSystem module - For file operations (Pixmap/Movie only)

Usage Patterns

Option 1: Initialize storage once (recommended for multiple media loads)

import { RemoteMediaModule } from 'LensStudio:RemoteMediaModule.js';
import { ScopedStorage } from 'LensStudio:ScopedStorage.js';

// Initialize once at startup
RemoteMediaModule.initializeStorage(new ScopedStorage());

// Then load media without passing storage each time
const config = await RemoteMediaModule.loadAsJson('https://api.example.com/config.json');
const pixmap = await RemoteMediaModule.loadAsPixmap(config.imageUrl);
const movie = await RemoteMediaModule.loadAsMovie('https://example.com/video.mp4');

Option 2: Pass storage per call

const storage = new ScopedStorage();
const pixmap = await RemoteMediaModule.loadAsPixmap('https://example.com/image.png', storage);

Constructors

Constructor

new RemoteMediaModule(): RemoteMediaModule

Returns

RemoteMediaModule

Methods

initializeStorage()

static initializeStorage(storage): void

Initializes the storage for the RemoteMediaModule. Call this once to set a default storage for all media loading operations. After initialization, storage parameter becomes optional for loadAsPixmap and loadAsMovie.

Parameters

ParameterTypeDescription
storageIStorageThe storage instance to use as default for media loading

Returns

void

Example

import { RemoteMediaModule } from 'LensStudio:RemoteMediaModule.js';
import { ScopedStorage } from 'LensStudio:ScopedStorage.js';

// Initialize once at startup
RemoteMediaModule.initializeStorage(new ScopedStorage());

// Then use without passing storage
const pixmap = await RemoteMediaModule.loadAsPixmap('https://example.com/image.png');

loadAsBytes()

static loadAsBytes(url): Promise<Uint8Array<ArrayBufferLike>>

Loads content from a URL as a byte array.

Parameters

ParameterTypeDescription
urlstringThe URL to load from

Returns

Promise<Uint8Array<ArrayBufferLike>>

Promise resolving to the response body as a Uint8Array

Throws

Rejects with the HTTP status code if the request fails

Example

const bytes = await RemoteMediaModule.loadAsBytes('https://example.com/file.bin');

loadAsJson()

static loadAsJson(url): Promise<object>

Loads and parses JSON content from a URL.

Parameters

ParameterTypeDescription
urlstringThe URL to load from

Returns

Promise<object>

Promise resolving to the parsed JSON object

Throws

Rejects with the HTTP status code if the request fails, or parsing error if JSON is invalid

Example

const data = await RemoteMediaModule.loadAsJson('https://api.example.com/data.json');
console.log(data.someProperty);

loadAsMovie()

static loadAsMovie(url, storage?): Promise<Movie>

Loads a video from a URL and creates a Movie. The video is downloaded, saved to storage, and then loaded as a Movie.

Parameters

ParameterTypeDescription
urlstringThe URL of the video
storage?IStorageOptional storage instance. If not provided, uses the storage from initializeStorage()

Returns

Promise<Movie>

Promise resolving to a Movie object

Throws

Rejects with the HTTP status code if the request fails, or Error if no storage is available

Example

// Option 1: Pass storage directly
const storage = new ScopedStorage();
const movie = await RemoteMediaModule.loadAsMovie('https://example.com/video.mp4', storage);

// Option 2: Use initialized storage
RemoteMediaModule.initializeStorage(new ScopedStorage());
const movie = await RemoteMediaModule.loadAsMovie('https://example.com/video.mp4');

loadAsPixmap()

static loadAsPixmap(url, storage?): Promise<Pixmap>

Loads an image from a URL and creates a Pixmap. The image is downloaded, saved to storage, and then loaded as a Pixmap.

Parameters

ParameterTypeDescription
urlstringThe URL of the image
storage?IStorageOptional storage instance. If not provided, uses the storage from initializeStorage()

Returns

Promise<Pixmap>

Promise resolving to a Pixmap object

Throws

Rejects with the HTTP status code if the request fails, or Error if no storage is available

Example

// Option 1: Pass storage directly
const storage = new ScopedStorage();
const pixmap = await RemoteMediaModule.loadAsPixmap('https://example.com/image.png', storage);

// Option 2: Use initialized storage
RemoteMediaModule.initializeStorage(new ScopedStorage());
const pixmap = await RemoteMediaModule.loadAsPixmap('https://example.com/image.png');

loadAsString()

static loadAsString(url): Promise<string>

Loads content from a URL as a string.

Parameters

ParameterTypeDescription
urlstringThe URL to load from

Returns

Promise<string>

Promise resolving to the response body as a string

Throws

Rejects with the HTTP status code if the request fails

Example

const text = await RemoteMediaModule.loadAsString('https://example.com/data.txt');
console.log(text);