27
26

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

iTunesみたいに再生中の曲のアートワーク(ジャケット写真)に合わせて背景色と文字色を変える

Last updated at Posted at 2014-02-21

yuch_iさんのiPhoneで再生中の曲情報を取得する方法という記事を読んで、「そういえばちょっと前にiTunesみたいに背景色変えるライブラリ使ったなー」と思ったので、書いておきます。

とは言っても便利なライブラリがあるので、それを使うだけなのですが。。

LEColorPicker
GitHub:https://github.com/luisespinoza/LEColorPicker

pod "LEColorPicker"

使い方はめちゃ簡単です。

LEColorPickerの使い方
LEColorPicker* colorpicker = [LEColorPicker new];
LEColorScheme* scheme = [colorpicker colorSchemeFromImage:[UIImage imageNamed:@"sample.jpg"]];
UIColor* backgroundColor = scheme.backgroundColor;
UIColor* primaryTextColor = scheme.primaryTextColor;
UIColor* secondaryTextColor = scheme.secondaryTextColor;
コード全文
//--------------------------------------------------
#pragma mark - LifeCycle
//--------------------------------------------------
- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    
    _artworkImageView.userInteractionEnabled = YES;
    
    self.player = [MPMusicPlayerController iPodMusicPlayer];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onNowPlayingItemChanged:)
                                                 name:MPMusicPlayerControllerNowPlayingItemDidChangeNotification
                                               object:self.player];
    
    [self.player beginGeneratingPlaybackNotifications];
}

- (void)viewDidUnload
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [self.player endGeneratingPlaybackNotifications];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
    
}

//--------------------------------------------------
#pragma mark - Notification
//--------------------------------------------------
-(void) onNowPlayingItemChanged:(NSNotification*)ntf {
    MPMediaItem *mediaItem = [self.player nowPlayingItem];
    NSInteger mediaType = [[mediaItem valueForProperty:MPMediaItemPropertyMediaType] integerValue];
    if (mediaType == MPMediaTypeMusic) {
        
        // アートワーク(ジャケット写真)
        MPMediaItemArtwork *artwork = [mediaItem valueForProperty:MPMediaItemPropertyArtwork];
        UIImage* artworkImage = [artwork imageWithSize:CGSizeMake(600.0, 600.0)];
        _artworkImageView.image = artworkImage;
        
        LEColorPicker* colorpicker = [LEColorPicker new];
        LEColorScheme* scheme = [colorpicker colorSchemeFromImage:artworkImage];
        
        //背景色
        _backgroundView.backgroundColor = scheme.backgroundColor;
        
        //曲名
        NSString* title = [mediaItem valueForProperty:MPMediaItemPropertyTitle];
        [_titleLabel setText:title];
        [_titleLabel setTextColor:scheme.primaryTextColor];
        
        //アルバム名
        NSString* albumTitle = [mediaItem valueForProperty:MPMediaItemPropertyAlbumTitle];
        [_albumLabel setText:albumTitle];
        [_albumLabel setTextColor:scheme.primaryTextColor];
        
        //リリース日
        NSDate* releaseDate = [mediaItem valueForProperty:MPMediaItemPropertyReleaseDate];
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        [formatter setDateFormat:@"yyyy/MM/dd"];
        NSString *dateStr = [formatter stringFromDate:releaseDate];
        
        [_releaseDateLabel setText:dateStr];
        [_releaseDateLabel setTextColor:scheme.secondaryTextColor];
        
        // アーティスト名
        NSString* artistName = [mediaItem valueForProperty:MPMediaItemPropertyArtist];
        [_artistLabel setText:artistName];
        [_artistLabel setTextColor:scheme.secondaryTextColor];
    }
}

//--------------------------------------------------
#pragma mark - Action
//--------------------------------------------------
#pragma mark === Tap Artwork ===
//--------------------------------------------------
- (IBAction)actionTapImageView:(id)sender
{
    if( [self.player playbackState] == MPMusicPlaybackStatePlaying ){
        [self.player pause];
    }
    else{
        [self.player play];
    }
}

//--------------------------------------------------
#pragma mark === Swipe Right Artwork ===
//--------------------------------------------------
- (IBAction)actionSwipeRightImageView:(id)sender
{
    [self.player skipToPreviousItem];
}

//--------------------------------------------------
#pragma mark === Swipe Left Artwork ===
//--------------------------------------------------
- (IBAction)actionSwipeLeftImageView:(id)sender
{
    [self.player skipToNextItem];
}
27
26
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
27
26

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?