simple audio help

Discussion in 'iPhone/iPad Programming' started by AdamRock, May 14, 2011.

  1. AdamRock, May 14, 2011
    Last edited by a moderator: May 15, 2011

    macrumors 6502a

    AdamRock

    Joined:
    Aug 30, 2010
    Location:
    Toronto
    #1
    i'm currently making an application and one part has a button and when clicked plays a sound, how do i make it that when the user clicks the button it plays, but if they click it and its half way done playing and they click it again it restarts to audio?

    heres the code;
    Code:
    -(IBAction)pushButton6 {
    	NSString *path =[[NSBundle mainBundle]
    					 pathForResource:@"Aah" ofType:@"wav"];
    	AVAudioPlayer* theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
    	theAudio.delegate = self;
    	[theAudio play];
    	
    }
     
  2. macrumors 603

    Joined:
    Jul 29, 2003
    Location:
    Silicon Valley
    #2
    First thing to do is to retain your audio player object using something other than a local variable.

    When you figure out where to retain and how to access that object, you will find that there are other messages you can sent to it listed in the extensive AVAudioPlayer documentation.
     
  3. macrumors 65816

    jnoxx

    Joined:
    Dec 29, 2010
    Location:
    Aartselaar // Antwerp // Belgium
    #3
    Someone asked this last week, and I shared code, not to be rude, but there is a Search button for not duplicating posts too much.
     
  4. thread starter macrumors 6502a

    AdamRock

    Joined:
    Aug 30, 2010
    Location:
    Toronto
    #4
    i actually did use the search feature before making this thread, and the post you made a week ago was not the same problem that i'm having, and there's no other threads on macrumors regarding this.
     
  5. macrumors regular

    nickculbertson

    Joined:
    Nov 19, 2010
    Location:
    Nashville, TN
    #5
    You must release the first sound before you start the new one. When in doubt try youtube.

    here

    Nick
     
  6. thread starter macrumors 6502a

    AdamRock

    Joined:
    Aug 30, 2010
    Location:
    Toronto
    #6
    that video doesn't help me that much...

    i need my 1 button to be able to cease all sounds before playing its new song.

    tried to implement a [player stop]; before the [player play]; but that didn't work.

    i'm starting to think apple likes making things harder then they are?
     
  7. macrumors 603

    Joined:
    Jul 29, 2003
    Location:
    Silicon Valley
    #7
    Which player object did you try to stop? The old one? Or the new one?

    Print out the object pointers (or inspect them in the debugger) to make sure you are using the correct one.
     
  8. macrumors regular

    nickculbertson

    Joined:
    Nov 19, 2010
    Location:
    Nashville, TN
    #8
    The solution is in the video, your just not finding it. I said you need to release the sound, not stop it. (how is the audio released in the video?)
     
  9. macrumors 603

    Joined:
    Jul 29, 2003
    Location:
    Silicon Valley
    #9
    Absolutely.

    Apple assumes that all iPhone developers are fairly intelligent and will carefully read the copious amounts of documentation and details available. Apple then creates their iOS dev materials accordingly.

    It's Apple's pseudo-IQ test to reduce the number of clueless or mediocre developers who manage to create and submit working apps.

    It also means Apple can spend more of their time creating advanced stuff for the good developers to make their apps even better.
     
  10. thread starter macrumors 6502a

    AdamRock

    Joined:
    Aug 30, 2010
    Location:
    Toronto
    #10
    the guy in the vid has 1 button for play 1 for stop, i need 1 button to do both.
     
  11. macrumors 603

    Joined:
    Jul 29, 2003
    Location:
    Silicon Valley
    #11
    Your button needs a model object (or state variable) that can save the current state of activity, or the action to do next. Look up "state machines", "state variables", and "conditional statements in C". Stuff that's mentioned in many basic computer science and programming textbooks.
     
  12. macrumors regular

    nickculbertson

    Joined:
    Nov 19, 2010
    Location:
    Nashville, TN
    #12
    From the video.

    Code:
    -(IBAction)pushButton6 {
    	NSString *path =[[NSBundle mainBundle]
    					 pathForResource:@"Aah" ofType:@"wav"];
            [B]if (theAudio)[theAudio release];[/B]
    	AVAudioPlayer* theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
    	theAudio.delegate = self;
    	[theAudio play];
    	
    }
    
     

Share This Page