Media

Improve this doc

$ ionic plugin add cordova-plugin-media
$ npm install --save @ionic-native/media

Repo: https://github.com/apache/cordova-plugin-media

Usage

import { MediaPlugin, MediaObject } from '@ionic-native/media';


constructor(private media: MediaPlugin) { }


...


// Create a MediaPlugin instance.  Expects path to file or url as argument
// We can optionally pass a second argument to track the status of the media

const onStatusUpdate = (status) => console.log(status);

this.media.create('path/to/file.mp3', onStatusUpdate)
  .then((file: MediaObject) => {

     // play the file
     file.play();

     // pause the file
     file.pause();

     // get current playback position
      file.getCurrentPosition().then((position) => {
        console.log(position);
     });

     // get file duration
     let duration = file.getDuration();
     console.log(duration);

     // skip to 10 seconds (expects int value in ms)
     file.seekTo(10000);

     // stop playing the file
     file.stop();

     // release the native audio resource
     // Platform Quirks:
     // iOS simply create a new instance and the old one will be overwritten
     // Android you must call release() to destroy instances of media when you are done
     file.release();

  })
  .catch(e => console.log('Error opening media file', e));


// Recording to a file
this.media.create('path/to/file.mp3')
  .then((file: MediaObject) => {

    file.startRecord();

    file.stopRecord();

  });

Instance Members

create(src, onStatusUpdate)

Open a media file

Param Type Details
src string

A URI containing the audio content.

onStatusUpdate Function

A callback function to be invoked when the status of the file changes

Returns: Promise<MediaObject>

MediaObject

Instance Members

getCurrentAmplitude()

Get the current amplitude of the current recording.

Returns: Promise<any> Returns a promise with the amplitude of the current recording

getCurrentPosition()

Get the current position within an audio file. Also updates the Media object’s position parameter.

Returns: Promise<any> Returns a promise with the position of the current recording

getDuration()

Get the duration of an audio file in seconds. If the duration is unknown, it returns a value of -1.

Returns: number Returns a promise with the duration of the current recording

play()

Starts or resumes playing an audio file.

pause()

Pauses playing an audio file.

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.

seekTo(milliseconds)

Sets the current position within an audio file.

Param Type Details
milliseconds number

The time position you want to set for the current audio file

setVolume(volume)

Set the volume for an audio file.

Param Type Details
volume number

The volume to set for playback. The value must be within the range of 0.0 to 1.0.

startRecord()

Starts recording an audio file.

stopRecord()

Stops recording

pauseRecord()

Pauses recording

resumeRecord()

Resumes recording

stop()

Stops playing an audio file.

API

Native

General