Skip to main content

Media

EOL Notice

Media will reach its end-of-life on July 1, 2024, and will no longer receive updates or support. Please see Support Policy for additional information.

The Media plugin provides the ability to record and play back audio files on a device.

Installation

If you have not already setup Ionic Enterprise in your app, follow the one-time setup steps.

Next, install the plugin:

npm install @ionic-enterprise/media
npx cap sync

Index

Enumerations

Classes

Interfaces

Type aliases


Enumerations

MEDIA_ERROR

MEDIA_ERROR:

ABORTED

ABORTED: = 1


DECODE

DECODE: = 3


NETWORK

NETWORK: = 2


SUPPORTED

SUPPORTED: = 4



MEDIA_STATUS

MEDIA_STATUS:

NONE

NONE: = 0


PAUSED

PAUSED: = 3


RUNNING

RUNNING: = 2


STARTING

STARTING: = 1


STOPPED

STOPPED: = 4



Classes

Media

usage:

import { Media, MediaObject } from '@ionic-enterprise/media/ngx';

constructor(private media: Media) { }

...

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

const file: MediaObject = this.media.create('file.mp3');

// to listen to plugin events:

file.onStatusUpdate.subscribe(status => console.log(status)); // fires when file status changes

file.onSuccess.subscribe(() => console.log('Action is successful'));

file.onError.subscribe(error => console.log('Error!', error));

// 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();

// Recording to a file
const file: MediaObject = this.media.create('path/to/file.mp3');

file.startRecord();

file.stopRecord();

Some hints if you are using iOS and recording doesn't work: 1.) Try to use a absolute file path but remove beginning "file://". Then it looks like: /var/mobile/Containers/Data/Application/AF438B8B-7724-4FBB-8E69-083463224FC4/tmp/my_file.m4a Example: this.media.create(this.file.tempDirectory.replace(/^file:\/\//, '') + 'my_file.m4a') 2.) If that's not working, too, create the file before using. Example:

import { Media, MediaObject } from '@ionic-enterprise/media/ngx';
import { File } from '@ionic-native/file/ngx';

...

constructor(private media: Media, private file: File) { }

...

this.file.createFile(this.file.tempDirectory, 'my_file.m4a', true).then(() => {
let file = this.media.create(this.file.tempDirectory.replace(/^file:\/\//, '') + 'my_file.m4a');
file.startRecord();
window.setTimeout(() => file.stopRecord(), 10000);
});

You can find the reasons here: https://github.com/ionic-team/ionic-native/issues/1452#issuecomment-299605906

classes: MediaObject

interfaces: MediaError

create

create(src: string): MediaObject

Open a media file

Parameters:

NameTypeDescription
srcstringA URI containing the audio content.

Returns: MediaObject



Interfaces

MediaError

MediaError:

code

● code: number

Error code


message

● message: string

Error message



Type aliases

MediaErrorCallback

Ƭ MediaErrorCallback: function

Type declaration

▸(error: MediaError): void

Parameters:

NameType
errorMediaError

Returns: void


MediaStatusUpdateCallback

Ƭ MediaStatusUpdateCallback: function

Type declaration

▸(statusCode: number): void

Parameters:

NameType
statusCodenumber

Returns: void