add a simple check to prevent loading sounds when device is muted
authorkoda
Sun, 05 Dec 2010 02:44:20 +0100
changeset 4461 2f4f5d649bcd
parent 4460 bdace1e2f8aa
child 4462 018b5642b873
add a simple check to prevent loading sounds when device is muted
hedgewars/uMobile.pas
hedgewars/uSound.pas
project_files/HedgewarsMobile/Classes/ObjcExports.m
project_files/HedgewarsMobile/Classes/UIImageExtra.m
--- a/hedgewars/uMobile.pas	Sun Dec 05 00:05:21 2010 +0100
+++ b/hedgewars/uMobile.pas	Sun Dec 05 02:44:20 2010 +0100
@@ -30,9 +30,11 @@
 procedure replayFinished; cdecl; external;
 procedure updateVisualsNewTurn; cdecl; external;
 function  isApplePhone: Boolean; cdecl; external;
+function  isAppleDeviceMuted: Boolean; cdecl; external;
 procedure AudioServicesPlaySystemSound(num: LongInt); cdecl; external;
 {$ENDIF}
 function  isPhone: Boolean; inline;
+function  isDeviceMute: Boolean; inline;
 procedure performRumble; inline;
 procedure perfExt_AddProgress; inline;
 procedure perfExt_FinishProgress; inline;
@@ -52,6 +54,14 @@
     exit(false);
 end;
 
+function isDeviceMute: Boolean; inline;
+begin
+{$IFDEF IPHONEOS}
+    exit(isAppleDeviceMuted());
+{$ENDIF}
+    exit(false);
+end;
+
 procedure performRumble; inline;
 begin
 {$IFDEF IPHONEOS}
--- a/hedgewars/uSound.pas	Sun Dec 05 00:05:21 2010 +0100
+++ b/hedgewars/uSound.pas	Sun Dec 05 02:44:20 2010 +0100
@@ -46,7 +46,7 @@
 
 
 implementation
-uses uVariables, uConsole, uUtils, uCommands, uDebug;
+uses uVariables, uConsole, uUtils, uCommands, uDebug, uMobile;
 
 const chanTPU = 32;
 var Volume: LongInt;
@@ -147,7 +147,8 @@
     begin
         defVoicepack^.chunks[i]:= nil;
         // preload all the big sound files (>32k) that would otherwise lockup the game
-        if (i in [sndBeeWater, sndBee, sndCake, sndHellishImpact1, sndHellish, sndHomerun, sndMolotov, sndMortar, sndRideOfTheValkyries, sndYoohoo])
+        if (i in [sndBeeWater, sndBee, sndCake, sndHellishImpact1, sndHellish, sndHomerun,
+                  sndMolotov, sndMortar, sndRideOfTheValkyries, sndYoohoo])
             and (Soundz[i].Path <> ptVoices) and (Soundz[i].FileName <> '') then
         begin
             s:= Pathz[Soundz[i].Path] + '/' + Soundz[i].FileName;
@@ -178,7 +179,7 @@
 procedure PlaySound(snd: TSound; voicepack: PVoicepack; keepPlaying: boolean);
 var s:shortstring;
 begin
-    if (not isSoundEnabled) or fastUntilLag then
+    if (not isSoundEnabled) or fastUntilLag or isDeviceMute() then
         exit;
 
     if keepPlaying and (lastChan[snd] <> -1) and (Mix_Playing(lastChan[snd]) <> 0) then
@@ -220,7 +221,7 @@
 function LoopSound(snd: TSound; voicepack: PVoicepack): LongInt;
 var s: shortstring;
 begin
-    if (not isSoundEnabled) or fastUntilLag then
+    if (not isSoundEnabled) or fastUntilLag or isDeviceMute() then
     begin
         LoopSound:= -1;
         exit
--- a/project_files/HedgewarsMobile/Classes/ObjcExports.m	Sun Dec 05 00:05:21 2010 +0100
+++ b/project_files/HedgewarsMobile/Classes/ObjcExports.m	Sun Dec 05 02:44:20 2010 +0100
@@ -21,6 +21,7 @@
 
 #import "ObjcExports.h"
 #import "AmmoMenuViewController.h"
+#import "AudioToolbox/AudioToolbox.h"
 
 #pragma mark -
 #pragma mark internal variables
@@ -32,6 +33,8 @@
 NSInteger grenadeTime;
 // the reference to the newMenu instance
 AmmoMenuViewController *amvc_instance;
+// the audiosession must be initialized before using properties
+BOOL gAudioSessionInited = NO;
 
 #pragma mark -
 #pragma mark functions called like oop
@@ -151,3 +154,34 @@
     DLog(@"updating visuals");
     [amvc_instance updateAmmoVisuals];
 }
+
+// http://stackoverflow.com/questions/287543/how-to-programatically-sense-the-iphone-mute-switch
+BOOL isAppleDeviceMuted(void) {
+    if (!gAudioSessionInited) {
+        AudioSessionInterruptionListener inInterruptionListener = NULL;
+        OSStatus error;
+        if ((error = AudioSessionInitialize (NULL, NULL, inInterruptionListener, NULL)))
+            DLog(@"*** Error *** error in AudioSessionInitialize: %d", error);
+        else
+            gAudioSessionInited = YES;
+    }
+    UInt32 propertySize = sizeof(CFStringRef);
+
+    // this checks if there is volume
+    Float32 volume;
+    OSStatus n = AudioSessionGetProperty(kAudioSessionProperty_CurrentHardwareOutputVolume, &propertySize, &volume);
+    if (n != 0)
+        DLog( @"AudioSessionGetProperty: %d", n );
+    BOOL volumeResult = (volume == 0.0);
+    
+    // this checks if the device is muted
+    CFStringRef state;
+    n = AudioSessionGetProperty(kAudioSessionProperty_AudioRoute, &propertySize, &state);
+    if (n != 0)
+        DLog( @"AudioSessionGetProperty: %d", n );
+    NSString *result = (NSString *)state;
+    BOOL muteResult = ([result length] == 0);
+    releaseAndNil(result);
+    
+    return volumeResult || muteResult;
+}
--- a/project_files/HedgewarsMobile/Classes/UIImageExtra.m	Sun Dec 05 00:05:21 2010 +0100
+++ b/project_files/HedgewarsMobile/Classes/UIImageExtra.m	Sun Dec 05 02:44:20 2010 +0100
@@ -219,7 +219,12 @@
     CGContextRelease(context);
     CGColorSpaceRelease(colorSpace);
 
-    UIImage *newImage = [UIImage imageWithCGImage:imageMasked scale:theScale orientation:UIImageOrientationUp];
+    UIImage *newImage;
+    if ([self respondsToSelector:@selector(imageWithCGImage:scale:orientation:)])
+        newImage = [UIImage imageWithCGImage:imageMasked scale:theScale orientation:UIImageOrientationUp];
+    else
+        newImage = [UIImage imageWithCGImage:imageMasked];
+
     CGImageRelease(imageMasked);
 
     return newImage;