Code Monkey home page Code Monkey logo

cordova-plugin-nativeaudio's Introduction

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.

Difference From Original Plugin

  1. Incorporated commits from floatinghotpot#153
  2. Incorporated commits from floatinghotpot#139
  3. Removing deprecated AVAudioSession.h & AVAudioPlayer.h import for iOS
  4. Closed issues

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
  • Android

Installation

Via Cordova CLI:

cordova plugin add https://github.com/wizpanda/cordova-plugin-nativeaudio.git

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

cordova-plugin-nativeaudio's People

Contributors

bbosman avatar dhaval85 avatar floatinghotpot avatar francescomussi avatar fridjon avatar giuseppelt avatar janpio avatar juangaspar avatar menardi avatar nrocy avatar pei-neofonie avatar rasmuswikman avatar richee85 avatar rngwlf avatar rodmg avatar sagrawal31 avatar sashasirotkin avatar sidneys avatar thiago-negri avatar thibaudd avatar tylervz avatar volrath avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

cordova-plugin-nativeaudio's Issues

Pause on Android

Hi! Awesome fork but one question: Is it possible to pause the audio?
I can see references to it the Java files.

Thank you!

Is it possible to play 2 sounds simultaneously?

See title. I want to play a sound which occurs after time x. While this sound plays, i want to let the user play sounds with a button click. As it looks for me the sound gets interrupted,as soon as the other sound gets played. Any help is appreciated, thanks!

[iOS - ionic] Plugin does not release audio resource in phone

Good day.
First, thanks for the work you put in to enhance this plugin on behalf of the errors found in the original source.

I have an application that accepts video calls, I manage to play a sound in the background of an incoming call to alert the user someone is calling him. But once I accept it the sound automatically drops low somewhat similar to a normal phone call, so you have to put your ear near to the speaker, otherwise, you won't hear anything. This makes no sense in a video call as you want to use your camera to show your face while chatting with the other person.

I use the "preloadComplex" function to load the asset in the deviceReady function, once I accept the incoming call I use the "stop" function to pause the sound. Although this seems to work, it causes the audio from the call to be reproduced through the speaker instead of the loudspeaker, it should be high enough to hear the conversation from far away. What should I do here?

My configuration:

  • iPhone 7

  • SO Version: 13.5.1

  • MacBook Pro with macOS Catalina Version 10.15.7 (build 19H114)

  • Ionic Framework: ionic-angular 3.9.10

  • Ionic CLI: 6.11.8

  • Cordova CLI: 10.0.0

  • Cordova Platforms: android 8.0.0, ios 5.1.0

  • Xcode: 12.4 Build Version 12D4e

  • Visual Studio Code Version 1.53.1

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.