Giter VIP home page Giter VIP logo

rosshadden / cordova-plugin-lowlatencyaudio Goto Github PK

View Code? Open in Web Editor NEW

This project forked from floatinghotpot/cordova-plugin-nativeaudio

0.0 2.0 0.0 1.07 MB

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.

License: MIT License

CSS 10.42% JavaScript 15.88% Java 34.63% Objective-C 39.07%

cordova-plugin-lowlatencyaudio's Introduction

#Cordova Low Latency Audio Plugin for iOS and Android

Prerequisites: A Cordova/PhoneGap 3.0+ project for iOS or Android.

Index

  1. Description
  2. Installation
  3. Usage
  4. API Methods
  5. Example
  6. Demo Projects
  7. Credits ##Description

The low latency audio plugin is designed to enable low latency and polyphonic/background audio from Cordova/PhoneGap applications.

It was orginally developed by Andrew Trice, you can read more about this plugin at: http://www.tricedesigns.com/2012/01/25/low-latency-polyphonic-audio-in-phonegap/

##Installation

To install this plugin, follow the Command-line Interface Guide.

This plugin follows the Cordova 3.0 plugin spec, so it can be installed through the Cordova CLI in your existing Cordova project:

cordova plugin add https://github.com/floatinghotpot/cordova-plugin-lowlatencyaudio.git

##Usage

  1. Preload the audio asset. You can use a relative path or absolute URL and set a lower volume. Note: Make sure to wait for the "deviceready" event before attepting to load assets.
  2. Play the audio asset.
  3. When done, unload the audio asset.

##API Methods

preloadFX: function ( id, assetPath, success, fail)

The preloadFX function loads an audio file into memory. Assets that are loaded using preloadFX are managed/played using AudioServices methods from the AudioToolbox framework. These are very low-level audio methods and have minimal overhead. Audio loaded using this function is played using AudioServicesPlaySystemSound. These assets should be short, and are not intended to be looped or stopped. They are fully concurrent and polyphonic.

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

The preloadAudio function loads an audio file into memory. Assets that are loaded using preloadAudio are managed/played using AVAudioPlayer. These have more overhead than assets laoded via preloadFX, and can be looped/stopped. By default, there is a single "voice" - only one instance that will be stopped & restarted when you hit play. If there are multiple voices (number greater than 0), it will cycle through voices to play overlapping audio. The default volume is for a preloaded sound is 1.0, a lower default volume can be preset by using a numerical value from 0.1 to 1.0.

  • 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 polyphonic voices available
  • success - success callback function
  • fail - error/fail callback function
play: function (id, success, fail)

Plays an audio asset.

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

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

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

Stops an audio file - this only works for assets loaded via preloadAudio.

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

Unloads an audio file from memory.

  • params:
  • ID - string unique ID for the audio file
  • success - success callback function
  • fail - error/fail callback function

##Example

In this example, the resources reside in a relative path under the Cordova root folder "www/". For example, if the file is under "www/audio/music.mp3", then:

var music_mp3 = 'audio/music.mp3';
var click_sound = 'audio/click.mp3'

The implementation goes as follows:

if( window.plugins && window.plugins.LowLatencyAudio ) {
	var lla = window.plugins.LowLatencyAudio;
	
	// preload audio resource
	lla.preloadAudio( music_mp3, music_mp3, 1, function(msg){
	}, function(msg){
		console.log( 'error: ' + msg );
	});
	
	lla.preloadFX( click_sound, click_sound, function(msg){
	}, function(msg){
		console.log( 'error: ' + msg );
	});
	
	// now start playing
	lla.play( click_sound );
	lla.loop( music_mp3 );

	// stop after 1 min	
	window.setTimeout( function(){
		//lla.stop( click_sound );
		lla.stop( music_mp3 );
			
		lla.unload( music_mp3 );
		lla.unload( click_sound );
	}, 1000 * 60 );
}

Demo Projects

The demonstration projects in the examples directory can get you started with the plugin.

Start by creating a project. Then replace index.html with the one in the example directory and copy the assets folder into the /www/ directory.

cordova create Piano com.example.piano Piano
cd Piano
cordova platform add ios
cordova plugin add https://github.com/floatinghotpot/cordova-plugin-lowlatencyaudio.git

Credits

The first iteration of the Plugin was built by Andrew Trice. This plugin was ported to Plugman / Cordova 3 by Raymond Xie, SidneyS and other committers.

cordova-plugin-lowlatencyaudio's People

Contributors

floatinghotpot avatar nrocy avatar rasmuswikman avatar rosshadden avatar sidneys avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.