Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add callbacks to the Media class for success and failure of playing a…

… media file
  • Loading branch information...
commit f460ee2b2f15b662334404abe22386821854dc82 1 parent 35c3bea
@robb1e robb1e authored shazron committed
View
19 iphone/Classes/Sound.h
@@ -8,11 +8,28 @@
#import <Foundation/Foundation.h>
#import <AudioToolbox/AudioServices.h>
+
+#ifdef __IPHONE_3_0
+#import <AVFoundation/AVFoundation.h>
+#endif
+
#import "PhoneGapCommand.h"
-@interface Sound : PhoneGapCommand {
+@interface Sound : PhoneGapCommand
+#ifdef __IPHONE_3_0
+ <AVAudioPlayerDelegate>
+#endif
+{
+ NSString *successCallback;
+ NSString *errorCallback;
+#ifdef __IPHONE_3_0
+ AVAudioPlayer *player;
+#endif
}
+@property (retain) NSString* successCallback;
+@property (retain) NSString* errorCallback;
+
- (void) play:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options;
@end
View
28 iphone/Classes/Sound.m
@@ -10,8 +10,15 @@
@implementation Sound
+@synthesize successCallback, errorCallback;
+
- (void) play:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options
{
+ NSUInteger argc = [arguments count];
+
+ if (argc > 1) self.successCallback = [arguments objectAtIndex:1];
+ if (argc > 2) self.errorCallback = [arguments objectAtIndex:2];
+
NSBundle * mainBundle = [NSBundle mainBundle];
NSMutableArray *directoryParts = [NSMutableArray arrayWithArray:[(NSString*)[arguments objectAtIndex:0] componentsSeparatedByString:@"/"]];
NSString *filename = [directoryParts lastObject];
@@ -41,4 +48,25 @@ - (void) play:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options
AudioServicesPlaySystemSound(soundID);
}
+/*
+ * event listener when file has stopped playing
+ */
+- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag {
+ NSLog(@"finished playing audio sample");
+
+ if (flag){
+ if (self.successCallback) {
+ NSString* jsString = [[NSString alloc] initWithFormat:@"%@(\"%@\");", self.successCallback, @""];
+ [webView stringByEvaluatingJavaScriptFromString:jsString];
+ [jsString release];
+ }
+ } else {
+ if (self.errorCallback) {
+ NSString* jsString = [[NSString alloc] initWithFormat:@"%@(\"%@\");", self.errorCallback, @""];
+ [webView stringByEvaluatingJavaScriptFromString:jsString];
+ [jsString release];
+ }
+ }
+}
+
@end
View
2  javascripts/iphone/media.js
@@ -5,6 +5,6 @@
Media.prototype.play = function() {
if (this.src != null) {
- PhoneGap.exec("Sound.play", this.src);
+ PhoneGap.exec("Sound.play", this.src, this.successCallback, this.errorCallback);
}
}
View
4 javascripts/media.js
@@ -3,8 +3,10 @@
* This class provides access to the device media, interfaces to both sound and video
* @constructor
*/
-function Media(src) {
+function Media(src, successCallback, errorCallback) {
this.src = src;
+ this.successCallback = successCallback;
+ this.errorCallback = errorCallback;
}
Media.prototype.record = function() {
Please sign in to comment.
Something went wrong with that request. Please try again.