LensStudio:RemoteMediaModule.js.RemoteMediaModule
ClassStatic 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 requestsLensStudio:Ui- For Pixmap and Movie typesIStoragefrom 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()
staticinitializeStorage(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
| Parameter | Type | Description |
|---|---|---|
storage | IStorage | The 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()
staticloadAsBytes(url):Promise<Uint8Array<ArrayBufferLike>>
Loads content from a URL as a byte array.
Parameters
| Parameter | Type | Description |
|---|---|---|
url | string | The 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()
staticloadAsJson(url):Promise<object>
Loads and parses JSON content from a URL.
Parameters
| Parameter | Type | Description |
|---|---|---|
url | string | The 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()
staticloadAsMovie(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
| Parameter | Type | Description |
|---|---|---|
url | string | The URL of the video |
storage? | IStorage | Optional 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()
staticloadAsPixmap(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
| Parameter | Type | Description |
|---|---|---|
url | string | The URL of the image |
storage? | IStorage | Optional 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()
staticloadAsString(url):Promise<string>
Loads content from a URL as a string.
Parameters
| Parameter | Type | Description |
|---|---|---|
url | string | The 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);