Skip to content

Commit

Permalink
fix(media): fix success/error/statusUpdate observables
Browse files Browse the repository at this point in the history
closes #1806
  • Loading branch information
ihadeed committed Jul 15, 2017
1 parent 6061af6 commit 7105048
Showing 1 changed file with 53 additions and 27 deletions.
80 changes: 53 additions & 27 deletions src/@ionic-native/plugins/media/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Injectable } from '@angular/core';
import { CordovaInstance, Plugin, checkAvailability, IonicNativePlugin } from '@ionic-native/core';
import { CordovaInstance, Plugin, checkAvailability, IonicNativePlugin, InstanceProperty } from '@ionic-native/core';
import { Observable } from 'rxjs/Observable';
import { Observer } from 'rxjs/Observer';

Expand All @@ -8,7 +8,55 @@ import { Observer } from 'rxjs/Observer';
*/
export class MediaObject {

constructor(private _objectInstance: any, public onSuccess: Observable<any>, public onError: Observable<any>, public onStatusUpdate: Observable<any>) {}
/**
* An observable that notifies you on actions success
*/
onSuccess: Observable<any>;

/**
* An observable that notifies you when an error occurs
*/
onError: Observable<MEDIA_ERROR>;

/**
* An observable that notifies you when the file status changes
*/
onStatusUpdate: Observable<MEDIA_STATUS>;

/**
* @hidden
*/
@InstanceProperty
successCallback: Function;

/**
* @hidden
*/
@InstanceProperty
errorCallback: Function;

/**
* @hidden
*/
@InstanceProperty
statusCallback: Function;

constructor(private _objectInstance: any) {
this.onSuccess = new Observable<any>((observer: Observer<any>) => {
this.successCallback = observer.next.bind(observer);
return () => this.successCallback = () => {};
});

this.onError = new Observable<MEDIA_ERROR>((observer: Observer<MEDIA_ERROR>) => {
this.errorCallback = observer.next.bind(observer);
return () => this.errorCallback = () => {};
});

this.onStatusUpdate = new Observable<MEDIA_STATUS>((observer: Observer<MEDIA_STATUS>) => {
this.statusCallback = observer.next.bind(observer);
return () => this.statusCallback = () => {};
});
}

/**
* Get the current amplitude of the current recording.
Expand Down Expand Up @@ -281,39 +329,17 @@ export class Media extends IonicNativePlugin {
/**
* Open a media file
* @param src {string} A URI containing the audio content.
* @param [onStatusUpdate] {MediaStatusUpdateCallback} A callback function to be invoked when the status of the file changes
* @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}
*/
create(src: string): MediaObject {

let instance: any,
onSuccess: Function,
onError: Function,
onStatusUpdate: Function;


const onSuccessObservable: Observable<any> = new Observable<any>((observer: Observer<any>) => {
onSuccess = observer.next.bind(observer);
return () => {};
}),
onErrorObservable: Observable<any> = new Observable<any>((observer: Observer<any>) => {
onError = observer.next.bind(observer);
return () => {};
}),
onStatusUpdateObservable: Observable<any> = new Observable<any>((observer: Observer<any>) => {
onStatusUpdate = observer.next.bind(observer);
return () => {};
});
let instance: any;

if (checkAvailability(Media.getPluginRef(), null, Media.getPluginName()) === true) {
// Creates a new media object
instance = new (Media.getPlugin())(src, onSuccess, onError, onStatusUpdate);
instance = new (Media.getPlugin())(src);
}

return new MediaObject(instance, onSuccessObservable, onErrorObservable, onStatusUpdateObservable);

return new MediaObject(instance);
}

}

2 comments on commit 7105048

@pedrorckt
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, @ihadeed,

I'am having trouble with this new version of the media plugin. Now that the onSuccess is not in the constructor, I can't make it fire, even subscribing just after creating the media.

This does not work:
current = this.media.create(url); then current.onSuccess.subscribe(() => alert('success'));.

And the onStatusUpdate does not fire status 1, maybe already fired when subscribed?

Please help!

@ihadeed
Copy link
Collaborator Author

@ihadeed ihadeed commented on 7105048 Aug 9, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pedrorckt

Does onStatusChange emit the status changes you are expecting? Usually that observable fires all the changes. OnSuccess only fires a few.

If you're still experiencing issues, please open a new issue instead of commenting here. It's harder to track conversations in the comments.

Please sign in to comment.