Play sound after screen loads.

Discussion in 'iOS Programming' started by KardelSharpeye, Jun 2, 2009.

  1. KardelSharpeye macrumors member

    KardelSharpeye

    Joined:
    Apr 28, 2009
    #1
    Good morning all,

    I am trying to play the intro sound for an application. However the sounds always play before the first screen ever loads. Even if I execute the play sound function after I have load/initialize all my views. Is there any event I can listen to like AS3.0 ( Event.addedToStage)? or anyway I can do so that the sound plays after the intro screen is visible to user?

    Thanks all.
     
  2. johnnyjibbs macrumors 68030

    johnnyjibbs

    Joined:
    Sep 18, 2003
    Location:
    London, UK
    #2
    Put your code in the viewDidAppear method of your view controller where you want to play the sound. It gets called immediately after the view has appeared and become visible.
     
  3. KardelSharpeye thread starter macrumors member

    KardelSharpeye

    Joined:
    Apr 28, 2009
    #3
    yeh so here is my code

    Code:
    - (void)viewDidLoad {
    myView = [[MyView alloc] initWithFrame:CGRectMake(0, 0, 320, 480) viewController:self];
    
    [self.view addSubview:myView];
    
    [introSound play];
    }
    
    and i also tried viewDidAppear but if i tell it to play the sound in here i get nothing...
     
  4. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
  5. KardelSharpeye thread starter macrumors member

    KardelSharpeye

    Joined:
    Apr 28, 2009
    #5
    haha yeah sorry i saw that...but yeh i managed to get the sound play in viewDidAppear but it is still a second early before myView is visible.

    Code:
    - (void)viewDidAppear:(BOOL)animated {
       [introSound play];
    }
    
     
  6. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #6
    Instead of playing the sound in the viewDidAppear method of the superview, why not try playing it in the viewDidAppear method of the MyView?
     
  7. KardelSharpeye thread starter macrumors member

    KardelSharpeye

    Joined:
    Apr 28, 2009
    #7
    hmm yeh i tried that i think its only called in the view controller.
     
  8. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #8
    And that's still too early? Maybe add a delay via an NSTimer?
     
  9. KardelSharpeye thread starter macrumors member

    KardelSharpeye

    Joined:
    Apr 28, 2009
    #9
    yeh i guess this is the only way. thanks guys.
     

Share This Page