fixed the voice selection where you couldn't hear a preview sound
authorkoda
Sun, 19 Sep 2010 13:54:38 +0200
changeset 3883 5934ddf9ed5d
parent 3882 66995a52e265
child 3884 d7479079a8a8
fixed the voice selection where you couldn't hear a preview sound
project_files/HedgewarsMobile/Classes/LevelViewController.m
project_files/HedgewarsMobile/Classes/VoicesViewController.m
--- a/project_files/HedgewarsMobile/Classes/LevelViewController.m	Sun Sep 19 04:12:58 2010 +0200
+++ b/project_files/HedgewarsMobile/Classes/LevelViewController.m	Sun Sep 19 13:54:38 2010 +0200
@@ -36,6 +36,7 @@
 #pragma mark View lifecycle
 -(void) viewDidLoad {
     [super viewDidLoad];
+    srandom(time(NULL));
 
     NSArray *array = [[NSArray alloc] initWithObjects:
                       NSLocalizedString(@"Brutal",@""),
@@ -131,24 +132,24 @@
     UISwitch *theSwitch = (UISwitch *)sender;
     NSIndexSet *sections = [[NSIndexSet alloc] initWithIndex:1];
     NSMutableArray *hogs = [self.teamDictionary objectForKey:@"hedgehogs"];
-
+    NSInteger level;
+    
     if (theSwitch.on) {
         numberOfSections = 2;
         [self.tableView insertSections:sections withRowAnimation:UITableViewRowAnimationFade];
-        for (NSMutableDictionary *hog in hogs)
-            [hog setObject:[NSNumber numberWithInt:4] forKey:@"level"];
-        
-        [[NSNotificationCenter defaultCenter] postNotificationName:@"setWriteNeedTeams" object:nil];
-        [self.tableView reloadData];
+        level = random() % [levelArray count];
     } else {
         numberOfSections = 1;
         [self.tableView deleteSections:sections withRowAnimation:UITableViewRowAnimationFade];
-        
-        for (NSMutableDictionary *hog in hogs)
-            [hog setObject:[NSNumber numberWithInt:0] forKey:@"level"];
+        level = 0;
+    }
 
-        [[NSNotificationCenter defaultCenter] postNotificationName:@"setWriteNeedTeams" object:nil];
-    }
+    for (NSMutableDictionary *hog in hogs)
+        [hog setObject:[NSNumber numberWithInt:0] forKey:@"level"];
+
+    [self.tableView reloadData];
+    [[NSNotificationCenter defaultCenter] postNotificationName:@"setWriteNeedTeams" object:nil];
+
     [sections release];
 }
 
--- a/project_files/HedgewarsMobile/Classes/VoicesViewController.m	Sun Sep 19 04:12:58 2010 +0200
+++ b/project_files/HedgewarsMobile/Classes/VoicesViewController.m	Sun Sep 19 13:54:38 2010 +0200
@@ -22,7 +22,6 @@
 #import "VoicesViewController.h"
 #import "CommodityFunctions.h"
 
-
 @implementation VoicesViewController
 @synthesize teamDictionary, voiceArray, lastIndexPath;
 
@@ -31,7 +30,6 @@
     return rotationManager(interfaceOrientation);
 }
 
-
 #pragma mark -
 #pragma mark View lifecycle
 -(void) viewDidLoad {
@@ -55,13 +53,19 @@
     [self.tableView setContentOffset:CGPointMake(0,0) animated:NO];
 }
 
--(void) viewWillDisappear:(BOOL)animated {
-    [super viewWillDisappear:animated];
+-(void) viewDidAppear:(BOOL)animated {
+    [super viewDidAppear:animated];
+    Mix_OpenAudio(44100, 0x8010, 1, 1024);
+}
+
+-(void) viewDidDisappear:(BOOL)animated {
+    [super viewDidDisappear:animated];
     if(voiceBeingPlayed != NULL) {
         Mix_HaltChannel(lastChannel);
         Mix_FreeChunk(voiceBeingPlayed);
         voiceBeingPlayed = NULL;
     }
+    Mix_CloseAudio();
 }
 
 
@@ -117,6 +121,7 @@
     }
     [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
 
+    // stop any sound before playing another one
     if (voiceBeingPlayed != NULL) {
         Mix_HaltChannel(lastChannel);
         Mix_FreeChunk(voiceBeingPlayed);
@@ -137,9 +142,13 @@
 #pragma mark -
 #pragma mark Memory management
 -(void) didReceiveMemoryWarning {
-    // Releases the view if it doesn't have a superview.
+    if (voiceBeingPlayed != NULL) {
+        Mix_HaltChannel(lastChannel);
+        Mix_FreeChunk(voiceBeingPlayed);
+        voiceBeingPlayed = NULL;
+    }
+    MSG_MEMCLEAN();
     [super didReceiveMemoryWarning];
-    // Relinquish ownership any cached data, images, etc that aren't in use.
 }
 
 -(void) viewDidUnload {