This capacitor plugin enables playing audio natively from a remote url, on web, android and ios.
import { PlayAudioFromUrl } from 'capacitor-play-audio-from-url'
PlayAudioFromUrl
.play({ url: 'https://www.soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3' })
.then(() => console.log('Audio played'))
.catch((error) => console.error('Error playing audio (e.g. failed to load)', error));
// or
try {
await PlayAudioFromUrl.play({url: 'https://www.soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3'})
console.log('Audio played')
}catch (error) {
console.error('Error playing audio (e.g. failed to load)', error)
}
npm install capacitor-play-audio-from-url
npx cap sync
play(options: PlayOptions) => Promise<void>
Plays an audio file from a given URL. In web mode, to avoid getting audio play rejected by mobile browsers when playing many audios, the audio sources are cached in an LRU cache with 256 entries.
Param | Type | Description |
---|---|---|
options |
PlayOptions |
- The options for playing the audio. Must include a url property with the URL of the audio file. |
Prop | Type |
---|---|
url |
string |
To test, it is best to link it to a project using
npm install <path-to-the-project>
Then lots of npx cap sync
, npx cap copy
and building the apps for good measure.:)