Home » Apple » iPhone » Hello Music Player

Hello Music Player

Here is a bare bones hello world-style example that demonstrates library access and music playback. In a few minutes you can have a working, if minimal, music player. Lacking a user interface, this code queues up the entire iPod library and starts playing immediately on launch.

Note: To follow these steps you’ll need a provisioned device because the Simulator has no access to a device’s iPod library.

  1. Create a new Xcode project.

    In Xcode, create a new project using the Window-based Application template. In the project window, add the MediaPlayer framework to the Frameworks group. Save the project.

  2. Import the umbrella header file for the Media Player framework.

    Add the following line to the AppDelegate.h file, after the existing #import line:

    1
    #import
  3. Add code to create a music player, assign it music to play, and start playback.

    Open the project’s AppDelegate.m implementation file. Before the end of the applicationDidFinishLaunching: block, add the three lines of code shown in Listing 1-1.

    Listing 1-1 A very bare-bones music player

    1
    2
    3
    4
    5
    6
    7
    8
    // instantiate a music player
    MPMusicPlayerController *myPlayer = [MPMusicPlayerController applicationMusicPlayer];

    // assign a playback queue containing all media items on the device
    [myPlayer setQueueWithQuery: [MPMediaQuery songsQuery]];

    // start playing from the beginning of the queue
    [myPlayer play];

Now, configure your project appropriately for your development device, which includes setting the code-signing identity and the bundle identifier. Also, ensure that the device has at least one song in its iPod library. Build and run the project. When the application launches on the device, the first song in the iPod library starts playing. The player continues to play through all the items in the iPod library or until you quit the application.

About Music Player Change Notifications

To keep track of what a music player is doing, you register for music player change notifications. This is essential for ensuring that your application’s state and the music player’s state are well coordinated.

For example, here is the sequence of events for correctly starting up music playback. Notice that, because music players operate on their own threads, you do not update your user interface until receiving the appropriate notification.

  1. A user taps Play.
  2. Your application invokes playback on the music player.
  3. The music player starts playing and issues a playback-state-change notification.
  4. Your application receives the notification and queries the music player’s state, confirming that it is indeed playing.
  5. Your application updates its user interface accordingly—perhaps changing the Play button to say Pause.

Music player change notifications support keeping track of playback state, the now-playing item, and the music player’s playback volume. “Using Media Playback” explains how to use them.

Comments are closed.