iPod Library Access Programming Guide 2 -- Media Playback

本文详细介绍了如何使用音乐播放器,包括注册接收音乐播放器通知、创建配置音乐播放器、设置播放队列以及控制播放流程等步骤。通过实例代码展示了如何实现这些功能。

Using Media Playback

To play songs, you use a music player—an instance of the MPMusicPlayerController class. In a nutshell, the steps to perform are as follows:

  1. Register to receive music player notifications, and turn on notifications
  2. Create a music player
  3. Set up a playback queue for the music player
  4. Configure the playback options
  5. Invoke playback

Registering for Music Player Notifications

If you provide music player playback control, or if you display information about the now-playing item, you must register for music player notifications 


Listing 2-1  Registering for and activating music player notifications
NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
 
[notificationCenter
    addObserver: self
    selector:    @selector (handle_NowPlayingItemChanged:)
    name:        MPMusicPlayerControllerNowPlayingItemDidChangeNotification
    object:      musicPlayer];
 
[notificationCenter
    addObserver: self
    selector:    @selector (handle_PlaybackStateChanged:)
    name:        MPMusicPlayerControllerPlaybackStateDidChangeNotification
    object:      musicPlayer];
 
[musicPlayer beginGeneratingPlaybackNotifications];

Before deallocating a music player, unregister for notifications and then turn them off as shown in Listing 2-2.

Listing 2-2  Unregistering and deactivating music player notifications
[[NSNotificationCenter defaultCenter]
    removeObserver: self
    name:           MPMusicPlayerControllerNowPlayingItemDidChangeNotification
    object:         musicPlayer];
 
[[NSNotificationCenter defaultCenter]
    removeObserver: self
    name:           MPMusicPlayerControllerPlaybackStateDidChangeNotification
    object:         musicPlayer];
 
[musicPlayer endGeneratingPlaybackNotifications];

Creating and Configuring a Music Player

Listing 2-3  Creating an application music player
MPMusicPlayerController* appMusicPlayer =
    [MPMusicPlayerController applicationMusicPlayer];
 
[appMusicPlayer setShuffleMode: MPMusicShuffleModeOff];
[appMusicPlayer setRepeatMode: MPMusicRepeatModeNone];

Listing 2-4  Creating an iPod music player
MPMusicPlayerController* iPodMusicPlayer =
    [MPMusicPlayerController iPodMusicPlayer];
 
if ([iPodMusicPlayer nowPlayingItem]) {
    // Update the UI (artwork, song name, volume indicator, etc.)
    //        to reflect the iPod state
}

Setting Up a Playback Queue

There are two main ways to set up a playback queue for a music player:

  • Use a media item collection
  • Use a media query, which implicitly defines a collection

You get a collection by using the database access classes or by employing the media item picker. Either way, applying the collection to a music player involves a single method call, shown here:

[musicPlayer setQueueWithItemCollection: userMediaItemCollection];

Alternatively, you can set up a queue using a media query—which, in turn, implicitly defines a collection, as shown here:

[musicPlayer setQueueWithQuery: [MPMediaQuery songsQuery]];
Listing 2-5  Setting up a playback queue in context
- (void) updateQueueWithCollection: (MPMediaItemCollection *) collection {
 
    // Add 'collection' to the music player's playback queue, but only if
    //    the user chose at least one song to play.
    if (collection) {
 
        // If there's no playback queue yet...
        if (userMediaItemCollection == nil) {
            [self setUserMediaItemCollection: collection];
            [musicPlayer setQueueWithItemCollection: userMediaItemCollection];
            [musicPlayer play];
 
        // Obtain the music player's state so it can be restored after
        //    updating the playback queue.
        } else {
            BOOL wasPlaying = NO;
            if (musicPlayer.playbackState == MPMusicPlaybackStatePlaying) {
                wasPlaying = YES;
            }
 
            // Save the now-playing item and its current playback time.
            MPMediaItem *nowPlayingItem        = musicPlayer.nowPlayingItem;
            NSTimeInterval currentPlaybackTime = musicPlayer.currentPlaybackTime;
 
            // Combine the previously-existing media item collection with
            //    the new one
            NSMutableArray *combinedMediaItems =
                [[userMediaItemCollection items] mutableCopy];
            NSArray *newMediaItems = [mediaItemCollection items];
            [combinedMediaItems addObjectsFromArray: newMediaItems];
 
            [self setUserMediaItemCollection:
                [MPMediaItemCollection collectionWithItems:
                    (NSArray *) combinedMediaItems]];
 
            [musicPlayer setQueueWithItemCollection: userMediaItemCollection];
 
            // Restore the now-playing item and its current playback time.
            musicPlayer.nowPlayingItem      = nowPlayingItem;
            musicPlayer.currentPlaybackTime = currentPlaybackTime;
 
            if (wasPlaying) {
                [musicPlayer play];
            }
        }
    }
}


Controlling Playback

In addition, the currentPlaybackTime property is read/write; you can use it to set the playback point within the now-playing item’s timeline.

using a slider to let the user set the playback point.

- (IBAction) setTimelinePosition: (id) sender {
    [musicPlayer setCurrentPlaybackTime: [timelineSlider value]];
}





评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值