Cordova Plugin Media
The Cordova plugin Media adds a new functionality in your Cordova app. With the use of this plugin, the user has an ease to record and play audio files on the device. A global media constructor is defined by this plugin. If it is in the global scope, in spite, this plugin is not available until after the deviceready event.
It should be noted that the current implementation of media does not follow the W3C specification for media capture. In the future implementation, it will follow the latest W3C specification. We can also deprecate the current APIs by this implementation.
This plugin is supported by various platforms like Windows, Android, iOS, Browser.
Installation
If you want to add this plugin in your app, first you have to install it by typing the below command on command prompt.
Media
Now, to use the plugin in your app, type the below command:
Parameters:
Here, we define the parameters, which are as follows:
- src: This parameter includes a URI of the audio.
- mediaSuccess (optional): It is a function that is passed as a parameter. We can define it as a callback function that will be executed when a media object has completed the action i.e., play, record, and stop.
- mediaError (optional): It is a function that is passed as a parameter. We can define it as a callback function that will be executed when an error occurs. An integer error code is taken by the mediaError.
- mediaStatus (optional): It is a function that is passed as a parameter. We can define it as a callback function that will be executed to indicate changes in the status.
Constants:
Here, we have listed the constants passed as the only parameter to indicate changes in the status.
- Media.MEDIA_NONE = 0;
- Media.MEDIA_STARTING = 1;
- Media.MEDIA_RUNNING = 2;
- Media.MEDIA_PAUSED = 3;
- Media.MEDIA_STOPPED = 4;
Methods:
- getCurrentAmplitude: You can use media.getCurrentAmplitude method, if you need to know the current amplitude in an audio file.
- getCurrentPosition: In the audio file, for returning the current position, we can use the media.getCurrentPosition method.
- getDuration: For returning the duration of an audio file, we can use the media.getDuration method.
- play: To start or resume playing an audio file, we can use the media.play method.
- pause: To pause the audio file, we can use the media.pause method.
- pauseRecord: In an audio file, use this method to pause the recording.
- release: This method is used to release the audio resources of the specific operating system.
- resumeRecord: In an audio file, use this method to resume the recording.
- seekTo: By using this method, we can move to a position in an audio file.
- setVolume: To set the audio in an audio file, we can use this method.
- startRecord: For starting the recording in an audio file, this method is used.
- stopRecord: For stopping the recording in an audio file, this method is used.
- stop: Use this method to stop an audio file.
- setRate: By using this method, we can set the playback rate to an audio file.
Now, let’s discuss the additional read only parameters in a media plugin:
- position: It refers to the position in seconds within an audio playback.
This parameter does not automatically update during playing an audio file. For doing this, we have to call the getCurrentPosition. - duration: It refers to the time period of media in seconds.
media.getCurrentAmplitude
You can use media.getCurrentAmplitude method, if you need to know the current amplitude in an audio file. If we talk about the compatibility of this method, it is supported by various platforms such as Android and iOS.
Syntax:
Parameters:
- mediaSuccess: It generally refers to the callback function that passes the current amplitude of an audio file as a parameter. The range of amplitude is from 0 – 1.0.
- mediaError (optional): It also refers to the callback function that is passed as a parameter. This method is executed when any event is occurred.
media.getCurrentPosition
This method provides an ease to the user to return the current position within an audio file.
Syntax:
Parameters:
mediaSuccess: It generally refers to the callback function that passes the current position of an audio file in seconds as a parameter.
media.Error (optional): It also refers to the callback function that is passed as a parameter. This method is executed when any event is occurred.
Example:
media.getDuration
For returning the duration of an audio file, we can use the media.getDuration method.
Syntax:
Example:
media.pause
To pause the audio file, we can use the media.pause method.
Syntax:
Example:
media.pauseRecord
In an audio file, we can use media.pauseRecord method to pause the recording. If we talk about the compatibility of this method, it is supported by iOS platform.
Syntax:
Example:
media.play
For starting or resume playing in an audio file, we can use the media.play method.
Example:
media.release
The media.release method is used to release the audio resources of the specific operating system. Specifically, it is important for Android platform, when there are finite numbers of OpenCore instances for media playback. For releasing any media resource that is no longer needed, application should call the release method.
Syntax:
Example:
media.resumeRecord
In an audio file, we can use media.resumeRecord method to resume the recording. This method is supported by iOS platform.
Example:
media.seekTo
By using the media.seekTo method, we can set the current position in an audio file.
Syntax:
Parameters:
milliseconds: In an audio file, this parameter set the audio playback position in milliseconds.
Example:
media.setVolume
To set the audio in an audio file, we can use the media.setVolume method. If we talk about the compatibility of this method, it is supported by various platforms such as Android and iOS.
Syntax:
Parameters:
volume: This parameter set the volume for audio playback. The range of the value must be within 0.0 – 1.0.
Example:
media.startRecord
For starting the recording in an audio file, the media.startRecord method is used. This method is supported by various platforms such as Android, Windows, and iOS.
Syntax:
Example:
media.stop
To stop an audio file, use the media.stop method.
Syntax:
Example:
media.stopRecord
For stopping the recording in an audio file, the media.stopRecord method is used. This method is supported by Windows, Android, and iOS platforms.
Syntax:
Example:
media.setRate
By using the media.setRate method, we can set the playback rate to an audio file. It is supported by iOS platform.
Syntax:
Parameters:
rate: This parameter sets the playback rate for playback an audio file.
Example:
MediaError
We can define MediaError as an object that will be returned to the mediaError callback function, when any event is occurred.
Properties:
code: It defines some predefined error codes.
message: This message shows the details of an error.
Constants:
- MediaError.MEDIA_ERR_ABORTED = 1
- MediaError.MEDIA_ERR_NETWORK = 2
- MediaError.MEDIA_ERR_DECODE = 3
- MediaError.MEDIA_ERR_NONE_SUPPORTED = 4