Passing audio from one view to another :(

Discussion in 'iOS Programming' started by SunnyLi, Nov 12, 2010.

  1. SunnyLi Guest

    Joined:
    Oct 7, 2010
    #1
    Hey Guys,

    im having some trouble passing a sound effect that i have on my game view to the settings view so it can but turned off it the uiswitch is moved to off.

    in the gameview ive created an instance for theaudio like so:

    Code:
    #import <UIKit/UIKit.h>
    #import <AVFoundation/AVAudioPlayer.h>
    #import "SettingsViewController.h"
    #import "CollisionViewController.h"
    
    @interface CollisionViewController : UIViewController <UITextFieldDelegate, AVAudioPlayerDelegate>{
    		
    	[B]AVAudioPlayer *theAudio;[/B]
    	
    }
    then in .m ive added the code to call the file i want too play.
    Code:
    #import "CollisionViewController.h"
    #import <AudioToolbox/AudioServices.h>
    #import "SettingsViewController.h"
    
    
    
    @implementation CollisionViewController
    
    	if (!stopdetection && CGRectIntersectsRect(ball.frame, paddle.frame))
    	{
    		score +=10;
    		scoreLabel.text = [NSString stringWithFormat:@"%d", score];
    		
    		stopdetection = YES;
    		
    		[B]NSString *filePath = [[NSBundle mainBundle] pathForResource:@"short" ofType:@"mp3"];
    		NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:filePath];
    		theAudio= [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:nil];
    		
    		[theAudio play];
    		theAudio.numberOfLoops = -1;
    		
    		SettingsViewController *second = [[SettingsViewController alloc] initWithNibName:nil bundle:nil];
    		second.theAudio = theAudio;
    		
    		[filePath release];
    		[fileURL release];
    		[/B]
    		
    	}
    and on the settings page ive declared theaudio and created a property for it:

    .h
    Code:
    #import <UIKit/UIKit.h>
    #import <AVFoundation/AVAudioPlayer.h>
    #import "CollisionViewController.h"
    
    
    @interface SettingsViewController : UIViewController{
    	
    	IBOutlet UISwitch *muteSwitch; 
    	AVAudioPlayer *theAudio;
    	IBOutlet UISlider *slider;
    }
    
    @property (nonatomic,retain) AVAudioPlayer *theAudio; 
    @property (nonatomic,retain) UISwitch *muteSwitch;
    - (IBAction) volume;
    - (IBAction) switchValueChanged;
    - (IBAction) toggleButtonPressed;
    and in the .m ive added the code to stop the audio if the switch is moved.

    Code:
    #import "SettingsViewController.h"
    #import "CollisionViewController.h"
    
    @implementation SettingsViewController
    
    @synthesize theAudio;
    @synthesize muteSwitch;
    
    	
    	
    - (IBAction)volume{
    	theAudio.volume = slider.value;
    }
    
    - (IBAction) switchValueChanged{  
    	
        if ([muteSwitch isOn])  
            [muteSwitch setOn:NO animated:YES];  
        else   
            [muteSwitch setOn:YES animated:YES];  
    }  
    
    
    - (IBAction) toggleButtonPressed{
    	if(muteSwitch.on){
    		[theAudio play];
    	}
    	else{
    		[theAudio stop];
    		
    	}
    	
    }
    But i cant get it to work correctly can anyone tell me what im missing here please.

    SunnyLi
     
  2. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    May not be related to your issue but if you go through the trouble to define properties and synthesize accessors for it, you should also take the time to reference them correctly.

    Code:
    [theAudio play];
    doesn't.

    P.S. That is why often it is recommended to name the instance variables for your properties different from the property-names themselves so you know which is which. I usually go with something like this:

    .h
    Code:
    @interface SettingsViewController : UIViewController{
    	AVAudioPlayer *_theAudio;
    }
    
    @property (nonatomic,retain) AVAudioPlayer *theAudio; 
    .m
    Code:
    @synthesize theAudio = _theAudio;
    Then you could do:

    Code:
    [self.theAudio play];
    As to your issue with "i cant get it to work correctly", please elaborate. Remember: be specific.
     
  3. SunnyLi thread starter Guest

    Joined:
    Oct 7, 2010
    #3
    Thanks for your reply Dejo you've helped me a lot on here :)

    I know my naming conventions are poor, I'm trying to get better at it this is only my 2nd month doing this with no programming experience :D

    Basically I have a settings page with a uiswitch on, what I'd like to be able to do is when the switch is off the sound effects one the game stop and when you switch the switch back on the sound comes back.
     
  4. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #4
    Alright, that explains what you're trying to do. Now, you should explain what is actually happening. You say it's not working and I asked you to elaborate. Please do so. Are you seeing any errors or warnings at compile-time? How about run-time? Is it crashing? If so, what does the crash log say? Have you tried basic debugging? If so, what? What have you discovered? Etc. We can't do this stuff for you.
     
  5. SunnyLi, Nov 13, 2010
    Last edited: Nov 13, 2010

    SunnyLi thread starter Guest

    Joined:
    Oct 7, 2010
    #5
    Everytime there's a collision between these 2 images the sound works fine, so I added the code like above to try and be able to stop the sound if i needed too.
    I don't get any warnings, errors, crashes or anything. The app loads but when I flick the switch nothing happens.


    Code:
    		
    		SettingsViewController *second = [[SettingsViewController alloc] initWithNibName:nil bundle:nil];
    		second.theAudio = theAudio;
    		
    		
    	}
    
    I was looking at this bit which passes the sound to the settings view I've done this before when using a transition but not sure if that's correct


    ---edit---

    Ive just done pretty much the same thing moving the score value into a gameover view using this code:

    Code:
    GameOverViewController *second = [[GameOverViewController alloc] initWithNibName:nil bundle:nil];
    			second.score = score;
    			second.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    			[self presentModalViewController:second animated:YES];
    
    but this audio isnt working.
     
  6. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #6
    I'll ask again then: what basic debugging have you done? You know, things like setting breakpoints and stepping through code. Or even NSLogs. Part of basic debugging is verifying that what you think is happening at each step is actually happening. Using skills like this is very key to troubleshooting.

    And the reason I'm pushing so hard on this stuff is that if you look at your code for switchValueChanged, you may notice that it doesn't even reference theAudio.

    Unless, I don't understand what it is you are trying to do exactly. Seems like you keep changing the conditions as we continue this conversation.
     
  7. SunnyLi thread starter Guest

    Joined:
    Oct 7, 2010
    #7
    I understand what you mean, ive tested this code on just a 2 view application and it stops the sound effects fine, its just when i add the 3rd view which is the actual game where it doesnt work anymore.
     
  8. SunnyLi thread starter Guest

    Joined:
    Oct 7, 2010
    #8
    This must be my problem, whats the best way to share audio?


    Code:
    		
    		SettingsViewController *second = [[SettingsViewController alloc] initWithNibName:nil bundle:nil];
    		second.theAudio = theAudio;
    		
    		
    	}
    
    			
    
     
  9. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #9
    What makes you say that's the problem? You're still being awfully light on details.
     
  10. SunnyLi thread starter Guest

    Joined:
    Oct 7, 2010
    #10
    I know ive only been programming about 6 weeks, ive stripped the code back to how it was. So ive put the sound effect in an animate ball method on the gameview and have the audio declared in the .h (i dont have a property for it though)

    .h
    Code:
    AVAudioPlayer *theAudio;
    .m
    Code:
    if (!stopdetection && CGRectIntersectsRect(ball.frame, paddle.frame))
    	{
    		score +=10;
    		scoreLabel.text = [NSString stringWithFormat:@"%d", score];
    		
    		stopdetection = YES;
    		
    		NSString *filePath = [[NSBundle mainBundle] pathForResource:@"short" ofType:@"mp3"];
    		NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:filePath];
    		theAudio= [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:nil];
    		
    		[theAudio play];
    		
    		[filePath release];
    		[fileURL release];
    		
    		SettingsViewController *settings = [[SettingsViewController alloc] initWithNibName:nil bundle:nil];
    		settings.theAudio = theAudio;
    		
    		
    	}
    
    and in the settings view all i have is

    .h

    Code:
    @interface SettingsViewController : UIViewController{
    	
    	AVAudioPlayer *theAudio;
    	IBOutlet UISlider *slider;
    	
    }
    
    @property (nonatomic,retain) AVAudioPlayer *theAudio; 
    .m

    Code:
    @synthesize theAudio;
    - (IBAction)volume{
    	theAudio.volume = slider.value;
    }
    
    - (IBAction) toggleButtonPressed{
    	if(muteSwitch.on){
    		[theAudio play];
    	}
    	else{
    		[theAudio stop];
    		
    	}
    	
    }
    
    Thats all i have.
     
  11. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #11
    I'm sorry but I'm afraid I can't help you anymore. You refrain from answering my simple questions and just keep posting code that shows no attempts to debug or provide any information on what debugging you've actually done. I'm out.
     
  12. SunnyLi thread starter Guest

    Joined:
    Oct 7, 2010
    #12
    Sorry forgot to update this, i was getting way out of my depth so what ive done is just add background music throughout the whole of the application.

    I can control that through the settings page, i understand how that works this other way was just tying me up in knots.
     
  13. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #13
    Hmm, maybe you can help pesto10 then.
     

Share This Page