• Stars
    star
    234
  • Rank 170,903 (Top 4 %)
  • Language
    Objective-C
  • License
    MIT License
  • Created about 11 years ago
  • Updated over 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

The low latency audio plugin is designed to enable low latency and polyphonic audio from Cordova/PhoneGap applications, using a very simple and basic API.

Cordova Native Audio Plugin

Cordova / PhoneGap 3.5+ extension for Native Audio playback, aimed at HTML5 gaming and audio applications which require minimum latency, polyphony and concurrency.

Contents

  1. Description
  2. History
  3. Roadmap
  4. Integration
  5. Supported Platforms
  6. Installation
  7. Usage
  8. API
  9. Demo

Description

This Cordova / PhoneGap (3.5+) plugin enables concurrency (multi-channel playback), polyphony (multi-voice playback) and minimized latency (via caching) in audio-based applications, by leveraging native audio APIs. Designed for the use in HTML5-based cross-platform games and mobile/hybrid audio applications.

History

Community-driven, clean fork of the Low Latency Audio Plugin for Cordova / PhoneGap, initially published by Andrew Trice and then maintained by Raymond Xie and Sidney Bofah.

This project cleans up a lot of legacy code, and adds success, error and completion callbacks. It also features integration in AngularJS projects via ngCordova.

Roadmap

Following the Cordova philosophy, this is a shim for a web audio implementation (on mobile) which is as fast and feature-rich as native mobile APIs. Currently, neither HTML5 Audio or the more recent Web Audio API offer a cross-platform solution which 1) is fast, 2) supports polyphony, 3) concurrency and 4) maintains a low overhead.

It should be replaced by a standardised W3C solution as soon as such an implementation offers comparable performance across (mobile) devices, which is crucial for HTML5-based games.

Integration

This plugin is available as an AngularJS service module, facilitating the usage in AngularJS-based Cordova/PhoneGap projects.

It extends the ngCordova project, an effort by the great guys at Drifty, creators of the Ionic Framework. Download it at the ngCordova website or the repository.

Supported Platforms

  • iOS (tested with 7.1.2, 8.1.3)
  • Android (tested in API levels 14 - 21)

Installation

Via Cordova CLI:

cordova plugin add cordova-plugin-nativeaudio

Usage

  1. Wait for deviceReady.
  2. Preload an audio asset and assign an id - either optimized for single-shot style short clips (preloadSimple()) or looping, ambient background audio (preloadComplex())
  3. play() the audio asset via its id.
  4. unload() the audio asset via its id.

API

Preloading

preloadSimple: function ( id, assetPath, successCallback, errorCallback)

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

Uses lower-level native APIs with small footprint (iOS: AudioToolbox/AudioServices). Fully concurrent and multichannel.

  • params
  • id - string unique ID for the audio file
  • assetPath - the relative path or absolute URL (inluding http://) to the audio asset.
  • successCallback - success callback function
  • errorCallback - error callback function
preloadComplex: function ( id, assetPath, volume, voices, delay, successCallback, errorCallback)

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.

Volume & Voices

The default volume is 1.0, a lower default can be set by using a numerical value from 0.1 to 1.0.

By default, there is 1 vice, that is: one instance that will be stopped & restarted on play(). If there are multiple voices (number greater than 0), it will cycle through voices to play overlapping audio.

Change the float-based delay parameter to increase the fade-in/fade-out timing.

Playback

  • params
  • id - string unique ID for the audio file
  • assetPath - the relative path to the audio asset within the www directory
  • volume - the volume of the preloaded sound (0.1 to 1.0)
  • voices - the number of multichannel voices available
  • successCallback - success callback function
  • errorCallback - error callback function
play: function (id, successCallback, errorCallback, completeCallback)

Plays an audio asset.

  • params:
  • id - string unique ID for the audio file
  • successCallback - success callback function
  • errorCallback - error callback function
  • completeCallback - error callback function
loop: function (id, successCallback, errorCallback)

Loops an audio asset infinitely - this only works for assets loaded via preloadComplex.

  • params
  • ID - string unique ID for the audio file
  • successCallback - success callback function
  • errorCallback - error callback function
stop: function (id, successCallback, errorCallback)

Stops an audio file. Only works for assets loaded via preloadComplex.

  • params:
  • ID - string unique ID for the audio file
  • successCallback - success callback function
  • errorCallback - error callback function
unload: function (id, successCallback, errorCallback)

Unloads an audio file from memory.

  • params:
  • ID - string unique ID for the audio file
  • successCallback - success callback function
  • errorCallback - error callback function
setVolumeForComplexAsset: function (id, volume, successCallback, errorCallback)

Changes the volume for preloaded complex assets.

  • params:
  • ID - string unique ID for the audio file
  • volume - the volume of the audio asset (0.1 to 1.0)
  • successCallback - success callback function
  • errorCallback - error callback function

Example Code

In this example, the resources reside in a relative path under the Cordova root folder "www/".

if( window.plugins && window.plugins.NativeAudio ) {
	
	// Preload audio resources
	window.plugins.NativeAudio.preloadComplex( 'music', 'audio/music.mp3', 1, 1, 0, function(msg){
	}, function(msg){
		console.log( 'error: ' + msg );
	});
	
	window.plugins.NativeAudio.preloadSimple( 'click', 'audio/click.mp3', function(msg){
	}, function(msg){
		console.log( 'error: ' + msg );
	});


	// Play
	window.plugins.NativeAudio.play( 'click' );
	window.plugins.NativeAudio.loop( 'music' );


	// Stop multichannel clip after 60 seconds
	window.setTimeout( function(){

		window.plugins.NativeAudio.stop( 'music' );
			
		window.plugins.NativeAudio.unload( 'music' );
		window.plugins.NativeAudio.unload( 'click' );

	}, 1000 * 60 );
}

Demo

The Drumpad in the examples directory is a first starting point.

[sudo] npm install plugin-verify -g
plugin-verify cordova-plugin-nativeaudio ios
plugin-verify cordova-plugin-nativeaudio android

Or, type the commands step by step:

cordova create drumpad com.example.nativeaudio drumpad
cd drumpad
cordova platform add ios
cordova plugin add cordova-plugin-nativeaudio
rm -r www/*
cp -r plugins/cordova-plugin-nativeaudio/test/* www
cordova build ios
cordova emulate ios

More Repositories

1

casino-server

🔥 An online poker game server powered by Redis, node.js and socket.io
JavaScript
965
star
2

cordova-admob-pro

🔥 Cordova Plugin for Google AdMob, DFP, ADX. Easy monetization using mobile Ad, with single line of JavaScript. Compatible with Cordova CLI, Inoic, PhoneGap Build, etc.
Java
716
star
3

socket.io-unity

socket.io client for Unity, power game client with node.js back-end
481
star
4

cordova-httpd

Embed tiny web server into Cordova with a plugin
Objective-C
282
star
5

cordova-plugin-admob

Basic Cordova Plugin for AdMob
C#
264
star
6

coding-to-monetization

📖 From Coding to Monetization:Programmer's Financial Freedom Approach
189
star
7

cordova-plugin-sms

Plugin to operate SMS, send / list / intercept / delete / restore
Java
169
star
8

cordova-plugin-facebookads

Cordova/PhoneGap plugin for Facebook Audience Network Ads
Objective-C
65
star
9

protogen

CLI tool to parse protobuf .proto to C#, based on protobuf-net, works on Windows/Mac/Linux
XSLT
65
star
10

LiteCsvParser

A lite CSV reader and writer in C#, without any heavy dependency
C#
49
star
11

ipa-deploy

Deploy .IPA package to iOS device with a command line tool
Python
43
star
12

cordova-plugin-iad

Cordova plugin to support iAd on iOS
Objective-C
38
star
13

cordova-plugin-iflyspeech

Cordova plugin to support speech recognizer and synthesizer with iFlyTek voice cloud service
Objective-C
33
star
14

cordova-iad-pro

Cordova/PhoneGap Plugin for iAd with Banner and Interstitial
Objective-C
20
star
15

cordova-plugin-flurry

Cordova plugin to support Flurry (analytics and advertisement)
Objective-C
19
star
16

cordova-plugin-wifi

Cordova plugin to access mobile device Wifi info and operation
Java
19
star
17

admob-demo-game-phaser

Demo game for Cordova AdMob plugin, using phaser game engine
JavaScript
18
star
18

cordova-plugin-mopub

Cordova/PhoneGap plugin for MoPub Ads
Objective-C
17
star
19

raymud

Web-based MUD powered by nodejs and socket.io
CSS
16
star
20

gomoku

Gomoku, a HTML5 game working on PC and mobile device
JavaScript
13
star
21

hiquant

Quatitative trading framework and out-of-box toolset for assisting stock/fund investment
Python
13
star
22

magpie

General plugin framework for Cocos2d-x to call Cordova plugins
Shell
13
star
23

cordova-plugin-appleiap

Cordova plugin to support In-App Purchase on iOS
Objective-C
11
star
24

cordova-plugin-qq

Cordova plugin for Tencent QQ OpenSDK
Objective-C
11
star
25

admob-demo-app-ionic

JavaScript
11
star
26

UnityBatchBuild

Batch build toolset for Unity
Python
10
star
27

cordova-plugin-mmedia

Cordova/PhoneGap for Millennial Media Ad
Objective-C
8
star
28

cordova-smart-adserver

Cordova/PhoneGap Plugin for Smart Ad Server
Objective-C
8
star
29

cordova-mobfox-pro

Enhanced MobFox plugin for Cordova/PhoneGap. Banner, Interstitial and Video Ad. Support many other Ad network with server-side integration.
JavaScript
8
star
30

cordova-plugin-lianlianpay

Cordova/PhoneGap plugin for LianLianPay SDK
Java
7
star
31

plugin-verify

A simple utility to verify a Cordova plugin with its own test sample index.html
JavaScript
5
star
32

hotjs

A HTML5/javascript game/app framework written in javascript, in object-oriented way.
JavaScript
5
star
33

charset-convert

A command line tool to convert text file from one charset to another
JavaScript
5
star
34

cordova-plugin-paypalmpl

Cordova Plugin for PayPal MPL Library, not for new SDK.
Objective-C
5
star
35

mixin-pro

Improved javascript mixin to realize multiple inheritance for code reuse
JavaScript
4
star
36

cordova-admobsdk

Google Mobile Ads SDK for Cordova
Objective-C
3
star
37

cordova-admob-adapters

Cordova plugin for AdMob adapters
Objective-C
3
star
38

gomoku2

Gomoku HTML5 Game, newly written with HotJS Framework
JavaScript
3
star
39

cordova-plugin-ext

Extend the Cordova plugin base class with adapter interface. Plugin written based on this interface, can also be used for Unity, Cocos2d-X, and other frameworks.
Java
3
star
40

cordova-plugin-easyad

The easiest way to add AdMob/iAd to Cordova Apps
Objective-C
2
star
41

cordova-plugin-chartboost

Cordova plugin for Chartboost Ads
Objective-C
2
star
42

MagpieDemo

Demo Project for Magpie Framework, Call Cordova Plugins in Cocos2d-X Game
C++
2
star
43

knowledge-management

An Illustrated Guide to Knowledge Management
2
star
44

brackets-eclipse-theme

Eclipse light theme for Brackets
CSS
2
star
45

google-admob-sdk

Add Google Ads SDK to Cordova/PhoneGap project
Objective-C
2
star
46

webrpc

A reusable API server/client framework, running on socket.io
JavaScript
2
star
47

cordova-admob-xdk

Cordova AdMob Plugin for Intel XDK
C++
1
star
48

chinafund

Python
1
star
49

phonegap-jumprope

Jumping rope is cheap, portable, and burns more calories than you might think, try this mobile app
JavaScript
1
star
50

handycorp

A hybrid APP and its back-end, developed with javascript
JavaScript
1
star
51

testpaypalmpl

Demo project for cordova-plugin-paypalmpl
Java
1
star
52

admob-demo-game-pixi

Demo game for Cordova AdMob plugin, using PIXI game engine
JavaScript
1
star
53

cordova-facebook-sdk

Facebook SDK for Cordova
Objective-C
1
star
54

jump-rope

Jumping rope is cheap, portable, and burns more calories than you might think, try this mobile app
Java
1
star
55

cordova-plugin-vungle

Cordova Plugin for Vungle
Objective-C
1
star
56

cordova-plugin-appgrade

Cordova plugin for AppGrade
1
star
57

admob-demo-xdk

AdMob Demo for Intel XDK and Cordova Project
JavaScript
1
star
58

ajax-local

Some workaround to allow AJAX to load local resources
JavaScript
1
star