iOS Developer Library

Developer

AVFoundation Framework Reference AVAudioPlayerDelegate Protocol Reference

Options
Deployment Target:

On This Page
Language:

AVAudioPlayerDelegate

Inheritance


Not Applicable

Import Statement


Swift

import AVFoundation

Objective-C

@import AVFoundation;

Availability


Available in iOS 2.2 and later.

The delegate of an AVAudioPlayer object must adopt the AVAudioPlayerDelegate protocol. All of the methods in this protocol are optional. They allow a delegate to respond to audio interruptions and audio decoding errors, and to the completion of a sound’s playback.

  • Called when a sound has finished playing.

    Declaration

    Swift

    optional func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer!, successfully flag: Bool)

    Objective-C

    - (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag

    Parameters

    player

    The audio player that finished playing.

    flag

    YEStrue on successful completion of playback; NOfalse if playback stopped because the system could not decode the audio data.

    Discussion

    This method is not called upon an audio interruption. Rather, an audio player is paused upon interruption—the sound has not finished playing.

    Import Statement

    Objective-C

    @import AVFoundation;

    Swift

    import AVFoundation

    Availability

    Available in iOS 2.2 and later.

  • Called when an audio player is interrupted, such as by an incoming phone call.

    Declaration

    Swift

    optional func audioPlayerBeginInterruption(_ player: AVAudioPlayer!)

    Objective-C

    - (void)audioPlayerBeginInterruption:(AVAudioPlayer *)player

    Parameters

    player

    The audio player that has been interrupted.

    Discussion

    Upon interruption, your application’s audio session is deactivated and the audio player pauses. You cannot use the audio player again until you receive a notification that the interruption has ended.

    Import Statement

    Objective-C

    @import AVFoundation;

    Swift

    import AVFoundation

    Availability

    Available in iOS 2.2 and later.

    Deprecated in iOS 8.0.

  • Called after your audio session interruption ends, with options indicating the state of the audio session.

    Declaration

    Swift

    optional func audioPlayerEndInterruption(_ player: AVAudioPlayer!, withOptions flags: Int)

    Objective-C

    - (void)audioPlayerEndInterruption:(AVAudioPlayer *)player withOptions:(NSUInteger)flags

    Parameters

    player

    The audio player whose interruption has ended.

    flags

    Options indicating the state of the audio session when this method is called. Options are described in AVAudioSessionInterruptionOptions.

    Discussion

    When an interruption ends, such as by a user ignoring an incoming phone call, the audio session for your application is automatically reactivated; at that point you can again interact with the audio player. To resume playback, call the play method.

    If this delegate method receives the AVAudioSessionInterruptionOptionShouldResume constant in its options parameter, the audio session is immediately ready to be used.

    Import Statement

    Objective-C

    @import AVFoundation;

    Swift

    import AVFoundation

    Availability

    Available in iOS 6.0 and later.

    Deprecated in iOS 8.0.

  • Called after your audio session interruption ends.

    Declaration

    Objective-C

    - (void)audioPlayerEndInterruption:(AVAudioPlayer *)player

    Parameters

    player

    The audio player whose interruption has ended.

    Discussion

    If you implement the preferred audioPlayerEndInterruption:withFlags: method, it will be called instead of this one.

    When an interruption ends, such as by a user ignoring an incoming phone call, the audio session for your application is automatically reactivated; at that point you can again interact with the audio player. To resume playback, call the play method.

    Import Statement

    Objective-C

    @import AVFoundation;

    Availability

    Available in iOS 2.2 and later.

    Deprecated in iOS 6.0.

  • Called after your audio session interruption ends, with flags indicating the state of the audio session.

    Declaration

    Objective-C

    - (void)audioPlayerEndInterruption:(AVAudioPlayer *)player withFlags:(NSUInteger)flags

    Parameters

    player

    The audio player whose interruption has ended.

    flags

    Flags indicating the state of the audio session when this method is called. Flags are described in Interruption Flags.

    Discussion

    When an interruption ends, such as by a user ignoring an incoming phone call, the audio session for your application is automatically reactivated; at that point you can again interact with the audio player. To resume playback, call the play method.

    If this delegate method receives the AVAudioSessionInterruptionFlags_ShouldResume constant in its flags parameter, the audio session is immediately ready to be used.

    If you implement this method, the system does not call the audioPlayerEndInterruption: method.

    Import Statement

    Objective-C

    @import AVFoundation;

    Availability

    Available in iOS 4.0 and later.

    Deprecated in iOS 6.0.