Yanap (yet another native audio plugin)
This Cordova's plugin is an alternative to the cordova-plugin-media
and cordova-plugin-nativeaudio
/cordova-plugin-lowlatencyaudio
plugins to play audio from native on Android. It may support iOS in the future.
Main features we have in mind while developing it are:
- gapless audio loops
- low latency playback for short sounds
Basic usage
var Yanap = cordovapluginsYanap; var myBackgroundLoop = YanapAUDIO_TYPELOOP;myBackgroundLoop;myBackgroundLoop; // and when it is not needed anymore...// myBackgroundLoop.stop();// myBackgroundLoop.release();
API
Yanap.AUDIO_TYPE
Enum of available audio instance types:
YanapAUDIO_TYPELOOP // for -relatively- long audio tracks that need to loopYanapAUDIO_TYPEMUSIC // for -relatively- long audio tracksYanapAUDIO_TYPESOUND // for short audio files (fx, ui...)
Yanap.AUDIO_INSTANCE_STATUS
Enum of possible audio instance status:
YanapAUDIO_INSTANCE_STATUSERROR // something bad happened, instance cannot be used anymoreYanapAUDIO_INSTANCE_STATUSEMPTY // instance has been initialisedYanapAUDIO_INSTANCE_STATUSLOADING // currently loadingYanapAUDIO_INSTANCE_STATUSLOADED // loaded successfullyYanapAUDIO_INSTANCE_STATUSPLAYING // currently playingYanapAUDIO_INSTANCE_STATUSLOOPING // set when looping the first time and re-triggered at each consecutive loopYanapAUDIO_INSTANCE_STATUSSTOPPED // playback reached the end or has been manually stoppedYanapAUDIO_INSTANCE_STATUSRELEASED // all resources for this instance have been released
NB:
LOOPING
is available only for audio typesLOOP
PLAYING
andSTOPPED
are not available for audio typesSOUND
Yanap.AudioInstance(audioType, onStatusUpdate)
Audio instance constructor.
audioType
can be any value present inYanap.AUDIO_TYPE
.onStatusUpdate
is an optional method that will receive aYanap.AUDIO_INSTANCE_STATUS
as first parameter and a string containing additional information as second parameter (mostly used when an error happened).
var mySong = YanapAUDIO_TYPEMUSIC { if status === YanapAUDIO_INSTANCE_STATUSERROR return console; console; };
This creation is successful even if audio type is invalid (it would fail during the loading).
Yanap.AudioInstance.load(filePath)
Used to load a file.
filePath
is a string:- relative to application asset folder if string starts with
file:///android_asset/
- else an absolute path if string starts with
file://
- else a relative path to application
cache
directory
- relative to application asset folder if string starts with
// example 1: if you want to be sure that audio is ready before trying to play itvar myLaserFx = YanapAUDIO_TYPESOUND { if status === YanapAUDIO_INSTANCE_STATUSERROR return console; if status === YanapAUDIO_INSTANCE_STATUSLOADED myLaserFx; };myLaserFx; // example 2: if you want to play automatically when loaded and don't care about errorsvar myLaserFx = YanapAUDIO_TYPESOUND;myLaserFx;myLaserFx;
Yanap.AudioInstance.play()
Used to start or resume an audio instance playback.
Yanap.AudioInstance.stop()
Used to interrupt an audio instance playback.
Yanap.AudioInstance.setVolume(channel1, channel2)
Set audio volume.
channel1
andchannel2
represents the left and right speakers.- Values are in a range of
0.0
~1.0
.
Yanap.AudioInstance.release()
Has to be called when you don't need an audio instance anymore. It's automatically stopping playback before releasing all resources.
Yanap.AudioInstance.fileLength
Size of the audio file in bytes. Default is -1
.
Yanap.releaseAll()
Static method used to stop and release all audio instances.