Giter VIP home page Giter VIP logo

cordova-plugin-media's Introduction

cordova-plugin-media

This plugin provides the ability to play back audio streaming on a device. Support AAC, AAC+ and MP3 format.

NOTE: The current implementation does not adhere to a W3C specification for media capture, and is provided for convenience only. A future implementation will adhere to the latest W3C specification and may deprecate the current APIs.

This plugin defines a global Media Constructor.

Although in the global scope, it is not available until after the deviceready event.

    document.addEventListener("deviceready", onDeviceReady, false);
    function onDeviceReady() {
        console.log(Media);
    }

Installation

    cordova plugin add https://github.com/AlexisCaffa/cordova-plugin-media.git

Supported Platforms

  • Android (tested)

Without aac decoders:

  • BlackBerry 10
  • iOS
  • Windows Phone 7 and 8
  • Tizen
  • Windows 8
  • Windows
  • Browser

Windows Phone Quirks

  • Only one media file can be played back at a time.

Media

    var media = new Media(src, mediaSuccess, [mediaError], [mediaStatus]);

Parameters

  • src: A URI containing the audio content. (DOMString)

  • mediaSuccess: (Optional) The callback that executes after a Media object has completed the current play, record, or stop action. (Function)

  • mediaError: (Optional) The callback that executes if an error occurs. (Function)

  • mediaStatus: (Optional) The callback that executes to indicate status changes. (Function)

NOTE: cdvfile path is supported as src parameter:

var my_media = new Media('cdvfile://localhost/temporary/recording.mp3', ...);

Constants

The following constants are reported as the only parameter to the mediaStatus callback:

  • Media.MEDIA_NONE = 0;
  • Media.MEDIA_STARTING = 1;
  • Media.MEDIA_RUNNING = 2;
  • Media.MEDIA_PAUSED = 3;
  • Media.MEDIA_STOPPED = 4;

Methods

  • media.play: Start or resume playing an audio file.

  • media.pause: Pause playback of an audio file.

  • media.release: Releases the underlying operating system's audio resources.

  • media.stop: Stop playing an audio file.

media.pause

Pauses playing an audio file.

media.pause();

Quick Example

    // Play audio
    //
    function playAudio(url) {
        // Play the audio file at url
        var my_media = new Media(url,
            // success callback
            function () { console.log("playAudio():Audio Success"); },
            // error callback
            function (err) { console.log("playAudio():Audio Error: " + err); }
        );

        // Play audio
        my_media.play();

        // Pause after 10 seconds
        setTimeout(function () {
            media.pause();
        }, 10000);
    }

media.play

Starts or resumes playing an audio file.

media.play();

Quick Example

    // Play audio
    //
    function playAudio(url) {
        // Play the audio file at url
        var my_media = new Media(url,
            // success callback
            function () {
                console.log("playAudio():Audio Success");
            },
            // error callback
            function (err) {
                console.log("playAudio():Audio Error: " + err);
            }
        );
        // Play audio
        my_media.play();
    }

iOS Quirks

  • numberOfLoops: Pass this option to the play method to specify the number of times you want the media file to play, e.g.:

      var myMedia = new Media("http://audio.ibeat.org/content/p1rj1s/p1rj1s_-_rockGuitar.mp3")
      myMedia.play({ numberOfLoops: 2 })
    
  • playAudioWhenScreenIsLocked: Pass in this option to the play method to specify whether you want to allow playback when the screen is locked. If set to true (the default value), the state of the hardware mute button is ignored, e.g.:

      var myMedia = new Media("http://audio.ibeat.org/content/p1rj1s/p1rj1s_-_rockGuitar.mp3")
      myMedia.play({ playAudioWhenScreenIsLocked : false })
      myMedia.setVolume('1.0');
    

Note: To allow playback with the screen locked or background audio you have to add audio to UIBackgroundModes in the info.plist file. See Apple documentation. Also note that the audio has to be started before going to background.

  • order of file search: When only a file name or simple path is provided, iOS searches in the www directory for the file, then in the application's documents/tmp directory:

      var myMedia = new Media("audio/beer.mp3")
      myMedia.play()  // first looks for file in www/audio/beer.mp3 then in <application>/documents/tmp/audio/beer.mp3
    

media.release

Releases the underlying operating system's audio resources. This is particularly important for Android, since there are a finite amount of OpenCore instances for media playback. Applications should call the release function for any Media resource that is no longer needed.

media.release();

Quick Example

    // Audio player
    //
    var my_media = new Media(src, onSuccess, onError);

    my_media.play();
    my_media.stop();
    my_media.release();

media.stop

Stops playing an audio file.

media.stop();

Quick Example

    // Play audio
    //
    function playAudio(url) {
        // Play the audio file at url
        var my_media = new Media(url,
            // success callback
            function() {
                console.log("playAudio():Audio Success");
            },
            // error callback
            function(err) {
                console.log("playAudio():Audio Error: "+err);
            }
        );

        // Play audio
        my_media.play();

        // Pause after 10 seconds
        setTimeout(function() {
            my_media.stop();
        }, 10000);
    }

MediaError

A MediaError object is returned to the mediaError callback function when an error occurs.

Properties

  • code: One of the predefined error codes listed below.

  • message: An error message describing the details of the error.

Constants

  • MediaError.MEDIA_ERR_ABORTED = 1
  • MediaError.MEDIA_ERR_NETWORK = 2
  • MediaError.MEDIA_ERR_DECODE = 3
  • MediaError.MEDIA_ERR_NONE_SUPPORTED = 4

cordova-plugin-media's People

Contributors

stevengill avatar agrieve avatar purplecabbage avatar ldeluca avatar clelland avatar bennmapes avatar hermwong avatar cmarcelk avatar shazron avatar timkim avatar pwqw avatar alsorokin avatar sgrebnov avatar cfjedimaster avatar martincgg avatar hardeep avatar csantanapr avatar jamesjong avatar jpchase avatar infil00p avatar jsoref avatar mwoghiren avatar zaspire avatar hansmi avatar nikhilkh avatar siovene avatar eworx avatar jarivas avatar javierbb31 avatar mariabukharina avatar

Watchers

James Cloos 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.