diff --git a/apidoc/Titanium/Media/SystemAlert.yml b/apidoc/Titanium/Media/SystemAlert.yml new file mode 100644 index 00000000000..dec2644d2ea --- /dev/null +++ b/apidoc/Titanium/Media/SystemAlert.yml @@ -0,0 +1,34 @@ +--- +name: Titanium.Media.SystemAlert +summary: An object for playing system sounds. +description: | + You can use this module to provide audible system alerts. + + You can use it to play short (30 seconds or shorter) sounds. The interface does not provide level, positioning, + looping, or timing control, and does not support simultaneous playback: You can play only one sound at a time. + + This module differs from the Sound module because it honors the ringtone volume, not the Music volume. + + Use the method to create a `SystemAlert` object. + + Know more about [System Sound Services](https://developer.apple.com/reference/audiotoolbox/1657326-system_sound_services). +extends: Titanium.Proxy +since: "6.1.0" +platforms: [iphone, ipad] +methods: + - name: play + summary: Start playing the system alert. + +properties: + - name: url + summary: URL identifying the audio resource. + type: String + +examples: + - title: Simple Example + example: | + Simple example of playing a WAVE file from the Resources directory. + + var player = Ti.Media.createSystemAlert({url:"alert.wav"}); + player.play(); + diff --git a/iphone/Classes/TiMediaSystemAlertProxy.h b/iphone/Classes/TiMediaSystemAlertProxy.h new file mode 100644 index 00000000000..3a4a678eff9 --- /dev/null +++ b/iphone/Classes/TiMediaSystemAlertProxy.h @@ -0,0 +1,23 @@ +/** + * Appcelerator Titanium Mobile + * Copyright (c) 2009-2016 by Appcelerator, Inc. All Rights Reserved. + * Licensed under the terms of the Apache Public License + * Please see the LICENSE included with this distribution for details. + */ +#ifdef USE_TI_MEDIA + +#import "TiProxy.h" +#import + +@interface TiMediaSystemAlertProxy : TiProxy { + NSURL* url; + SystemSoundID sound; +} + +@property (nonatomic,readonly) NSURL *url; + +-(void)play:(id)args; + +@end + +#endif diff --git a/iphone/Classes/TiMediaSystemAlertProxy.m b/iphone/Classes/TiMediaSystemAlertProxy.m new file mode 100644 index 00000000000..483d774c230 --- /dev/null +++ b/iphone/Classes/TiMediaSystemAlertProxy.m @@ -0,0 +1,77 @@ +/** + * Appcelerator Titanium Mobile + * Copyright (c) 2009-2016 by Appcelerator, Inc. All Rights Reserved. + * Licensed under the terms of the Apache Public License + * Please see the LICENSE included with this distribution for details. + */ +#ifdef USE_TI_MEDIA + +#import "TiMediaSystemAlertProxy.h" +#import "TiUtils.h" + +@implementation TiMediaSystemAlertProxy + +#pragma mark Proxy Lifecycle + +-(void)_destroy +{ + AudioServicesDisposeSystemSoundID(sound); + RELEASE_TO_NIL(url); + + [super _destroy]; +} + +#pragma mark Public APIs + +-(id)url +{ + return [url absoluteString]; +} + +-(void)setUrl:(id)url_ +{ + RELEASE_TO_NIL(url); + + // Handle string url + if ([url_ isKindOfClass:[NSString class]]) { + url = [[TiUtils toURL:url_ proxy:self] retain]; + + if ([url isFileURL] == NO) { + // we need to download it and save it off into temp file + NSData *data = [NSData dataWithContentsOfURL:url]; + NSString *ext = [[[url path] lastPathComponent] pathExtension]; + TiFile* tempFile = [[TiFile createTempFile:ext] retain]; // file auto-deleted on release + [data writeToFile:[tempFile path] atomically:YES]; + RELEASE_TO_NIL(url); + url = [[NSURL fileURLWithPath:[tempFile path]] retain]; + } + + // Handle file blob + } else if ([url_ isKindOfClass:[TiBlob class]]) { + TiBlob *blob = (TiBlob*)url_; + if ([blob type] == TiBlobTypeFile){ + url = [[NSURL fileURLWithPath:[blob path]] retain]; + } + + // Handle file object + } else if ([url_ isKindOfClass:[TiFile class]]) { + url = [[NSURL fileURLWithPath:[(TiFile*)url_ path]] retain]; + } + + // Dispose sound before re-referencing + AudioServicesDisposeSystemSoundID(sound); + AudioServicesCreateSystemSoundID((CFURLRef)url, &sound); +} + +-(void)play:(id)unused +{ + if (url == nil) { + NSLog(@"[ERROR] Trying to play a system alert without having specified the `url` property. Skipping playback."); + return; + } + + AudioServicesPlayAlertSound(sound); +} + +@end +#endif diff --git a/iphone/iphone/Titanium.xcodeproj/project.pbxproj b/iphone/iphone/Titanium.xcodeproj/project.pbxproj index 992729782dc..9471b117a91 100644 --- a/iphone/iphone/Titanium.xcodeproj/project.pbxproj +++ b/iphone/iphone/Titanium.xcodeproj/project.pbxproj @@ -314,6 +314,7 @@ BB26FC8019AB13B9007A35AF /* TiAppiOSNotificationActionProxy.m in Sources */ = {isa = PBXBuildFile; fileRef = BB26FC7D19AB13B9007A35AF /* TiAppiOSNotificationActionProxy.m */; }; BB26FC8319AB13B9007A35AF /* TiAppiOSNotificationCategoryProxy.m in Sources */ = {isa = PBXBuildFile; fileRef = BB26FC7F19AB13B9007A35AF /* TiAppiOSNotificationCategoryProxy.m */; }; BBDD81341A2C71C9003CDA10 /* TiUIAttributedStringProxy.m in Sources */ = {isa = PBXBuildFile; fileRef = BBDD81331A2C71C9003CDA10 /* TiUIAttributedStringProxy.m */; }; + C6BEA5861D8FD0B100485DAC /* TiMediaSystemAlertProxy.m in Sources */ = {isa = PBXBuildFile; fileRef = C6BEA5851D8FD0B100485DAC /* TiMediaSystemAlertProxy.m */; }; CA0D39E51B7F55C6009D534C /* TiAppiOSSearchableItemAttributeSetProxy.m in Sources */ = {isa = PBXBuildFile; fileRef = CA0D39E41B7F55C6009D534C /* TiAppiOSSearchableItemAttributeSetProxy.m */; }; CA0D39E81B7F709E009D534C /* TiAppiOSSearchableItemProxy.m in Sources */ = {isa = PBXBuildFile; fileRef = CA0D39E71B7F709E009D534C /* TiAppiOSSearchableItemProxy.m */; }; CA0D39EB1B7F74F8009D534C /* TiAppiOSSearchableIndexProxy.m in Sources */ = {isa = PBXBuildFile; fileRef = CA0D39EA1B7F74F8009D534C /* TiAppiOSSearchableIndexProxy.m */; }; @@ -978,6 +979,8 @@ BB26FC7F19AB13B9007A35AF /* TiAppiOSNotificationCategoryProxy.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TiAppiOSNotificationCategoryProxy.m; sourceTree = ""; }; BBDD81321A2C71C9003CDA10 /* TiUIAttributedStringProxy.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TiUIAttributedStringProxy.h; sourceTree = ""; }; BBDD81331A2C71C9003CDA10 /* TiUIAttributedStringProxy.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TiUIAttributedStringProxy.m; sourceTree = ""; }; + C6BEA5841D8FD0B100485DAC /* TiMediaSystemAlertProxy.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TiMediaSystemAlertProxy.h; sourceTree = ""; }; + C6BEA5851D8FD0B100485DAC /* TiMediaSystemAlertProxy.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TiMediaSystemAlertProxy.m; sourceTree = ""; }; CA0D39E31B7F55C6009D534C /* TiAppiOSSearchableItemAttributeSetProxy.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TiAppiOSSearchableItemAttributeSetProxy.h; sourceTree = ""; }; CA0D39E41B7F55C6009D534C /* TiAppiOSSearchableItemAttributeSetProxy.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TiAppiOSSearchableItemAttributeSetProxy.m; sourceTree = ""; }; CA0D39E61B7F709E009D534C /* TiAppiOSSearchableItemProxy.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TiAppiOSSearchableItemProxy.h; sourceTree = ""; }; @@ -1579,6 +1582,8 @@ DA864E3111A2314A00B9CD68 /* TiMediaMusicPlayer.m */, DAA905CC11A359F10030B119 /* TiMediaItem.h */, DAA905CD11A359F10030B119 /* TiMediaItem.m */, + C6BEA5841D8FD0B100485DAC /* TiMediaSystemAlertProxy.h */, + C6BEA5851D8FD0B100485DAC /* TiMediaSystemAlertProxy.m */, ); name = Media; sourceTree = ""; @@ -2827,6 +2832,7 @@ BB26FC8319AB13B9007A35AF /* TiAppiOSNotificationCategoryProxy.m in Sources */, 15CB440E1C4EBE4000D81480 /* TiUIiOSStatusBarProxy.m in Sources */, 24A1EAF8112F4C02003DA834 /* UIImage+RoundedCorner.m in Sources */, + C6BEA5861D8FD0B100485DAC /* TiMediaSystemAlertProxy.m in Sources */, 245B3C3D11375A6600CE7530 /* UtilsModule.m in Sources */, 1592CC3D1C4D800B00C3DB83 /* TiUIiOSScrollIndicatorStyleProxy.m in Sources */, 245B45641139B41800CE7530 /* TiUIiPhoneTableViewCellSelectionStyleProxy.m in Sources */,