awesome-cordova-plugins/src/plugins/media.ts

202 lines
4.5 KiB
TypeScript
Raw Normal View History

2016-07-18 02:00:56 +08:00
import { CordovaInstance, Plugin } from './plugin';
import { Observable } from 'rxjs/Observable';
declare var Media: any;
2016-07-18 02:00:56 +08:00
export interface MediaError {
code: number;
message: string;
}
/**
* @name MediaPlugin
* @description
* @usage
* ```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
* var file = new MediaPlugin('path/to/file.mp3');
2016-04-30 09:46:35 +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()
* file.init.then(() => {
* console.log('Playback Finished');
* }, (err) => {
* console.log('somthing went wrong! error code: ' + err.code + ' message: ' + err.message);
* });
*
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
* var newFile = new MediaPlugin('path/to/file.mp3');
2016-04-30 09:46:35 +08:00
* newFile.startRecord();
*
* newFile.stopRecord();
*
*
*
* ```
*/
@Plugin({
repo: 'https://github.com/apache/cordova-plugin-media',
plugin: 'cordova-plugin-media',
pluginRef: 'Media'
})
export class MediaPlugin {
// Constants
static MEDIA_NONE: number = 0;
static MEDIA_STARTING: number = 1;
static MEDIA_RUNNING: number = 2;
static MEDIA_PAUSED: number = 3;
static MEDIA_STOPPED: number = 4;
//error codes
static MEDIA_ERR_ABORTED: number = 1;
static MEDIA_ERR_NETWORK: number = 2;
static MEDIA_ERR_DECODE: number = 3;
static MEDIA_ERR_NONE_SUPPORTED: number = 4;
// Properties
private _objectInstance: any;
status: Observable<any>;
init: Promise<any>;
// Methods
/**
* Open a media file
* @param src {string} A URI containing the audio content.
*/
2016-07-18 02:00:56 +08:00
constructor(src: string) {
let res, rej, next;
2016-07-18 02:00:56 +08:00
this.init = new Promise<any>((resolve, reject) => { res = resolve; rej = reject; });
2016-06-04 11:01:06 +08:00
this.status = new Observable((observer) => {
2016-07-18 02:00:56 +08:00
next = data => observer.next(data);
2016-06-04 11:01:06 +08:00
});
this._objectInstance = new Media(src, res, rej, next);
}
/**
* Returns the current amplitude of the current recording.
*/
@CordovaInstance()
2016-07-18 02:00:56 +08:00
getCurrentAmplitude(): Promise<any> { return; }
/**
* Returns the current position within an audio file. Also updates the Media object's position parameter.
*/
@CordovaInstance()
2016-07-18 02:00:56 +08:00
getCurrentPosition(): Promise<any> { return; }
/**
* Returns the duration of an audio file in seconds. If the duration is unknown, it returns a value of -1.
*/
@CordovaInstance({
sync: true
})
2016-07-18 02:00:56 +08:00
getDuration(): number { return; }
/**
* Starts or resumes playing an audio file.
*/
@CordovaInstance({
sync: true
})
2016-07-18 02:00:56 +08:00
play(iosOptions?: {
numberOfLoops?: number,
playAudioWhenScreenIsLocked?: boolean
2016-07-18 02:00:56 +08:00
}): void { }
/**
* Pauses playing an audio file.
*/
@CordovaInstance({
sync: true
})
2016-07-18 02:00:56 +08:00
pause(): void { }
/**
* 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 { }
/**
* Sets the current position within an audio file.
* @param milliseconds
*/
@CordovaInstance({
sync: true
})
2016-07-18 02:00:56 +08:00
seekTo(milliseconds: number): void { }
/**
* 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 { }
/**
* Starts recording an audio file.
*/
@CordovaInstance({
sync: true
})
2016-07-18 02:00:56 +08:00
startRecord(): void { }
/**
* Stops recording
*/
@CordovaInstance({
sync: true
})
2016-07-18 02:00:56 +08:00
stopRecord(): void { }
/**
* Stops playing an audio file.
*/
@CordovaInstance({
sync: true
})
2016-07-18 02:00:56 +08:00
stop(): void { }
}