6.4.23 β€’ Published 8 months ago

@capgo/native-audio v6.4.23

Weekly downloads
-
License
MIT
Repository
github
Last release
8 months ago

Native audio

Capacitor Native Audio Plugin

Capacitor plugin for native audio engine. Capacitor V6 - βœ… Support!

Click on video to see example πŸ’₯

YouTube Example

Maintainers

MaintainerGitHubSocial
Martin DonadieuriderxTelegram

Mainteinance Status: Actively Maintained

Preparation

All audio files must be with the rest of your source files.

First make your sound file end up in your builded code folder, example in folder BUILDFOLDER/assets/sounds/FILENAME.mp3 Then use it in preload like that assets/sounds/FILENAME.mp3

Installation

To use npm

npm install @capgo/native-audio

To use yarn

yarn add @capgo/native-audio

Sync native files

npx cap sync

On iOS, Android and Web, no further steps are needed.

Configuration

No configuration required for this plugin.

Supported methods

NameAndroidiOSWeb
configureβœ…βœ…βŒ
preloadβœ…βœ…βœ…
playβœ…βœ…βœ…
pauseβœ…βœ…βœ…
resumeβœ…βœ…βœ…
loopβœ…βœ…βœ…
stopβœ…βœ…βœ…
unloadβœ…βœ…βœ…
setVolumeβœ…βœ…βœ…
getDurationβœ…βœ…βœ…
getCurrentTimeβœ…βœ…βœ…
isPlayingβœ…βœ…βœ…

Usage

Example repository

import {NativeAudio} from '@capgo/native-audio'


/**
 * This method will load more optimized audio files for background into memory.
 * @param assetPath - relative path of the file, absolute url (file://) or remote url (https://)
 *        assetId - unique identifier of the file
 *        audioChannelNum - number of audio channels
 *        isUrl - pass true if assetPath is a `file://` url
 * @returns void
 */
NativeAudio.preload({
    assetId: "fire",
    assetPath: "assets/sounds/fire.mp3",
    audioChannelNum: 1,
    isUrl: false
});

/**
 * This method will play the loaded audio file if present in the memory.
 * @param assetId - identifier of the asset
 * @param time - (optional) play with seek. example: 6.0 - start playing track from 6 sec
 * @returns void
 */
NativeAudio.play({
    assetId: 'fire',
    // time: 6.0 - seek time
});

/**
 * This method will loop the audio file for playback.
 * @param assetId - identifier of the asset
 * @returns void
 */
NativeAudio.loop({
  assetId: 'fire',
});


/**
 * This method will stop the audio file if it's currently playing.
 * @param assetId - identifier of the asset
 * @returns void
 */
NativeAudio.stop({
  assetId: 'fire',
});

/**
 * This method will unload the audio file from the memory.
 * @param assetId - identifier of the asset
 * @returns void
 */
NativeAudio.unload({
  assetId: 'fire',
});

/**
 * This method will set the new volume for a audio file.
 * @param assetId - identifier of the asset
 *        volume - numerical value of the volume between 0.1 - 1.0 default 1.0
 * @returns void
 */
NativeAudio.setVolume({
  assetId: 'fire',
  volume: 0.4,
});

/**
 * this method will getΒ the duration of an audio file.
 * only works if channels == 1
 */
NativeAudio.getDuration({
  assetId: 'fire'
})
.then(result => {
  console.log(result.duration);
})

/**
 * this method will get the current time of a playing audio file.
 * only works if channels == 1
 */
NativeAudio.getCurrentTime({
  assetId: 'fire'
});
.then(result => {
  console.log(result.currentTime);
})

/**
 * This method will return false if audio is paused or not loaded.
 * @param assetId - identifier of the asset
 * @returns {isPlaying: boolean}
 */
NativeAudio.isPlaying({
  assetId: 'fire'
})
.then(result => {
  console.log(result.isPlaying);
})

API

configure(...)

configure(options: ConfigureOptions) => Promise<void>

Configure the audio player

ParamType
optionsConfigureOptions

Since: 5.0.0


preload(...)

preload(options: PreloadOptions) => Promise<void>

Load an audio file

ParamType
optionsPreloadOptions

Since: 5.0.0


isPreloaded(...)

isPreloaded(options: PreloadOptions) => Promise<{ found: boolean; }>

Check if an audio file is preloaded

ParamType
optionsPreloadOptions

Returns: Promise<{ found: boolean; }>

Since: 6.1.0


play(...)

play(options: { assetId: string; time?: number; delay?: number; }) => Promise<void>

Play an audio file

ParamType
options{ assetId: string; time?: number; delay?: number; }

Since: 5.0.0


pause(...)

pause(options: Assets) => Promise<void>

Pause an audio file

ParamType
optionsAssets

Since: 5.0.0


resume(...)

resume(options: Assets) => Promise<void>

Resume an audio file

ParamType
optionsAssets

Since: 5.0.0


loop(...)

loop(options: Assets) => Promise<void>

Stop an audio file

ParamType
optionsAssets

Since: 5.0.0


stop(...)

stop(options: Assets) => Promise<void>

Stop an audio file

ParamType
optionsAssets

Since: 5.0.0


unload(...)

unload(options: Assets) => Promise<void>

Unload an audio file

ParamType
optionsAssets

Since: 5.0.0


setVolume(...)

setVolume(options: { assetId: string; volume: number; }) => Promise<void>

Set the volume of an audio file

ParamType
options{ assetId: string; volume: number; }

Since: 5.0.0


setRate(...)

setRate(options: { assetId: string; rate: number; }) => Promise<void>

Set the rate of an audio file

ParamType
options{ assetId: string; rate: number; }

Since: 5.0.0


getCurrentTime(...)

getCurrentTime(options: { assetId: string; }) => Promise<{ currentTime: number; }>

Set the current time of an audio file

ParamType
options{ assetId: string; }

Returns: Promise<{ currentTime: number; }>

Since: 5.0.0


getDuration(...)

getDuration(options: Assets) => Promise<{ duration: number; }>

Get the duration of an audio file

ParamType
optionsAssets

Returns: Promise<{ duration: number; }>

Since: 5.0.0


isPlaying(...)

isPlaying(options: Assets) => Promise<{ isPlaying: boolean; }>

Check if an audio file is playing

ParamType
optionsAssets

Returns: Promise<{ isPlaying: boolean; }>

Since: 5.0.0


addListener('complete', ...)

addListener(eventName: "complete", listenerFunc: CompletedListener) => Promise<PluginListenerHandle>

Listen for complete event

ParamType
eventName'complete'
listenerFuncCompletedListener

Returns: Promise<PluginListenerHandle>

Since: 5.0.0 return {@link CompletedEvent}


Interfaces

ConfigureOptions

PropTypeDescription
fadebooleanPlay the audio with Fade effect, only available for IOS
focusbooleanfocus the audio with Audio Focus
backgroundbooleanPlay the audio in the background

PreloadOptions

PropTypeDescription
assetPathstringPath to the audio file, relative path of the file, absolute url (file://) or remote url (https://)
assetIdstringAsset Id, unique identifier of the file
volumenumberVolume of the audio, between 0.1 and 1.0
audioChannelNumnumberAudio channel number, default is 1
isUrlbooleanIs the audio file a URL, pass true if assetPath is a file:// url

Assets

PropTypeDescription
assetIdstringAsset Id, unique identifier of the file

PluginListenerHandle

PropType
remove() => Promise<void>

CompletedEvent

PropTypeDescriptionSince
assetIdstringEmit when a play completes5.0.0

Type Aliases

CompletedListener

(state: CompletedEvent): void

6.4.23

8 months ago

6.4.22

8 months ago

6.4.5

10 months ago

6.4.7

10 months ago

6.4.6

10 months ago

6.4.9

10 months ago

6.4.8

10 months ago

6.4.21

9 months ago

6.4.20

9 months ago

6.4.18

10 months ago

6.4.17

10 months ago

6.4.10

10 months ago

6.4.12

10 months ago

6.4.11

10 months ago

6.4.14

10 months ago

6.4.13

10 months ago

6.4.3

1 year ago

6.4.2

1 year ago

6.4.4

1 year ago

6.4.1

1 year ago

6.3.4

1 year ago

6.3.3

1 year ago

6.3.2

1 year ago

6.2.7

1 year ago

6.2.6

1 year ago

6.2.5

1 year ago

6.2.4

1 year ago

6.2.3

1 year ago

6.2.2

1 year ago

6.2.1

1 year ago

6.1.36

1 year ago

6.1.35

1 year ago

6.2.0

1 year ago

6.1.34

1 year ago

6.1.33

1 year ago

6.1.32

1 year ago

6.1.31

1 year ago

6.1.30

2 years ago

6.1.28

2 years ago

6.1.27

2 years ago

6.1.29

2 years ago

6.1.26

2 years ago

6.1.25

2 years ago

6.1.24

2 years ago

6.1.23

2 years ago

6.1.20

2 years ago

6.1.22

2 years ago

6.1.21

2 years ago

6.1.17

2 years ago

6.1.19

2 years ago

6.1.18

2 years ago

6.1.16

2 years ago

6.1.15

2 years ago

6.1.14

2 years ago

6.1.0

2 years ago

6.1.13

2 years ago

6.1.2

2 years ago

6.1.12

2 years ago

6.1.1

2 years ago

6.1.11

2 years ago

6.1.4

2 years ago

6.1.10

2 years ago

6.1.3

2 years ago

6.0.21

2 years ago

6.0.20

2 years ago

6.0.16

2 years ago

6.0.15

2 years ago

6.0.14

2 years ago

6.0.13

2 years ago

6.0.19

2 years ago

6.0.18

2 years ago

6.0.17

2 years ago

6.1.6

2 years ago

6.1.5

2 years ago

6.1.8

2 years ago

6.1.7

2 years ago

6.1.9

2 years ago

6.0.12

2 years ago

6.0.11

2 years ago

6.0.5

2 years ago

6.0.4

2 years ago

6.0.10

2 years ago

6.0.7

2 years ago

6.0.6

2 years ago

5.1.19

2 years ago

6.0.9

2 years ago

5.1.18

2 years ago

6.0.8

2 years ago

5.1.3

2 years ago

5.1.2

2 years ago

5.1.1

2 years ago

5.1.17

2 years ago

5.1.16

2 years ago

5.1.15

2 years ago

5.1.14

2 years ago

5.1.13

2 years ago

5.1.10

2 years ago

5.1.0

2 years ago

5.0.11

2 years ago

5.0.12

2 years ago

5.0.13

2 years ago

5.0.14

2 years ago

5.0.15

2 years ago

5.0.16

2 years ago

5.0.7

2 years ago

5.0.6

3 years ago

5.0.5

3 years ago

5.0.4

3 years ago

5.0.3

3 years ago

5.0.2

3 years ago

5.0.1

3 years ago

5.0.0

3 years ago

4.2.7

3 years ago

4.2.6

3 years ago

4.2.5

3 years ago

4.2.4

3 years ago

4.2.3

3 years ago

4.2.2

3 years ago

4.2.0

3 years ago

4.1.1

3 years ago

4.1.0

3 years ago

4.0.2

3 years ago

4.0.1

3 years ago

4.0.0

3 years ago