Skip to content

Commit

Permalink
Merge pull request #8396 from caffeinalab/PR-TIMOB-5436
Browse files Browse the repository at this point in the history
[TIMOB-5436] iOS: Expose System Sound Services
  • Loading branch information
hansemannn committed Oct 31, 2016
2 parents 5eaa6d2 + 8d27ac1 commit e5d2679
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 0 deletions.
34 changes: 34 additions & 0 deletions 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 <Titanium.Media.createSystemAlert> 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();
23 changes: 23 additions & 0 deletions 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 <AudioToolbox/AudioServices.h>

@interface TiMediaSystemAlertProxy : TiProxy {
NSURL* url;
SystemSoundID sound;
}

@property (nonatomic,readonly) NSURL *url;

-(void)play:(id)args;

@end

#endif
77 changes: 77 additions & 0 deletions 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
6 changes: 6 additions & 0 deletions iphone/iphone/Titanium.xcodeproj/project.pbxproj
Expand Up @@ -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 */; };
Expand Down Expand Up @@ -978,6 +979,8 @@
BB26FC7F19AB13B9007A35AF /* TiAppiOSNotificationCategoryProxy.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TiAppiOSNotificationCategoryProxy.m; sourceTree = "<group>"; };
BBDD81321A2C71C9003CDA10 /* TiUIAttributedStringProxy.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TiUIAttributedStringProxy.h; sourceTree = "<group>"; };
BBDD81331A2C71C9003CDA10 /* TiUIAttributedStringProxy.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TiUIAttributedStringProxy.m; sourceTree = "<group>"; };
C6BEA5841D8FD0B100485DAC /* TiMediaSystemAlertProxy.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TiMediaSystemAlertProxy.h; sourceTree = "<group>"; };
C6BEA5851D8FD0B100485DAC /* TiMediaSystemAlertProxy.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TiMediaSystemAlertProxy.m; sourceTree = "<group>"; };
CA0D39E31B7F55C6009D534C /* TiAppiOSSearchableItemAttributeSetProxy.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TiAppiOSSearchableItemAttributeSetProxy.h; sourceTree = "<group>"; };
CA0D39E41B7F55C6009D534C /* TiAppiOSSearchableItemAttributeSetProxy.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TiAppiOSSearchableItemAttributeSetProxy.m; sourceTree = "<group>"; };
CA0D39E61B7F709E009D534C /* TiAppiOSSearchableItemProxy.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TiAppiOSSearchableItemProxy.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1579,6 +1582,8 @@
DA864E3111A2314A00B9CD68 /* TiMediaMusicPlayer.m */,
DAA905CC11A359F10030B119 /* TiMediaItem.h */,
DAA905CD11A359F10030B119 /* TiMediaItem.m */,
C6BEA5841D8FD0B100485DAC /* TiMediaSystemAlertProxy.h */,
C6BEA5851D8FD0B100485DAC /* TiMediaSystemAlertProxy.m */,
);
name = Media;
sourceTree = "<group>";
Expand Down Expand Up @@ -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 */,
Expand Down

0 comments on commit e5d2679

Please sign in to comment.