Cordova Plugin MediaThe 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. InstallationIf you want to add this plugin in your app, first you have to install it by typing the below command on command prompt. MediaNow, to use the plugin in your app, type the below command: Parameters:Here, we define the parameters, which are as follows:
Constants:Here, we have listed the constants passed as the only parameter to indicate changes in the status.
Methods:
Now, let's discuss the additional read only parameters in a media plugin:
media.getCurrentAmplitudeYou 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:
media.getCurrentPositionThis 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.getDurationFor returning the duration of an audio file, we can use the media.getDuration method. Syntax:Example:media.pauseTo pause the audio file, we can use the media.pause method. Syntax:Example:media.pauseRecordIn 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.playFor starting or resume playing in an audio file, we can use the media.play method. Example:media.releaseThe 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.resumeRecordIn an audio file, we can use media.resumeRecord method to resume the recording. This method is supported by iOS platform. Example:media.seekToBy 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.setVolumeTo 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.startRecordFor 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.stopTo stop an audio file, use the media.stop method. Syntax:Example:media.stopRecordFor 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.setRateBy 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:MediaErrorWe 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:
Next TopicCordova Plugin Screen Orientation |