From 5df447a02fe7b079b67e3a152638c69e5ada0e46 Mon Sep 17 00:00:00 2001 From: Gaven Henry Date: Fri, 3 Jun 2016 15:09:55 +0800 Subject: [PATCH 1/3] add success, error, status handlers fixes #186 --- src/plugins/media.ts | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/plugins/media.ts b/src/plugins/media.ts index 37c7d4715..eb961a38f 100644 --- a/src/plugins/media.ts +++ b/src/plugins/media.ts @@ -1,4 +1,5 @@ import {CordovaInstance, Plugin} from './plugin'; +import {Observable} from 'rxjs/Rx'; declare var Media: any; /** * @name MediaPlugin @@ -14,6 +15,13 @@ declare var Media: any; * // Playing a file * var file = new MediaPlugin("path/to/file.mp3"); * + * // Catch the Success & Error Output + * file.init.then(() => { + * console.log("Playback Finished"); + * }, (err) => { + * console.log("somthing went wrong! error code: "+err.code+" message: "+err.message); + * }); + * * // play the file * file.play(); * @@ -52,6 +60,8 @@ export class MediaPlugin { // Properties private _objectInstance: any; + status: Observable; + init: Promise; // Methods /** @@ -59,8 +69,10 @@ export class MediaPlugin { * @param src {string} A URI containing the audio content. */ constructor (src: string) { - // TODO handle success, error, and status - this._objectInstance = new Media(src); + let res, rej, next; + this.init = new Promise((resolve, reject) => {res = resolve; rej = reject;}); + this.status = new Observable((observer) => {next = observer.next;}); + this._objectInstance = new Media(src, res, rej, next); } /** From 6e426074235b8506578296f2d995502a51db7784 Mon Sep 17 00:00:00 2001 From: Gaven Henry Date: Sat, 4 Jun 2016 11:01:06 +0800 Subject: [PATCH 2/3] update status method --- src/plugins/media.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/plugins/media.ts b/src/plugins/media.ts index eb961a38f..5e6648022 100644 --- a/src/plugins/media.ts +++ b/src/plugins/media.ts @@ -1,5 +1,5 @@ import {CordovaInstance, Plugin} from './plugin'; -import {Observable} from 'rxjs/Rx'; +import {Observable} from 'rxjs/Observable'; declare var Media: any; /** * @name MediaPlugin @@ -71,7 +71,9 @@ export class MediaPlugin { constructor (src: string) { let res, rej, next; this.init = new Promise((resolve, reject) => {res = resolve; rej = reject;}); - this.status = new Observable((observer) => {next = observer.next;}); + this.status = new Observable((observer) => { + next = data => observer.next(data); + }); this._objectInstance = new Media(src, res, rej, next); } From 75dfdd236c2673afa48ad82345fd68fd9cf7b0bf Mon Sep 17 00:00:00 2001 From: Gaven Henry Date: Fri, 10 Jun 2016 12:00:06 +0800 Subject: [PATCH 3/3] update doc --- src/plugins/media.ts | 31 ++++++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/src/plugins/media.ts b/src/plugins/media.ts index 5e6648022..f7ab6a217 100644 --- a/src/plugins/media.ts +++ b/src/plugins/media.ts @@ -9,13 +9,14 @@ declare var Media: any; * import {MediaPlugin} from 'ionic-native'; * * - * ... * - * - * // Playing a file + * // Create a MediaPlugin instance. Expects path to file or url as argument * var file = new MediaPlugin("path/to/file.mp3"); * * // Catch the Success & Error Output + * // 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) => { @@ -25,14 +26,30 @@ declare var Media: any; * // play the file * file.play(); * - * // skip to 10 seconds + * // 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) * file.seekTo(10000); * - * // stop plying the file + * // stop playing the file * file.stop(); * - * - * ... + * // 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(); * * // Recording to a file * var newFile = new MediaPlugin("path/to/file.mp3");