iOS Developer Library

Developer

MediaPlayer Framework Reference MPMusicPlayerController Class Reference

Options
Deployment Target:

On This Page
Language:

MPMusicPlayerController

Use an MPMusicPlayerController object, or music player, to play media items from the device iPod library. There are two types of music player: More...

Inheritance


Conforms To


Import Statement


import MediaPlayer @import MediaPlayer;

Availability


Available in iOS 3.0 and later.
  • Returns the application music player.

    Declaration

    Swift

    class func applicationMusicPlayer() -> MPMusicPlayerController!

    Objective-C

    + (MPMusicPlayerController *)applicationMusicPlayer

    Return Value

    The application music player.

    Discussion

    The application music player plays music locally within your app. It does not affect the iPod state. When your app moves to the background, the music player stops if it was playing.

    Import Statement

    import MediaPlayer

    Availability

    Available in iOS 3.0 and later.

  • Returns the iPod music player, which controls the iPod app’s state.

    Declaration

    Swift

    class func iPodMusicPlayer() -> MPMusicPlayerController!

    Objective-C

    + (MPMusicPlayerController *)iPodMusicPlayer

    Return Value

    The iPod music player.

    Discussion

    The iPod music player employs the iPod app on your behalf. On instantiation, it takes on the current iPod app state and controls that state as your app runs. Specifically, the shared state includes the following:

    Other aspects of iPod state, such as the on-the-go playlist, are not shared. Music that is playing continues to play when your app moves to the background.

    Import Statement

    import MediaPlayer

    Availability

    Available in iOS 3.0 and later.

    Deprecated in iOS 8.0.

  • The currently-playing media item, or the media item, within a queue, that you have designated to begin playback with.

    Declaration

    Swift

    @NSCopying var nowPlayingItem: MPMediaItem!

    Objective-C

    @property(nonatomic, copy) MPMediaItem *nowPlayingItem

    Discussion

    To specify that playback should begin at a particular media item in the playback queue, set this property to that item while the music player is stopped or paused.

    If no media item is playing or designated to play, this property’s value is nil.

    If you create an iPod music player and the user plays an item from another library using Home Sharing, the value of this property is nil.

    Import Statement

    import MediaPlayer

    Availability

    Available in iOS 3.0 and later.

  • The index of the now playing item in the current playback queue.

    Declaration

    Swift

    var indexOfNowPlayingItem: Int { get }

    Objective-C

    @property(nonatomic, readonly) NSUInteger indexOfNowPlayingItem

    Discussion

    This property’s value is NSNotFound if the index is not valid—for example, if the playback queue is empty.

    Import Statement

    import MediaPlayer

    Availability

    Available in iOS 5.0 and later.

  • The current playback state of the music player.

    Declaration

    Swift

    var playbackState: MPMusicPlaybackState { get }

    Objective-C

    @property(nonatomic, readonly) MPMusicPlaybackState playbackState

    Discussion

    If you configure a music player as an iPod music player, the playback state matches the playback state of the built-in iPod app. This is true whether the iPod app is using the device iPod library or a home shared library. Note, however, that when the iPod is using a home shared library, the music player’s nowPlayingItem property is nil.

    For the available playback states, see Playback States.

    Import Statement

    import MediaPlayer

    Availability

    Available in iOS 3.0 and later.

  • The current repeat mode of the music player.

    Declaration

    Swift

    var repeatMode: MPMusicRepeatMode

    Objective-C

    @property(nonatomic) MPMusicRepeatMode repeatMode

    Discussion

    For the available repeat modes, see Repeat Modes. If not explicitly set, repeatMode defaults to MPMusicRepeatModeDefault.

    Import Statement

    import MediaPlayer

    Availability

    Available in iOS 3.0 and later.

  • The current shuffle mode of the music player.

    Declaration

    Swift

    var shuffleMode: MPMusicShuffleMode

    Objective-C

    @property(nonatomic) MPMusicShuffleMode shuffleMode

    Discussion

    For the available shuffle modes, see Shuffle Modes. If not explicitly set, shuffleMode defaults to MPMusicShuffleModeDefault.

    Import Statement

    import MediaPlayer

    Availability

    Available in iOS 3.0 and later.

  • volume volume (iOS 7.0) Property

    The audio playback volume for the music player, in the range from 0.0 (silent) through 1.0 (maximum volume).

    Deprecation Statement

    To provide UI for adjusting system playback volume, use the MPVolumeView class, which provides media playback controls that iOS users expect and whose appearance you can customize.

    Declaration

    Objective-C

    @property(nonatomic) float volume

    Import Statement

    Availability

    Available in iOS 3.0 and later.

    Deprecated in iOS 7.0.

  • Starts playback of the next media item in the playback queue; or, the music player is not playing, designates the next media item as the next to be played.

    Declaration

    Swift

    func skipToNextItem()

    Objective-C

    - (void)skipToNextItem

    Discussion

    If already at the last item in the playback queue when this method is called, ends playback.

    Import Statement

    import MediaPlayer

    Availability

    Available in iOS 3.0 and later.

  • Restarts playback at the beginning of the currently playing media item.

    Declaration

    Swift

    func skipToBeginning()

    Objective-C

    - (void)skipToBeginning

    Import Statement

    import MediaPlayer

    Availability

    Available in iOS 3.0 and later.

  • Starts playback of the previous media item in the playback queue; or, the music player is not playing, designates the previous media item as the next to be played.

    Declaration

    Swift

    func skipToPreviousItem()

    Objective-C

    - (void)skipToPreviousItem

    Discussion

    If already at the first item in the playback queue when this method is called, ends playback.

    Import Statement

    import MediaPlayer

    Availability

    Available in iOS 3.0 and later.

  • Values for the playbackState property.

    Declaration

    Swift

    enum MPMusicPlaybackState : Int { case Stopped case Playing case Paused case Interrupted case SeekingForward case SeekingBackward }

    Objective-C

    enum { MPMusicPlaybackStateStopped, MPMusicPlaybackStatePlaying, MPMusicPlaybackStatePaused, MPMusicPlaybackStateInterrupted, MPMusicPlaybackStateSeekingForward, MPMusicPlaybackStateSeekingBackward }; typedef NSInteger MPMusicPlaybackState;

    Constants

    • Stopped

      MPMusicPlaybackStateStopped

      The music player is stopped.

      Available in iOS 3.0 and later.

    • Playing

      MPMusicPlaybackStatePlaying

      The music player is playing.

      Available in iOS 3.0 and later.

    • Paused

      MPMusicPlaybackStatePaused

      The music player is paused.

      Available in iOS 3.0 and later.

    • Interrupted

      MPMusicPlaybackStateInterrupted

      The music player has been interrupted, such as by an incoming phone call.

      Available in iOS 3.0 and later.

    • SeekingForward

      MPMusicPlaybackStateSeekingForward

      The music player is seeking forward.

      Available in iOS 3.0 and later.

    • SeekingBackward

      MPMusicPlaybackStateSeekingBackward

      The music player is seeking backward.

      Available in iOS 3.0 and later.

    Discussion

    You determine a music player’s state by checking the playbackState property. Depending on the property’s value, you can update your application’s user interface or take other appropriate action.

    Import Statement

    import MediaPlayer

    Availability

    Available in iOS 3.0 and later.

  • Values for the repeatMode property.

    Declaration

    Swift

    enum MPMusicRepeatMode : Int { case Default case None case One case All }

    Objective-C

    enum { MPMusicRepeatModeDefault, MPMusicRepeatModeNone, MPMusicRepeatModeOne, MPMusicRepeatModeAll }; typedef NSInteger MPMusicRepeatMode;

    Constants

    • Default

      MPMusicRepeatModeDefault

      The user’s preferred repeat mode.

      Available in iOS 3.0 and later.

    • None

      MPMusicRepeatModeNone

      The music player will not repeat the current song or playlist.

      Available in iOS 3.0 and later.

    • One

      MPMusicRepeatModeOne

      The music player will repeat the current song.

      Available in iOS 3.0 and later.

    • All

      MPMusicRepeatModeAll

      The music player will repeat the current playlist.

      Available in iOS 3.0 and later.

    Import Statement

    import MediaPlayer

    Availability

    Available in iOS 3.0 and later.

  • Values for the shuffleMode property.

    Declaration

    Swift

    enum MPMusicShuffleMode : Int { case Default case Off case Songs case Albums }

    Objective-C

    enum { MPMusicShuffleModeDefault, MPMusicShuffleModeOff, MPMusicShuffleModeSongs, MPMusicShuffleModeAlbums }; typedef NSInteger MPMusicShuffleMode;

    Constants

    • Default

      MPMusicShuffleModeDefault

      The user’s preferred shuffle mode.

      Available in iOS 3.0 and later.

    • Off

      MPMusicShuffleModeOff

      The playlist is not shuffled.

      Available in iOS 3.0 and later.

    • Songs

      MPMusicShuffleModeSongs

      The playlist is shuffled by song.

      Available in iOS 3.0 and later.

    • Albums

      MPMusicShuffleModeAlbums

      The playlist is shuffled by album.

      Available in iOS 3.0 and later.

    Import Statement

    import MediaPlayer

    Availability

    Available in iOS 3.0 and later.