Skip to content

Bundled media assets

Audio and Video take a URI string. A Web bundler can supply one directly. Metro’s require('./chime.wav') returns a numeric asset ID instead: passing that number or converting it to a string does not give the player a URI.

Write the loading, error, and retry UI once. Only the source value and its resolver change between platforms:

tsx
// BundledTrack.tsximport { Button, Text } from '@hozo/core'import { Audio } from '@hozo/media/audio'import { type MediaSourceResolver, useMediaSource } from '@hozo/media/source'export function BundledTrack<T>({  source,  resolveSource,}: {  source: T  resolveSource: MediaSourceResolver<T>}) {  const asset = useMediaSource({ source, resolveSource })  if (asset.error) {    return (      <>        <Text>{asset.error.message}</Text>        <Button onPress={asset.reload}>Retry loading audio</Button>      </>    )  }  if (!asset.uri) return <Text>Loading audio…</Text>  return <Audio src={asset.uri} accessibilityLabel="Notification chime" controls />}

For example, in Vite import the asset as a URL. resolveMediaURI validates and returns that string; it does not fetch the file or check that it can be decoded.

tsx
import { resolveMediaURI } from '@hozo/media/source'import { BundledTrack } from './BundledTrack'import chimeUrl from './chime.wav?url'export function Chime() {  return <BundledTrack source={chimeUrl} resolveSource={resolveMediaURI} />}

With Expo, install the audio engine and the asset peer in the app:

sh
npx expo install expo-audio expo-asset

Keep this import in the Native entry (for example, Chime.native.tsx). Only the opt-in /source/expo entry uses Expo Asset.

tsx
import { resolveExpoMediaAsset } from '@hozo/media/source/expo'import { BundledTrack } from './BundledTrack'const chimeAsset: number = require('./chime.wav')export function Chime() {  return <BundledTrack source={chimeAsset} resolveSource={resolveExpoMediaAsset} />}
  • useMediaSource reports resolution failures. reload() retries that step; it does not retry playback. Audio errors belong to Audio.onError.
  • Changing the source clears the old URI before the next one resolves. Pass a stable resolver so an unrelated render does not restart resolution.
  • Caching and offline availability depend on the bundler, the Expo asset system, and the application’s policy. A resolved URI is not a guarantee of offline playback or codec support.

Return to Video & Audio for playback controls, status, and captions.