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.
Keep the player shared
Section titled “Keep the player shared”Write the loading, error, and retry UI once. Only the source value and its resolver change between platforms:
// 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 />}Web: pass the bundler’s URL
Section titled “Web: pass the bundler’s URL”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.
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} />}Native: resolve the Metro asset ID
Section titled “Native: resolve the Metro asset ID”With Expo, install the audio engine and the asset peer in the app:
npx expo install expo-audio expo-assetKeep this import in the Native entry (for example, Chime.native.tsx).
Only the opt-in /source/expo entry uses Expo Asset.
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} />}Resolution is not playback readiness
Section titled “Resolution is not playback readiness”useMediaSourcereports resolution failures.reload()retries that step; it does not retry playback. Audio errors belong toAudio.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.