Need help upgrading to Ionic Framework 4.0? Get assistance with our Enterprise Migration Services EXPLORE NOW

Native Audio

Improve this doc

Native Audio Playback

Repo: https://github.com/floatinghotpot/cordova-plugin-nativeaudio

Installation

  1. Install the Cordova and Ionic Native plugins:
    $ ionic cordova plugin add cordova-plugin-nativeaudio
    $ npm install --save @ionic-native/native-audio@4
    
  2. Add this plugin to your app's module

Supported platforms

Usage

import { NativeAudio } from '@ionic-native/native-audio';

constructor(private nativeAudio: NativeAudio) { }

...

this.nativeAudio.preloadSimple('uniqueId1', 'path/to/file.mp3').then(onSuccess, onError);
this.nativeAudio.preloadComplex('uniqueId2', 'path/to/file2.mp3', 1, 1, 0).then(onSuccess, onError);

this.nativeAudio.play('uniqueId1').then(onSuccess, onError);

// can optionally pass a callback to be called when the file is done playing
this.nativeAudio.play('uniqueId1', () => console.log('uniqueId1 is done playing'));

this.nativeAudio.loop('uniqueId2').then(onSuccess, onError);

this.nativeAudio.setVolumeForComplexAsset('uniqueId2', 0.6).then(onSuccess,onError);

this.nativeAudio.stop('uniqueId1').then(onSuccess,onError);

this.nativeAudio.unload('uniqueId1').then(onSuccess,onError);

Instance Members

preloadSimple(id, assetPath)

Loads an audio file into memory. Optimized for short clips / single shots (up to five seconds). Cannot be stopped / looped.

Param Type Details
id string

unique ID for the audio file

assetPath string

the relative path or absolute URL (inluding http://) to the audio asset.

Returns: Promise<any>

preloadComplex(id, assetPath, volume, voices, delay)

Loads an audio file into memory. Optimized for background music / ambient sound. Uses highlevel native APIs with a larger footprint. (iOS: AVAudioPlayer). Can be stopped / looped and used with multiple voices. Can be faded in and out using the delay parameter.

Param Type Details
id string

unique ID for the audio file

assetPath string

the relative path or absolute URL (inluding http://) to the audio asset.

volume number

the volume of the preloaded sound (0.1 to 1.0)

voices number

the number of multichannel voices available

delay number

Returns: Promise<any>

play(id, completeCallback)

Plays an audio asset

Param Type Details
id string

unique ID for the audio file

completeCallback Function

optional. Callback to be invoked when audio is done playing

Returns: Promise<any>

stop(id)

Stops playing an audio

Param Type Details
id string

unique ID for the audio file

Returns: Promise<any>

loop(id)

Loops an audio asset infinitely, this only works for complex assets

Param Type Details
id string

unique ID for the audio file

Returns: Promise<any>

unload(id)

Unloads an audio file from memory

Param Type Details
id string

unique ID for the audio file

Returns: Promise<any>

setVolumeForComplexAsset(id, volume)

Changes the volume for preloaded complex assets.

Param Type Details
id string

unique ID for the audio file

volume number

the volume of the audio asset (0.1 to 1.0)

Returns: Promise<any>

API

Native

General