我想知道在MPMusicPlayerController中歌曲发生变化时你会更新UI。例如,如果您有包含专辑封面的图像视图,但是用户按下跳过按钮,您将如何使用新专辑封面更新图像视图的图像?
答案 0 :(得分:4)
为了控制音乐播放,我们使用MPMusicPlayerController的一个实例。有两种类型的音乐播放器。 iPodMusicPlayer是对iPod应用程序使用的音乐播放器实例的引用。您更改的任何设置(例如随机播放或重复播放模式)也将在iPod应用中更改。如果在应用程序启动时正在播放iPod,则音乐将继续播放,您可以访问当前歌曲并在当前活动的播放列表中前后跳过。当您的应用退出时,音乐将继续播放。我想这种模式对于大多数试图通过与iPod交互来改善您的音乐聆听体验的应用程序应用程序非常方便。 相比之下,applicationMusicPlayer为您提供了一个音乐播放器,其设置可以独立于iPod应用进行更改。如果您的应用程序是游戏并且您希望让用户能够从其库中选择背景音乐,那么这可能就是您的选择。在Songtext中,我们将使用iPodMusicPlayer,因为我们想知道我们的应用程序启动时正在播放哪首歌曲:
@property (nonatomic, strong) MPMusicPlayerController *musicPlayer;
self.musicPlayer = [MPMusicPlayerController iPodMusicPlayer];
音乐播放器使用通知来通知您有关以下情况的更改: - 当前歌曲(MPMusicPlayerControllerNowPlayingItemDidChangeNotification), - 播放/暂停/停止状态(MPMusicPlayerControllerPlaybackStateDidChangeNotification),或者 - 音量(MPMusicPlayerControllerVolumeDidChangeNotification)。 因此,您通常要做的下一件事就是将自己注册为您感兴趣的通知的观察者,例如:在viewDidLoad中。我们希望收到所有3个通知:
// Register for music player notifications
NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
[notificationCenter addObserver:self
selector:@selector(handleNowPlayingItemChanged:)
name:MPMusicPlayerControllerNowPlayingItemDidChangeNotification
object:self.musicPlayer];
[notificationCenter addObserver:self
selector:@selector(handlePlaybackStateChanged:)
name:MPMusicPlayerControllerPlaybackStateDidChangeNotification
object:self.musicPlayer];
[notificationCenter addObserver:self
selector:@selector(handleExternalVolumeChanged:)
name:MPMusicPlayerControllerVolumeDidChangeNotification
object:self.musicPlayer];
[self.musicPlayer beginGeneratingPlaybackNotifications];
当媒体库的内容发生变化时,iPod媒体库会发送另一个相关的通知,例如:当您将设备与iTunes同步时。如果您的应用创建了需要在库更改后更新的播放列表,则必须收听此通知。为此,请将自己注册为MPMediaLibraryDidChangeNotification通知的观察者并致电:
[[MPMediaLibrary defaultMediaLibrary] beginGeneratingLibraryChangeNotifications]
通知处理程序是您更新UI以响应玩家状态变化的地方:
//当正在播放的项目发生变化时,更新歌曲信息标签和艺术作品显示。 - (void)handleNowPlayingItemChanged:(id)notification { //向音乐播放器询问当前歌曲。 MPMediaItem * currentItem = self.musicPlayer.nowPlayingItem;
// Display the artist, album, and song name for the now-playing media item.
// These are all UILabels.
self.songLabel.text = [currentItem valueForProperty:MPMediaItemPropertyTitle];
self.artistLabel.text = [currentItem valueForProperty:MPMediaItemPropertyArtist];
self.albumLabel.text = [currentItem valueForProperty:MPMediaItemPropertyAlbumTitle];
// Display album artwork. self.artworkImageView is a UIImageView.
CGSize artworkImageViewSize = self.artworkImageView.bounds.size;
MPMediaItemArtwork *artwork = [currentItem valueForProperty:MPMediaItemPropertyArtwork];
if (artwork != nil) {
self.artworkImageView.image = [artwork imageWithSize:artworkImageViewSize];
} else {
self.artworkImageView.image = nil;
}
}
// When the playback state changes, set the play/pause button appropriately.
- (void)handlePlaybackStateChanged:(id)notification {
MPMusicPlaybackState playbackState = self.musicPlayer.playbackState;
if (playbackState == MPMusicPlaybackStatePaused || playbackState == MPMusicPlaybackStateStopped) {
[self.playPauseButton setTitle:@"Play" forState:UIControlStateNormal];
} else if (playbackState == MPMusicPlaybackStatePlaying) {
[self.playPauseButton setTitle:@"Pause" forState:UIControlStateNormal];
}
}
// When the volume changes, sync the volume slider
- (void)handleExternalVolumeChanged:(id)notification {
// self.volumeSlider is a UISlider used to display music volume.
// self.musicPlayer.volume ranges from 0.0 to 1.0.
[self.volumeSlider setValue:self.musicPlayer.volume animated:YES];
}
答案 1 :(得分:0)
如果您正在寻找锁屏播放信息:
您需要传递一个NSDictionary
,例如:
-(void)updateNowPlayingInfo
{
//Set Values for MPNowPlayingInfoCenter
NSArray *keys = [NSArray arrayWithObjects:
MPMediaItemPropertyTitle,
MPMediaItemPropertyPlaybackDuration,
MPNowPlayingInfoPropertyPlaybackRate,
MPNowPlayingInfoPropertyElapsedPlaybackTime,
nil];
NSArray *values = [NSArray arrayWithObjects:
self.currentPlayingVideo.title,
[NSNumber numberWithFloat:self.currentPlayingVideo.duration],
[NSNumber numberWithInt:1],
[NSNumber numberWithDouble:self.currentVideoPlayView.videoPlayerViewController.moviePlayer.currentPlaybackTime],
nil];
NSDictionary *mediaInfo = [NSDictionary dictionaryWithObjects:values forKeys:keys];
[[MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:mediaInfo];
}