fix(media): function 'create' never returns media object (#1419)

The function never returns an object of MediaObject. The onSuccess callback is wrongly used to return the object. In fact, onSuccess/onError callbacks are only invoked after the current play, record, or stop action is called.
This commit is contained in:
Ronald Mak 2017-04-27 12:34:43 +08:00 committed by Ibby Hadeed
parent b6afc19e72
commit b58149f2f2

View File

@ -126,6 +126,24 @@ export class MediaObject {
stop(): void { } stop(): void { }
} }
export type MediaStatusUpdateCallback = (statusCode: number) => void;
export interface MediaError {
/**
* Error message
*/
message: string;
/**
* Error code
*/
code: number;
}
export type MediaErrorCallback = (error: MediaError) => void;
/** /**
* @name Media * @name Media
* @description * @description
@ -144,9 +162,10 @@ export class MediaObject {
* // We can optionally pass a second argument to track the status of the media * // We can optionally pass a second argument to track the status of the media
* *
* const onStatusUpdate = (status) => console.log(status); * const onStatusUpdate = (status) => console.log(status);
* const onSuccess = () => console.log('Action is successful.');
* const onError = (error) => console.error(error.message);
* *
* this.media.create('path/to/file.mp3', onStatusUpdate) * const file: MediaObject = this.media.create('path/to/file.mp3', onStatusUpdate, onSuccess, onError);
* .then((file: MediaObject) => {
* *
* // play the file * // play the file
* file.play(); * file.play();
@ -175,23 +194,21 @@ export class MediaObject {
* // Android you must call release() to destroy instances of media when you are done * // Android you must call release() to destroy instances of media when you are done
* file.release(); * file.release();
* *
* })
* .catch(e => console.log('Error opening media file', e));
* *
* *
* // Recording to a file * // Recording to a file
* this.media.create('path/to/file.mp3') * const file: MediaObject = this.media.create('path/to/file.mp3');
* .then((file: MediaObject) => {
* *
* file.startRecord(); * file.startRecord();
* *
* file.stopRecord(); * file.stopRecord();
* *
* });
* *
* ``` * ```
* @classes * @classes
* MediaObject * MediaObject
* @interfaces
* MediaError
*/ */
@Plugin({ @Plugin({
pluginName: 'MediaPlugin', pluginName: 'MediaPlugin',
@ -245,18 +262,20 @@ export class MediaPlugin {
/** /**
* Open a media file * Open a media file
* @param src {string} A URI containing the audio content. * @param src {string} A URI containing the audio content.
* @param onStatusUpdate {Function} A callback function to be invoked when the status of the file changes * @param onStatusUpdate {MediaStatusUpdateCallback} A callback function to be invoked when the status of the file changes
* @return {Promise<MediaObject>} * @param onSuccess {Function} A callback function to be invoked after the current play, record, or stop action is completed
* @param onError {MediaErrorCallback} A callback function is be invoked if an error occurs.
* @return {MediaObject}
*/ */
@CordovaCheck() @CordovaCheck()
create(src: string, onStatusUpdate?: Function): Promise<MediaObject> { create(src: string,
onStatusUpdate?: MediaStatusUpdateCallback,
onSuccess?: Function,
onError?: MediaErrorCallback): MediaObject {
return new Promise<MediaObject>((resolve, reject) => {
// Creates a new media object // Creates a new media object
// Resolves with the media object const instance = new Media(src, onSuccess, onError, onStatusUpdate);
// or rejects with the error return new MediaObject(instance);
const instance = new Media(src, () => resolve(new MediaObject(instance)), reject, onStatusUpdate);
});
} }