AVAudioPlayer - how to show stop-button?

Discussion in 'iOS Programming' started by Danneman101, Mar 7, 2009.

  1. macrumors 6502

    Joined:
    Aug 14, 2008
    #1
    How do you display controls for the AVAudioPlayer. I just want the stop-button at the bottom of the screen, but my code displays no controls at all. Ive been googling like a maniac for this, but nothing.

    Code:
    NSString *path = [[NSBundle mainBundle] pathForResource:soundPathName ofType:@"mp3"];   
    AVAudioPlayer *theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];   
    theAudio.delegate = self;   
    [theAudio play];   
    
     
  2. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    I believe you need to provide your own controls and then have them call AVAudioPlayer's playback control methods. This is all described in more detail in the AVAudioPlayer Class Reference.
     
  3. macrumors regular

    Joined:
    Jun 18, 2008
    #3
    Add a button to your view and and an IBAction to your viewController, connect them, and then call [theAudio stop] in your action method.
     
  4. thread starter macrumors 6502

    Joined:
    Aug 14, 2008
    #4
    Ok, thanks. Theyll probably add an interface in some comming version, I guess.
     
  5. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #5
    Doubtful. It's meant more as an audio engine than a UI interface. Hence, the lack of the word 'Controller' in the class name. If they want to add an interface component, they will probably add a new AVAudioPlayerController class to the API.
     
  6. macrumors 6502a

    Joined:
    Jul 17, 2008
    Location:
    Seattle, WA
    #6
    Super easy:

    Code:
    [I]In .h[/I]
    - (IBAction)stopMusic;
    
    [I]In .m[/I]
    - (IBAction)stopMusic {
       [theAudio stop];
    }
    
    Connect the button to the stopMusic action method in Interface Builder.

    Ethan
     

Share This Page