Hello Readers,
In today's post we will discuss about media plugin for Cordova apps. This plugin allows to record and play back audio files on a device. This plugin defines media constructor. We need to use this plugin with the device ready event. As it is not available after device ready event.
Now we will move to the installation part:
We can install the plugin through CLI simple run:
$ cordova plugin add cordova-plugin-media
This plugin support following platforms:
- Android
- Blackberry 10
- iOS
- Window phone7 and 8
- Windows
- Windows8
The next step is how we can use the plugin in our application:
var media = new Media(src, mediaSuccess, [mediaError], [mediaStatus]);
In the above, We are using some parameters.
- src contains the URI of the audio.
- mediasuccess is a callback function which fires when media object finished the action i.e play, record and stop. It is an optional function.
- mediaerror is callback function and works when any error occurs. It is an optional function.
- mediastatus works when the user changes the status. It is an optional function. Following are the constant when user changes the status:
Media.MEDIA_NONE = 0;
Media.MEDIA_STARTING = 1;
Media.MEDIA_RUNNING = 2;
Media.MEDIA_PAUSED = 3;
Media.MEDIA_STOPPED = 4;
Following are the methods of this plugin:
- media.play : Start playing an audio file.
- media.pause : Pause the playback an audio file.
- media.playRecord : Start recording an audio file.
- media.pauseRecord : Pause recording an audio file.
- media.stopRecord : Stop recording an audio file.
- media.stop : Stop playing an audio file.
Example:
var beep = new Media('/android_asset/www/media/sounds/beep.mp3');
beep.play();
In the above example, I have defined the audio file path on place of src and then play the sound using play function.
For more details visit: Cordova-plugin-media
Hope this will help you :)
1 Comment(s)