Updated Branches: refs/heads/master b48fc3003 -> 850a08b65
Fixes CB-879 - Support to set the volume when playing short sounds Project: http://git-wip-us.apache.org/repos/asf/incubator-cordova-ios/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-cordova-ios/commit/850a08b6 Tree: http://git-wip-us.apache.org/repos/asf/incubator-cordova-ios/tree/850a08b6 Diff: http://git-wip-us.apache.org/repos/asf/incubator-cordova-ios/diff/850a08b6 Branch: refs/heads/master Commit: 850a08b65fd81c27b8b4f74743eb2b7d7510d063 Parents: b48fc30 Author: Shazron Abdullah <[email protected]> Authored: Tue Jun 19 15:08:25 2012 -0700 Committer: Shazron Abdullah <[email protected]> Committed: Tue Jun 19 15:08:25 2012 -0700 ---------------------------------------------------------------------- CordovaLib/Classes/CDVSound.h | 4 +++ CordovaLib/Classes/CDVSound.m | 51 ++++++++++++++++++++++++++++++----- 2 files changed, 47 insertions(+), 8 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-cordova-ios/blob/850a08b6/CordovaLib/Classes/CDVSound.h ---------------------------------------------------------------------- diff --git a/CordovaLib/Classes/CDVSound.h b/CordovaLib/Classes/CDVSound.h index f607023..9fc4fbf 100755 --- a/CordovaLib/Classes/CDVSound.h +++ b/CordovaLib/Classes/CDVSound.h @@ -72,11 +72,13 @@ typedef NSUInteger CDVMediaMsg; NSURL* resourceURL; CDVAudioPlayer* player; CDVAudioRecorder* recorder; + NSNumber* volume; } @property (nonatomic, retain) NSString* resourcePath; @property (nonatomic, retain) NSURL* resourceURL; @property (nonatomic, retain) CDVAudioPlayer* player; +@property (nonatomic, retain) NSNumber* volume; @property (nonatomic, retain) CDVAudioRecorder* recorder; @@ -122,4 +124,6 @@ typedef NSUInteger CDVMediaMsg; - (void) startRecordingAudio:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options; - (void) stopRecordingAudio:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options; +- (void) setVolume:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options; + @end http://git-wip-us.apache.org/repos/asf/incubator-cordova-ios/blob/850a08b6/CordovaLib/Classes/CDVSound.m ---------------------------------------------------------------------- diff --git a/CordovaLib/Classes/CDVSound.m b/CordovaLib/Classes/CDVSound.m index ce2c3d0..b0a1e2b 100644 --- a/CordovaLib/Classes/CDVSound.m +++ b/CordovaLib/Classes/CDVSound.m @@ -150,23 +150,55 @@ [self startPlayingAudio:arguments withDict:options]; } -// not used - (void) create:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options { + NSString* callbackId = [arguments objectAtIndex:0]; + NSString* mediaId = [arguments objectAtIndex:1]; + NSString* resourcePath = [arguments objectAtIndex:2]; + + CDVPluginResult* result; + CDVAudioFile* audioFile = [self audioFileForResource:resourcePath withId: mediaId]; + + if (audioFile == nil) { + NSString* errorMessage = [NSString stringWithFormat:@"Failed to initialize Media file with path %@", resourcePath]; + NSString* jsString = [NSString stringWithFormat: @"%@(\"%@\",%d,%@);", @"cordova.require('cordova/plugin/Media').onStatus", mediaId, MEDIA_ERROR,[self createMediaErrorWithCode: MEDIA_ERR_ABORTED message: errorMessage]]; + [super writeJavascript:jsString]; + } else { + result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK]; + [super writeJavascript:[result toSuccessCallbackString:callbackId]]; + } } +- (void) setVolume:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options +{ + NSString* callbackId = [arguments objectAtIndex:0]; + #pragma unused(callbackId) + NSString* mediaId = [arguments objectAtIndex:1]; + NSNumber* volume = [arguments objectAtIndex:2 withDefault:[NSNumber numberWithFloat:1.0]]; + + CDVAudioFile* audioFile; + if ([self soundCache] == nil) { + [self setSoundCache: [NSMutableDictionary dictionaryWithCapacity:1]]; + } else { + audioFile = [[self soundCache] objectForKey: mediaId]; + audioFile.volume = volume; + [[self soundCache] setObject:audioFile forKey:mediaId]; + } + + // don't care for any callbacks +} - (void) startPlayingAudio:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options { - NSString* callbackId = [arguments objectAtIndex:0]; -#pragma unused(callbackId) - + #pragma unused(callbackId) NSString* mediaId = [arguments objectAtIndex:1]; + NSString* resourcePath = [arguments objectAtIndex:2]; + BOOL bError = NO; NSString* jsString = nil; - CDVAudioFile* audioFile = [self audioFileForResource:[arguments objectAtIndex:2] withId: mediaId]; + CDVAudioFile* audioFile = [self audioFileForResource:resourcePath withId: mediaId]; if (audioFile != nil) { if (audioFile.player == nil){ @@ -203,6 +235,10 @@ [audioFile.player stop]; audioFile.player.currentTime = 0; } + if (audioFile.volume != nil) { + audioFile.player.volume = [audioFile.volume floatValue]; + } + [audioFile.player play]; double position = round(audioFile.player.duration * 1000)/1000; jsString = [NSString stringWithFormat: @"%@(\"%@\",%d,%.3f);\n%@(\"%@\",%d,%d);", @"cordova.require('cordova/plugin/Media').onStatus", mediaId, MEDIA_DURATION, position, @"cordova.require('cordova/plugin/Media').onStatus", mediaId, MEDIA_STATE, MEDIA_RUNNING]; @@ -624,10 +660,8 @@ @synthesize resourcePath; @synthesize resourceURL; -@synthesize player; -#ifdef __IPHONE_3_0 +@synthesize player, volume; @synthesize recorder; -#endif - (void) dealloc { @@ -635,6 +669,7 @@ self.resourceURL = nil; self.player = nil; self.recorder = nil; + self.volume = nil; [super dealloc]; }
