iOS自定義控制中心音樂播放 鎖屏界面背景圖片

簡單快速的說明一下如何設置控制中心或鎖屏界面的音樂播放控制與圖片定製


1. 設置控制中心 內容

需要使用到類 MPNowPlayingInfoCenter

是一個全局單例 通過 defaultCenter獲得

它只有一個屬性 


// The current now playing info for the center.  // 控制中心當前的信息
// Setting the info to nil will clear it. // 設置爲nil 的時候,會清除控制中心的設置

@property (copy, nullable) NSDictionary<NSString *, id> *nowPlayingInfo;


可以通過設置這個  nowPlayingInfo 來設置

如下:
       

    // 初始化一個可變字典
        NSMutableDictionary *songInfo = [ [NSMutableDictionary alloc] init];
        
        // 初始化一個封面
        MPMediaItemArtwork *albumArt = [ [MPMediaItemArtwork alloc] initWithImage: [UIImage imageNamed:@"about-qqfriends"] ];
        // 設置封面
        [ songInfo setObject: albumArt forKey:MPMediaItemPropertyArtwork ];
        
        // 設置標題
        [ songInfo setObject: @"追夢人" forKey:MPMediaItemPropertyTitle ];
        
        // 設置作者
        [ songInfo setObject: @"作者" forKey:MPMediaItemPropertyArtist ];
        
        // 設置專輯
        [ songInfo setObject: @"唱片集" forKey:MPMediaItemPropertyAlbumTitle ];
        
        // 流派
        [songInfo setObject:@"流派" forKey:MPMediaItemPropertyGenre];
        
        // 設置總時長

        [songInfo setObject:[NSString stringWithFormat:@"%f",_auPlayer.duration] forKey:MPMediaItemPropertyPlaybackDuration];


// 設置

        

        [ [MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:songInfo ];


// 更多設置

        
        
        /**
         *  
         .
         // MPMediaItemPropertyAlbumTitle // 專輯名
         // MPMediaItemPropertyAlbumTrackCount  // 專輯個數
         // MPMediaItemPropertyAlbumTrackNumber // 當前播放的專輯位置
         // MPMediaItemPropertyArtist   // 藝術家
         // MPMediaItemPropertyArtwork  // 封面
         // MPMediaItemPropertyComposer // 作曲家
         // MPMediaItemPropertyDiscCount    // 迪斯科 數量
         // MPMediaItemPropertyDiscNumber   // 當前位置
         // MPMediaItemPropertyGenre    // 流派
         // MPMediaItemPropertyPersistentID // ID
         // MPMediaItemPropertyPlaybackDuration // 後臺播放時長
         // MPMediaItemPropertyTitle    // 標題
         
         // 已經播放的時間
          NSString *const MPNowPlayingInfoPropertyElapsedPlaybackTime NS_AVAILABLE_IOS(5_0); // NSNumber (double)
         
         // 當前正在播放的item 後臺播放的 速率,默認是1.0,如果沒有特殊需要,設置爲1.0
         // The playback rate of the now playing item, with 1.0 representing normal
         // playback. For example, 2.0 would represent playback at twice the normal rate.
         // If not specified, assumed to be 1.0.
         MP_EXTERN NSString *const MPNowPlayingInfoPropertyPlaybackRate NS_AVAILABLE_IOS(5_0); // NSNumber (double)
         
         
         // 默認的後臺播放速率
         MP_EXTERN NSString *const MPNowPlayingInfoPropertyDefaultPlaybackRate NS_AVAILABLE_IOS(8_0); // NSNumber (double)
         
         // 當前播放的item 在 當前播放隊列的位置
         // The index of the now playing item in the application's playback queue.
         // Note that the queue uses zero-based indexing, so the index of the first item
         // would be 0 if the item should be displayed as "item 1 of 10".
         MP_EXTERN NSString *const MPNowPlayingInfoPropertyPlaybackQueueIndex NS_AVAILABLE_IOS(5_0); // NSNumber (NSUInteger)
         
         // 總的播放隊列個數
         // The total number of items in the application's playback queue.
         MP_EXTERN NSString *const MPNowPlayingInfoPropertyPlaybackQueueCount NS_AVAILABLE_IOS(5_0); // NSNumber (NSUInteger)
         
         // 當前播放的章節
         // The chapter currently being played. Note that this is zero-based.
         MP_EXTERN NSString *const MPNowPlayingInfoPropertyChapterNumber NS_AVAILABLE_IOS(5_0); // NSNumber (NSUInteger)
         
         // 總的章節數
         // The total number of chapters in the now playing item.
         MP_EXTERN NSString *const MPNowPlayingInfoPropertyChapterCount NS_AVAILABLE_IOS(5_0); // NSNumber (NSUInteger)
         
         // 可用的語言選項 注意:同時只有一個語言能夠使用
         // A list of available language option groups in the now playing item
         // Only one language option in a given group can be played at once.
         MP_EXTERN NSString *const MPNowPlayingInfoPropertyAvailableLanguageOptions NS_AVAILABLE_IOS(9_0); // NSArrayRef of MPNowPlayingInfoLanguageOptionGroup
         
         // 當前播放的語言選項
         // A list of currently active language options in the now playing item.
         MP_EXTERN NSString *const MPNowPlayingInfoPropertyCurrentLanguageOptions NS_AVAILABLE_IOS(9_0); // NSArray of MPNowPlayingInfoLanguageOption
         .
         .
         .
        */

2. 監控和設置控制中心的按鈕響應  MPRemoteCommandCenter

全局單例  通過 [MPRemoteCommandCenter sharedCommandCenter] 獲得

舉例:

監控暫停按鈕:

MPRemoteCommand * pause = [[MPRemoteCommandCenter sharedCommandCenter] pauseCommand] ;
    pause.enabled = YES;
    // 注意 只有添加響應時間之後,在控制中心纔會顯示暫停按鈕
    // 建議:暫停和播放按鈕都要添加事件
    [pause addTarget:self action:@selector(pause:)];


-(void)pause:(MPRemoteCommandEvent*)comm{
    [_auPlayer pause];
    NSLog(@"暫停");
}


同理:

@property (nonatomic, readonly) MPRemoteCommand *pauseCommand;// 暫停
@property (nonatomic, readonly) MPRemoteCommand *playCommand;// 開始
@property (nonatomic, readonly) MPRemoteCommand *stopCommand;// 停止
@property (nonatomic, readonly) MPFeedbackCommand *likeCommand;// 喜歡
@property (nonatomic, readonly) MPFeedbackCommand *dislikeCommand;// 不喜歡


// Previous/Next Track Commands
@property (nonatomic, readonly) MPRemoteCommand *nextTrackCommand;// 下一首歌
@property (nonatomic, readonly) MPRemoteCommand *previousTrackCommand; // 上一首歌


等等。。。。。。。。。。。。


擴展:MPRemoteCommand 遠程響應

@property (nonatomic, assign, getter = isEnabled) BOOL enabled; // 是否可用

- (void)addTarget:(id)target action:(SEL)action; // 添加事件
- (void)removeTarget:(id)target action:(nullable SEL)action ; // 移除事件
- (void)removeTarget:(nullable id)target;




以下的類都是 MPRemoteCommand 的子類

MPFeedbackCommand;簡介

@property (nonatomic, assign, getter = isActive) BOOL active; // 是否激活

@property (nonatomic, copy) NSString *localizedTitle; // 標題

@property (nonatomic, copy) NSString *localizedShortTitle // 標題簡寫



MPRatingCommand 簡介

@property (nonatomic, assign) float minimumRating; // 最低速率


@property (nonatomic, assign) float maximumRating; // 最高速率

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章