Feature
Not all sounds that an app plays are media, so we should expose the SystemAlert through the Media module. This will let developers choose the most appropriate way to play a sound.
Example Usage
var systemAlert = Ti.Media.createSystemAlert({
url: 'bad.wav'
});
for (var i = 0; i < 10; i++) {
systemAlert.play();
var until = new Date().getTime() + 300;
while (until > new Date().getTime()) {}
}
Example Implementation
#import "TiProxy.h"
#import "TiUtils.h"
#import <AudioToolbox/AudioServices.h>
@interface TiMediaSystemAlertProxy : TiProxy {
NSURL* url;
TiFile *tempFile;
SystemSoundID sound;
}
@property (nonatomic,readonly) NSURL *url;
-(void)play:(id)args;
@end
#import "TiMediaSystemAlertProxy.h"
@implementation TiMediaSystemAlertProxy
#pragma mark Proxy Lifecycle
-(id)init
{
return [super init];
}
-(void)_destroy
{
if (sound) {
AudioServicesDisposeSystemSoundID(sound);
}
RELEASE_TO_NIL(url);
[super _destroy];
}
#pragma mark System Sound
-(NSURL*)url
{
return url;
}
-(void)setUrl:(id)url_
{
RELEASE_TO_NIL(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];
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];
}
}
else if ([url_ isKindOfClass:[TiBlob class]])
{
TiBlob *blob = (TiBlob*)url_;
//TODO: for now we're only supporting File-type blobs
if ([blob type]==TiBlobTypeFile)
{
url = [[NSURL fileURLWithPath:[blob path]] retain];
}
}
else if ([url_ isKindOfClass:[TiFile class]])
{
url = [[NSURL fileURLWithPath:[(TiFile*)url_ path]] retain];
}
if (sound) {
AudioServicesDisposeSystemSoundID(sound);
}
AudioServicesCreateSystemSoundID((CFURLRef)url, &sound);
}
-(void)play:(id)args
{
if (url == nil)
return;
AudioServicesPlayAlertSound(sound);
}
@end
Android Implementation
I'm not sure what the corresponding feature on Android would be.
https://github.com/appcelerator/titanium_mobile/pull/8396
Test-case for QE:
Verified as fixed, SystemAlert is now supported on iOS9 & iOS10. Tested On: iPhone 6 Plus 10.1.1 Device & Simulator iPhone 5S 9.3.5 Device Mac OS Sierra (10.12.1) Ti SDK: 6.1.0.v20161116071014 Appc Studio: 4.8.0.201611121409 Appc NPM: 4.2.8 App CLI: 6.1.0-14 Xcode 8.1 Node v4.4.7 *Closing ticket.*