3

同様の機能(アプリのバックグラウンドでユーザーが選択した音楽のキューを再生する)を追加できるように探しているので、xcodeでリンゴのaddMusicサンプルアプリで遊んでいます。

組み込みの MediaPicker で曲を選択した後、ユーザーが [完了] ボタンを押したときに、音楽プレーヤーが一時的に停止してから再び再生するという、この厄介なことを行います。- これは、新しい配列をキューとして適用し、現在再生中の状態を中断した場所にリセットする、apple が使用したメソッドによるものだと思います (そのように:)

        // apply the new media item collection as a playback queue for the music player
        [self setUserMediaItemCollection: mediaItemCollection];
        [musicPlayer setQueueWithItemCollection: userMediaItemCollection];
        [self setPlayedMusicOnce: YES];
        [musicPlayer play];

        // Obtain the music player's state so it can then be
        //      restored after updating the playback queue.
    } else {

        // Take note of whether or not the music player is playing. If it is
        //      it needs to be started again at the end of this method.
        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]];

        // Apply the new media item collection as a playback queue for the music player.
        [musicPlayer setQueueWithItemCollection: userMediaItemCollection];

        // Restore the now-playing item and its current playback time.
        musicPlayer.nowPlayingItem          = nowPlayingItem;
        musicPlayer.currentPlaybackTime     = currentPlaybackTime;

        // If the music player was playing, get it playing again.
        if (wasPlaying) {
            [musicPlayer play];

        }
    }


}
}

// If the music player was paused, leave it paused. If it was playing, it will continue to
//      play on its own. The music player state is "stopped" only if the previous list of songs
//      had finished or if this is the first time the user has chosen songs after app
//      launch--in which case, invoke play.

- (void) restorePlaybackState {

if (musicPlayer.playbackState == MPMusicPlaybackStateStopped && userMediaItemCollection) {

    if (playedMusicOnce == NO) {

        [self setPlayedMusicOnce: YES];
        [musicPlayer play];
    }
}

}

誰でもこの問題を経験しましたか? 誰でも考えられる代替/より効率的な方法はありますか?

事前に共有していただきありがとうございます:)

4

1 に答える 1

0

Music Player の変更通知の時点で MPMediaItemCollection に変更を適用するだけです。

于 2012-11-11T11:25:56.007 に答える