Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8396 from caffeinalab/PR-TIMOB-5436
[TIMOB-5436] iOS: Expose System Sound Services
- Loading branch information
Showing
4 changed files
with
140 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters