Stop a sound file on second view....

Discussion in 'iOS Programming' started by hodgey87, Oct 5, 2010.

  1. hodgey87 Guest

    Joined:
    Mar 20, 2009
    #1
    Hi everyone,

    Im having some trouble with AVAudioPlayer, i have 2 views in my application and some background music that runs at the same time. I want to be able to stop the music on the second view only but not sure how to do it.

    Ive only been doing this for a few weeks so be easy on me :)

    Cheers
     
  2. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    First, do you know the method to stop the music playing?

    Second, how are you presenting the second view?
     
  3. hodgey87 thread starter Guest

    Joined:
    Mar 20, 2009
    #3
    On my first view i have

    .h

    .m

    second view

    .h

    .m


    I can stop it fine if its just on the 1st view, but not sure about on the second one.

    Its probably something simple, but im not sure where to look.
     
  4. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #4
    secondViewController has no reference to countdownViewController's AVAudioPlayer (i.e. theAudio).
     
  5. hodgey87 thread starter Guest

    Joined:
    Mar 20, 2009
    #5
    where do i need to add somethin in? is it the view did load method something like.

    but that just plays 2 songs. Ive got the AVAudioPlayer *theAudio on the .h

    Is it something in the backbuttonpressed method?

     
  6. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #6
    Since you're new to this, let me ask: Have you encountered anything about sharing objects between classes in your learning so far?
     
  7. hodgey87 thread starter Guest

    Joined:
    Mar 20, 2009
    #7
    I havent yet, do i need to add extra code into the appdelegate or something like that?
     
  8. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #8
    That's one way to do it (although not really recommended). Another way is to make secondViewController's theAudio a property and then set it to refer to countdownViewController's theAudio before you present the modal view.
     
  9. hodgey87 thread starter Guest

    Joined:
    Mar 20, 2009
    #9
    ok, so i create a property on the 1st view controller like

    .m

    something like that?
     
  10. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #10
    Except I said to make secondViewController's theAudio a property.

    P.S. The passTheAudio method is unnecessary and not part of creating a property.
     
  11. hodgey87 thread starter Guest

    Joined:
    Mar 20, 2009
    #11
    this is what i have then

    .h

    .m

    anything else i need to add at all?
     
  12. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #12
    First, be a good memory-citizen and don't forget to release your property's ivar in the dealloc.

    Second, since theAudio is now a property, you should use the getter when referring to it.

    Third, don't forget to set the value of theAudio property from the secondViewController instance before your present the view for it (i.e. a change in countdownViewController is still needed).
     
  13. hodgey87 thread starter Guest

    Joined:
    Mar 20, 2009
    #13
    right theAudio is released, i dont understand the last bit though :eek:
     
  14. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #14
    The reason we created a property for theAudio in secondViewController is so that you could set it once you have an instance of secondViewController (which, in your case, is named second). This, then, permits both classes to point to the same object. But, the key is: you need to assign the property a value, after you instantiate secondViewController but before you present second's view.

    P.S. Just a word of advice: Normal naming convention is to start class names with an upper-case character (as in, SecondViewController) rather than a lower-case character (as in, secondViewController) so as to differentiate the class names from the variable names.
     
  15. hodgey87 thread starter Guest

    Joined:
    Mar 20, 2009
    #15
    This is slightly confusing now, i cant find many examples on what to do here. Any chance of something example coding?
     
  16. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #16
    Here's an example of setting a property of an instance to a value (from your own code!):
    Code:
    second.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    second is the instance.
    modalTransitionStyle is the property.
    UIModalTransitionStyleFlipHorizontal is the value.

    If this seems confusing, perhaps it's time to step back from the real coding and go (re)learn the basics of Objective-C programming. Speaking of which, what/where have you learned from already?
     
  17. hodgey87 thread starter Guest

    Joined:
    Mar 20, 2009
    #17
    Cheers i know its frustrating :) I'm currently learning at university now, my books arrived today.

    ive added this code

    seems to work.
     

Share This Page