Jouez une alerte en diminuant le volume iTunes pendant la lecture.

Nebuchad34Nebuchad34 Membre
décembre 2009 modifié dans Objective-C, Swift, C, C++ #1
Bonjour !

Pour la v3.0 de Poker Manager, j'ai introduit une classe qui me permet de diminuer le volume de lecture d'iTunes à  zéro pendant que le logiciel émet une alerte, et qui remonte progressivement le volume à  la fin de l'alerte (comme sur les alertes sur iPhone).

Je me suis dit que ça serait pas mal de partager le truc.
A noter qu'il vous faudra générer le iTunes.h pour utiliser ScriptingBridge (Google est votre ami).

Elle implémente également du code pour utiliser le synthétiseur vocal en tant qu'alerte, je vous laisse faire le tri.

//<br />//&nbsp; PMAlertSound.h<br />//&nbsp; Poker Manager<br />//<br />//&nbsp; Created by Romain Cosentino on 20/11/09.<br />//&nbsp; Copyright 2009 __MyCompanyName__. All rights reserved.<br />//<br /><br />#import &lt;Cocoa/Cocoa.h&gt;<br />#import &quot;iTunes.h&quot;<br /><br />typedef NSUInteger PMAlertType;<br /><br />enum PMAlertType<br />{<br />	PMAlertTypeTournamentStarted,<br />	PMAlertTypeOneMinuteBeforeBlindsChanges,<br />	PMAlertTypeBlindsChanges,<br />	PMAlertTypeTablesBalance,<br />};<br /><br />@protocol PMAlertSoundDelegate;<br /><br />BOOL anInstanceIscurrentlyChangingVolume;<br /><br />@interface PMAlertSound : NSObject<br />{<br />	BOOL shouldReduceITunesVolume;<br />	BOOL alertShouldPlay;<br />	BOOL alertIsSynthesized;<br />	NSString *soundFilePath;<br />	NSString *stringToSpeak;<br />	NSSpeechSynthesizer *speechSynth;<br />	<br />	int originalVolume, rampVolume;<br />	<br />	NSTimer *volumeTimer;<br />	iTunesApplication *iTunes;<br />	<br />	id&lt;PMAlertSoundDelegate&gt; delegate;<br />}<br /><br />@property (assign) id&lt;PMAlertSoundDelegate&gt; delegate;<br />@property (nonatomic, retain) iTunesApplication *iTunes;<br />@property (nonatomic, assign) BOOL shouldReduceITunesVolume;<br />@property (nonatomic, assign) BOOL alertShouldPlay;<br />@property (nonatomic, assign) BOOL alertIsSynthesized;<br />@property (nonatomic, copy) NSString *soundFilePath;<br />@property (nonatomic, copy) NSString *stringToSpeak;<br /><br />//Don&#39;t use alloc init with this class, use only the following class methods<br /><br />+ (void)playAlert:(PMAlertType)type;<br />+ (void)playSoundWithPath:(NSString*)path;<br />+ (void)playAlert:(PMAlertType)type delegate:(id)aDelegate;<br />+ (void)playSoundWithPath:(NSString*)path delegate:(id)aDelegate;<br /><br />@end<br /><br />@protocol PMAlertSoundDelegate<br /><br />- (void)alertSoundDidFinishPlaying:(PMAlertSound*)alertSound;<br /><br />@end<br />

<br /><br />//&nbsp; PMAlertSound.m<br />//&nbsp; Poker Manager<br />//<br />//&nbsp; Created by Romain Cosentino on 20/11/09.<br />//&nbsp; Copyright 2009 __MyCompanyName__. All rights reserved.<br /><br /><br />#import &quot;PMAlertSound.h&quot;<br /><br />@interface PMAlertSound ()<br />- (void)play;<br />@end<br /><br />@implementation PMAlertSound<br /><br />@synthesize iTunes;<br />@synthesize shouldReduceITunesVolume;<br />@synthesize alertShouldPlay;<br />@synthesize alertIsSynthesized;<br />@synthesize soundFilePath;<br />@synthesize stringToSpeak;<br />@synthesize delegate;<br /><br />+ (void)playAlert:(PMAlertType)type delegate:(id)aDelegate<br />{<br />	PMAlertSound *alertSound = [[PMAlertSound alloc] init];<br />	alertSound.delegate = aDelegate;<br />	<br />	NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];<br />	<br />	alertSound.shouldReduceITunesVolume = [defaults boolForKey:@&quot;lowerITunesVolumeDuringAlerts&quot;];<br />	<br />	alertSound.iTunes = [SBApplication applicationWithBundleIdentifier:@&quot;com.apple.iTunes&quot;];<br />	<br />	switch (type) {<br />		case PMAlertTypeTournamentStarted:<br />			alertSound.alertShouldPlay = [defaults boolForKey:@&quot;alertsTournamentStart&quot;];<br />			alertSound.alertIsSynthesized = [defaults boolForKey:@&quot;alertTournamentStartIsSynthesized&quot;];<br />			alertSound.soundFilePath = [defaults stringForKey:@&quot;alertTournamentStartPath&quot;];<br />			alertSound.stringToSpeak = [defaults stringForKey:@&quot;alertTournamentStartString&quot;];<br />			break;<br />		case PMAlertTypeOneMinuteBeforeBlindsChanges:<br />			alertSound.alertShouldPlay = [defaults boolForKey:@&quot;alertsOneMinuteLeft&quot;];<br />			alertSound.alertIsSynthesized = [defaults boolForKey:@&quot;alertOneMinuteLeftIsSynthesized&quot;];<br />			alertSound.soundFilePath = [defaults stringForKey:@&quot;alertOneMinuteLeftPath&quot;];<br />			alertSound.stringToSpeak = [defaults stringForKey:@&quot;alertOneMinuteLeftString&quot;];<br />			break;<br />		case PMAlertTypeBlindsChanges:<br />			alertSound.alertShouldPlay = [defaults boolForKey:@&quot;alertsBlindsChanges&quot;];<br />			alertSound.alertIsSynthesized = [defaults boolForKey:@&quot;alertBlindsChangesIsSynthesized&quot;];<br />			alertSound.soundFilePath = [defaults stringForKey:@&quot;alertBlindsChangesPath&quot;];<br />			alertSound.stringToSpeak = [defaults stringForKey:@&quot;alertBlindsChangesString&quot;];<br />			break;<br />		case PMAlertTypeTablesBalance:<br />			alertSound.alertShouldPlay = [defaults boolForKey:@&quot;alertsTablesBalance&quot;];<br />			alertSound.alertIsSynthesized = [defaults boolForKey:@&quot;alertTablesBalanceIsSynthesized&quot;];<br />			alertSound.soundFilePath = [defaults stringForKey:@&quot;alertTablesBalancePath&quot;];<br />			alertSound.stringToSpeak = [defaults stringForKey:@&quot;alertTablesBalanceString&quot;];<br />			break;<br />		default:<br />			break;<br />	}<br />	<br />	[alertSound play];<br />}<br /><br />+ (void)playAlert:(PMAlertType)type<br />{<br />	[self playAlert:type delegate:nil];<br />}<br /><br />+ (void)playSoundWithPath:(NSString*)path<br />{<br />	[self playSoundWithPath:path delegate:nil];<br />}<br /><br />+ (void)playSoundWithPath:(NSString*)path delegate:(id)aDelegate<br />{<br />	PMAlertSound *sound = [[PMAlertSound alloc] init];<br />	sound.delegate = aDelegate;<br />	sound.alertShouldPlay = YES;<br />	sound.alertIsSynthesized = NO;<br />	sound.soundFilePath = path;<br />	sound.stringToSpeak = @&quot;&quot;;<br />	<br />	NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];<br />	sound.shouldReduceITunesVolume = [defaults boolForKey:@&quot;lowerITunesVolumeDuringAlerts&quot;];<br />	sound.iTunes = [SBApplication applicationWithBundleIdentifier:@&quot;com.apple.iTunes&quot;];<br />	<br />	[sound play];<br />}<br /><br />- (id) init<br />{<br />	self = [super init];<br />	if (self != nil) {<br />		speechSynth = [[NSSpeechSynthesizer alloc] init];<br />		[speechSynth setDelegate:self];<br />	}<br />	return self;<br />}<br /><br /><br />- (void)play<br />{<br />	if (!alertShouldPlay)<br />	{<br />		[self autorelease];<br />		return;<br />	}<br />	<br />	if (anInstanceIscurrentlyChangingVolume) //another instance of PMAlertSound is already changing iTune Volume, we should not change it concurrently.<br />	{<br />		shouldReduceITunesVolume = NO;<br />	}<br />	<br />	if (shouldReduceITunesVolume)<br />	{<br />		if ( [iTunes isRunning] )<br />		{<br />			anInstanceIscurrentlyChangingVolume = YES;<br />			<br />			originalVolume = [iTunes soundVolume];<br />			<br />			rampVolume = originalVolume;<br />			<br />			volumeTimer = [NSTimer scheduledTimerWithTimeInterval:1.0/50<br />														&nbsp;  target:self <br />														 selector:@selector(decreaseVolume) <br />														 userInfo:nil <br />														&nbsp; repeats:YES];<br />		}<br />	}<br />	<br />	<br />	if (alertIsSynthesized)<br />	{<br />		[speechSynth startSpeakingString:stringToSpeak];<br />	}<br />	else<br />	{<br />		NSSound *sound = [[NSSound alloc] initWithContentsOfFile:soundFilePath byReference:YES];<br />		[sound play];<br />		[sound setDelegate:self];<br />	}<br /><br />}<br /><br />- (void)sound:(NSSound *)sound didFinishPlaying:(BOOL)finishedPlaying<br />{<br />	if (finishedPlaying)<br />	{<br />		[delegate alertSoundDidFinishPlaying:self];<br />		<br />		if (shouldReduceITunesVolume)<br />		{<br />			if ( [iTunes isRunning] ) <br />			{<br />				rampVolume = 0;<br />				[volumeTimer invalidate];<br />				volumeTimer = [NSTimer scheduledTimerWithTimeInterval:1.0/15 <br />															&nbsp;  target:self <br />															 selector:@selector(increaseVolume) <br />															 userInfo:nil <br />															&nbsp; repeats:YES];<br />			}<br />		}<br />		else<br />		{<br />			[self autorelease];<br />		}<br /><br /><br />	}<br /><br />}<br /><br />- (void)speechSynthesizer:(NSSpeechSynthesizer *)sender didFinishSpeaking:(BOOL)success<br />{<br />	<br />	if (shouldReduceITunesVolume)<br />	{<br />		if ( [iTunes isRunning] ) <br />		{<br />			rampVolume = 0;<br />			[volumeTimer invalidate];<br />			volumeTimer = [NSTimer scheduledTimerWithTimeInterval:1.0/15 <br />														&nbsp;  target:self <br />														 selector:@selector(increaseVolume) <br />														 userInfo:nil <br />														&nbsp; repeats:YES];<br />		}<br />	}<br />	else<br />	{<br />		[self autorelease];<br />	}<br />}<br /><br />- (void)decreaseVolume<br />{<br />	rampVolume -= originalVolume / 20;<br />	<br />	if (rampVolume &gt; 0)<br />	{<br />		[iTunes setSoundVolume: rampVolume];<br />	}<br />	else<br />	{<br />		[volumeTimer invalidate];<br />		volumeTimer = nil;<br />		[iTunes setSoundVolume: 0];<br />	}<br />}<br /><br />- (void)increaseVolume<br />{<br />	rampVolume += originalVolume / 20;<br />	<br />	if (rampVolume &lt; originalVolume)<br />	{<br />		[iTunes setSoundVolume: rampVolume];<br />	}<br />	else<br />	{<br />		[volumeTimer invalidate];<br />		volumeTimer = nil;<br />		[iTunes setSoundVolume: originalVolume];<br />		<br />		anInstanceIscurrentlyChangingVolume = NO;<br />		<br />		[self autorelease];<br />	}<br />}<br /><br />- (void)dealloc<br />{<br />	[soundFilePath release];<br />	soundFilePath = nil;<br />	[stringToSpeak release];<br />	stringToSpeak = nil;<br />	[speechSynth release];<br />	speechSynth = nil;<br /><br />	[iTunes release];<br />	iTunes = nil;<br /><br />	[super dealloc];<br />}<br /><br />@end<br />

Connectez-vous ou Inscrivez-vous pour répondre.