2016-07-18 02:00:56 +08:00
|
|
|
import { CordovaInstance, Plugin } from './plugin';
|
2016-09-22 04:04:46 +08:00
|
|
|
import { Observable } from 'rxjs/Observable';
|
2016-07-18 02:00:56 +08:00
|
|
|
|
|
|
|
|
2016-04-30 11:56:49 +08:00
|
|
|
declare var Media: any;
|
2016-07-18 02:00:56 +08:00
|
|
|
|
2016-08-23 06:19:43 +08:00
|
|
|
export interface MediaError {
|
2016-08-21 22:42:11 +08:00
|
|
|
code: number;
|
|
|
|
message: string;
|
|
|
|
}
|
|
|
|
|
2016-04-25 18:51:42 +08:00
|
|
|
/**
|
2016-04-27 21:54:14 +08:00
|
|
|
* @name MediaPlugin
|
2016-04-25 18:51:42 +08:00
|
|
|
* @description
|
|
|
|
* @usage
|
2016-07-20 23:17:09 +08:00
|
|
|
* ```typescript
|
|
|
|
* import { MediaPlugin } from 'ionic-native';
|
2016-04-30 09:46:35 +08:00
|
|
|
*
|
|
|
|
*
|
|
|
|
*
|
2016-06-10 12:00:06 +08:00
|
|
|
* // Create a MediaPlugin instance. Expects path to file or url as argument
|
2016-07-20 23:17:09 +08:00
|
|
|
* var file = new MediaPlugin('path/to/file.mp3');
|
2016-04-30 09:46:35 +08:00
|
|
|
*
|
2016-06-03 15:09:55 +08:00
|
|
|
* // Catch the Success & Error Output
|
2016-06-10 12:00:06 +08:00
|
|
|
* // Platform Quirks
|
|
|
|
* // iOS calls success on completion of playback only
|
|
|
|
* // Android calls success on completion of playback AND on release()
|
2016-06-03 15:09:55 +08:00
|
|
|
* file.init.then(() => {
|
2016-07-20 23:17:09 +08:00
|
|
|
* console.log('Playback Finished');
|
2016-06-03 15:09:55 +08:00
|
|
|
* }, (err) => {
|
2016-07-20 23:17:09 +08:00
|
|
|
* console.log('somthing went wrong! error code: ' + err.code + ' message: ' + err.message);
|
2016-06-03 15:09:55 +08:00
|
|
|
* });
|
|
|
|
*
|
2016-04-30 09:46:35 +08:00
|
|
|
* // play the file
|
|
|
|
* file.play();
|
|
|
|
*
|
2016-06-10 12:00:06 +08:00
|
|
|
* // pause the file
|
|
|
|
* file.pause();
|
|
|
|
*
|
|
|
|
* // get current playback position
|
|
|
|
* file.getCurrentPosition().then((position) => {
|
|
|
|
* console.log(position);
|
|
|
|
* });
|
|
|
|
*
|
|
|
|
* // get file duration
|
|
|
|
* file.getDuration().then((duration) => {
|
|
|
|
* console.log(position);
|
|
|
|
* });
|
|
|
|
*
|
|
|
|
* // skip to 10 seconds (expects int value in ms)
|
2016-04-30 09:46:35 +08:00
|
|
|
* file.seekTo(10000);
|
|
|
|
*
|
2016-06-10 12:00:06 +08:00
|
|
|
* // stop playing the file
|
2016-04-30 09:46:35 +08:00
|
|
|
* file.stop();
|
|
|
|
*
|
2016-06-10 12:00:06 +08:00
|
|
|
* // 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();
|
2016-04-30 09:46:35 +08:00
|
|
|
*
|
|
|
|
* // Recording to a file
|
2016-07-20 23:17:09 +08:00
|
|
|
* var newFile = new MediaPlugin('path/to/file.mp3');
|
2016-04-30 09:46:35 +08:00
|
|
|
* newFile.startRecord();
|
|
|
|
*
|
|
|
|
* newFile.stopRecord();
|
|
|
|
*
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* ```
|
2016-04-25 18:51:42 +08:00
|
|
|
*/
|
|
|
|
@Plugin({
|
|
|
|
repo: 'https://github.com/apache/cordova-plugin-media',
|
|
|
|
plugin: 'cordova-plugin-media',
|
|
|
|
pluginRef: 'Media'
|
|
|
|
})
|
2016-04-27 21:54:14 +08:00
|
|
|
export class MediaPlugin {
|
2016-04-25 18:51:42 +08:00
|
|
|
|
|
|
|
// Constants
|
2016-09-07 03:48:53 +08:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
2016-04-30 11:56:49 +08:00
|
|
|
static MEDIA_NONE: number = 0;
|
2016-09-07 03:48:53 +08:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
2016-04-30 11:56:49 +08:00
|
|
|
static MEDIA_STARTING: number = 1;
|
2016-09-07 03:48:53 +08:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
2016-04-30 11:56:49 +08:00
|
|
|
static MEDIA_RUNNING: number = 2;
|
2016-09-07 03:48:53 +08:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
2016-04-30 11:56:49 +08:00
|
|
|
static MEDIA_PAUSED: number = 3;
|
2016-09-07 03:48:53 +08:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
2016-04-30 11:56:49 +08:00
|
|
|
static MEDIA_STOPPED: number = 4;
|
2016-04-25 18:51:42 +08:00
|
|
|
|
2016-08-23 06:19:43 +08:00
|
|
|
// error codes
|
2016-09-07 03:48:53 +08:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
2016-08-21 22:42:11 +08:00
|
|
|
static MEDIA_ERR_ABORTED: number = 1;
|
2016-09-07 03:48:53 +08:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
2016-08-23 06:19:43 +08:00
|
|
|
static MEDIA_ERR_NETWORK: number = 2;
|
2016-09-07 03:48:53 +08:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
2016-08-21 22:42:11 +08:00
|
|
|
static MEDIA_ERR_DECODE: number = 3;
|
2016-09-07 03:48:53 +08:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
2016-08-21 22:42:11 +08:00
|
|
|
static MEDIA_ERR_NONE_SUPPORTED: number = 4;
|
2016-08-23 06:19:43 +08:00
|
|
|
|
2016-04-25 18:51:42 +08:00
|
|
|
// Properties
|
2016-04-30 11:56:49 +08:00
|
|
|
private _objectInstance: any;
|
2016-06-03 15:09:55 +08:00
|
|
|
init: Promise<any>;
|
2016-04-25 18:51:42 +08:00
|
|
|
|
|
|
|
// Methods
|
|
|
|
/**
|
|
|
|
* Open a media file
|
|
|
|
* @param src {string} A URI containing the audio content.
|
2016-10-02 03:05:08 +08:00
|
|
|
* @param onStatusUpdate {Function} A callback function to be invoked when the status of the file changes
|
2016-04-25 18:51:42 +08:00
|
|
|
*/
|
2016-10-02 03:05:08 +08:00
|
|
|
constructor(src: string, onStatusUpdate?: Function) {
|
2016-09-07 11:02:46 +08:00
|
|
|
this.init = new Promise<any>((resolve, reject) => {
|
2016-10-02 03:05:08 +08:00
|
|
|
this._objectInstance = new Media(src, resolve, reject, onStatusUpdate);
|
2016-06-04 11:01:06 +08:00
|
|
|
});
|
2016-04-25 18:51:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-09-07 03:48:53 +08:00
|
|
|
* Get the current amplitude of the current recording.
|
|
|
|
* @returns {Promise} Returns a promise with the amplitude of the current recording
|
2016-04-25 18:51:42 +08:00
|
|
|
*/
|
|
|
|
@CordovaInstance()
|
2016-07-18 02:00:56 +08:00
|
|
|
getCurrentAmplitude(): Promise<any> { return; }
|
2016-04-25 18:51:42 +08:00
|
|
|
|
|
|
|
/**
|
2016-09-07 03:48:53 +08:00
|
|
|
* Get the current position within an audio file. Also updates the Media object's position parameter.
|
|
|
|
* @returns {Promise} Returns a promise with the position of the current recording
|
2016-04-25 18:51:42 +08:00
|
|
|
*/
|
|
|
|
@CordovaInstance()
|
2016-07-18 02:00:56 +08:00
|
|
|
getCurrentPosition(): Promise<any> { return; }
|
2016-04-25 18:51:42 +08:00
|
|
|
|
|
|
|
/**
|
2016-09-07 03:48:53 +08:00
|
|
|
* Get the duration of an audio file in seconds. If the duration is unknown, it returns a value of -1.
|
|
|
|
* @returns {Promise} Returns a promise with the duration of the current recording
|
2016-04-25 18:51:42 +08:00
|
|
|
*/
|
|
|
|
@CordovaInstance({
|
|
|
|
sync: true
|
|
|
|
})
|
2016-07-18 02:00:56 +08:00
|
|
|
getDuration(): number { return; }
|
2016-04-25 18:51:42 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Starts or resumes playing an audio file.
|
|
|
|
*/
|
|
|
|
@CordovaInstance({
|
|
|
|
sync: true
|
|
|
|
})
|
2016-07-18 02:00:56 +08:00
|
|
|
play(iosOptions?: {
|
2016-04-30 11:56:49 +08:00
|
|
|
numberOfLoops?: number,
|
|
|
|
playAudioWhenScreenIsLocked?: boolean
|
2016-07-18 02:00:56 +08:00
|
|
|
}): void { }
|
2016-04-25 18:51:42 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Pauses playing an audio file.
|
|
|
|
*/
|
|
|
|
@CordovaInstance({
|
|
|
|
sync: true
|
|
|
|
})
|
2016-07-18 02:00:56 +08:00
|
|
|
pause(): void { }
|
2016-04-25 18:51:42 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
@CordovaInstance({
|
|
|
|
sync: true
|
|
|
|
})
|
2016-07-18 02:00:56 +08:00
|
|
|
release(): void { }
|
2016-04-25 18:51:42 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the current position within an audio file.
|
2016-09-07 03:48:53 +08:00
|
|
|
* @param {number} milliseconds The time position you want to set for the current audio file
|
2016-04-25 18:51:42 +08:00
|
|
|
*/
|
|
|
|
@CordovaInstance({
|
|
|
|
sync: true
|
|
|
|
})
|
2016-07-18 02:00:56 +08:00
|
|
|
seekTo(milliseconds: number): void { }
|
2016-04-25 18:51:42 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the volume for an audio file.
|
|
|
|
* @param volume The volume to set for playback. The value must be within the range of 0.0 to 1.0.
|
|
|
|
*/
|
|
|
|
@CordovaInstance({
|
|
|
|
sync: true
|
|
|
|
})
|
2016-07-18 02:00:56 +08:00
|
|
|
setVolume(volume: number): void { }
|
2016-04-25 18:51:42 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Starts recording an audio file.
|
|
|
|
*/
|
|
|
|
@CordovaInstance({
|
|
|
|
sync: true
|
|
|
|
})
|
2016-07-18 02:00:56 +08:00
|
|
|
startRecord(): void { }
|
2016-04-25 18:51:42 +08:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stops recording
|
|
|
|
*/
|
|
|
|
@CordovaInstance({
|
|
|
|
sync: true
|
|
|
|
})
|
2016-07-18 02:00:56 +08:00
|
|
|
stopRecord(): void { }
|
2016-04-25 18:51:42 +08:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stops playing an audio file.
|
|
|
|
*/
|
|
|
|
@CordovaInstance({
|
|
|
|
sync: true
|
|
|
|
})
|
2016-07-18 02:00:56 +08:00
|
|
|
stop(): void { }
|
2016-04-25 18:51:42 +08:00
|
|
|
|
2016-08-23 06:19:43 +08:00
|
|
|
}
|