Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- - (void)startPlayingAudio:(CDVInvokedUrlCommand*)command
- {
- NSString* callbackId = command.callbackId;
- #pragma unused(callbackId)
- NSString* mediaId = [command.arguments objectAtIndex:0];
- NSString* resourcePath = [command.arguments objectAtIndex:1];
- NSDictionary* options = [command.arguments objectAtIndex:2 withDefault:nil];
- BOOL bError = NO;
- NSString* jsString = nil;
- CDVAudioFile* audioFile = [self audioFileForResource:resourcePath withId:mediaId doValidation:YES forRecording:NO];
- if ((audioFile != nil) && (audioFile.resourceURL != nil)) {
- if (!bError) {
- // audioFile.player != nil or player was successfully created
- // get the audioSession and set the category to allow Playing when device is locked or ring/silent switch engaged
- if ([self hasAudioSession]) {
- NSError* __autoreleasing err = nil;
- NSNumber* playAudioWhenScreenIsLocked = [options objectForKey:@"playAudioWhenScreenIsLocked"];
- BOOL bPlayAudioWhenScreenIsLocked = YES;
- if (playAudioWhenScreenIsLocked != nil) {
- bPlayAudioWhenScreenIsLocked = [playAudioWhenScreenIsLocked boolValue];
- }
- NSString* sessionCategory = bPlayAudioWhenScreenIsLocked ? AVAudioSessionCategoryAmbient : AVAudioSessionCategoryAmbient;
- [self.avSession setCategory:sessionCategory error:&err];
- if (audioFile.player == nil) {
- bError = [self prepareToPlay:audioFile withId:mediaId];
- }
- if (![self.avSession setActive:YES error:&err]) {
- // other audio with higher priority that does not allow mixing could cause this to fail
- NSLog(@"Unable to play audio: %@", [err localizedFailureReason]);
- bError = YES;
- }
- }
- if (!bError) {
- NSLog(@"Playing audio sample '%@'", audioFile.resourcePath);
- NSNumber* loopOption = [options objectForKey:@"numberOfLoops"];
- NSInteger numberOfLoops = 0;
- if (loopOption != nil) {
- numberOfLoops = [loopOption intValue] - 1;
- }
- audioFile.player.numberOfLoops = numberOfLoops;
- if (audioFile.player.isPlaying) {
- [audioFile.player stop];
- audioFile.player.currentTime = 0;
- }
- if (audioFile.volume != nil) {
- audioFile.player.volume = [audioFile.volume floatValue];
- }
- if (audioFile.player == nil) {
- bError = [self prepareToPlay:audioFile withId:mediaId];
- }
- [audioFile.player play];
- double position = round(audioFile.player.duration * 1000) / 1000;
- jsString = [NSString stringWithFormat:@"%@(\"%@\",%d,%.3f);\n%@(\"%@\",%d,%d);", @"cordova.require('org.apache.cordova.media.Media').onStatus", mediaId, MEDIA_DURATION, position, @"cordova.require('org.apache.cordova.media.Media').onStatus", mediaId, MEDIA_STATE, MEDIA_RUNNING];
- [self.commandDelegate evalJs:jsString];
- }
- }
- if (bError) {
- /* I don't see a problem playing previously recorded audio so removing this section - BG
- NSError* error;
- // try loading it one more time, in case the file was recorded previously
- audioFile.player = [[ AVAudioPlayer alloc ] initWithContentsOfURL:audioFile.resourceURL error:&error];
- if (error != nil) {
- NSLog(@"Failed to initialize AVAudioPlayer: %@\n", error);
- audioFile.player = nil;
- } else {
- NSLog(@"Playing audio sample '%@'", audioFile.resourcePath);
- audioFile.player.numberOfLoops = numberOfLoops;
- [audioFile.player play];
- } */
- // error creating the session or player
- // jsString = [NSString stringWithFormat: @"%@(\"%@\",%d,%d);", @"cordova.require('org.apache.cordova.media.Media').onStatus", mediaId, MEDIA_ERROR, MEDIA_ERR_NONE_SUPPORTED];
- jsString = [NSString stringWithFormat:@"%@(\"%@\",%d,%@);", @"cordova.require('org.apache.cordova.media.Media').onStatus", mediaId, MEDIA_ERROR, [self createMediaErrorWithCode:MEDIA_ERR_NONE_SUPPORTED message:nil]];
- [self.commandDelegate evalJs:jsString];
- }
- }
- // else audioFile was nil - error already returned from audioFile for resource
- return;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement