锁屏界面设置

锁屏界面设置

一 接收远程事件
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions方法中启用远程事件

// 开始接收远程事件
    [application beginReceivingRemoteControlEvents];

此时已经可以看到锁屏界面简单的控制按钮

二 音乐软件需要不停的更新界面信息(歌词)

  • 导入框架#import
  • 写更新方法
- (void)updateLockScreen {
    // MPMediaItemPropertyAlbumTitle
    // MPMediaItemPropertyAlbumTrackCount
    // MPMediaItemPropertyAlbumTrackNumber
    // MPMediaItemPropertyArtist
    // MPMediaItemPropertyArtwork
    // MPMediaItemPropertyComposer
    // MPMediaItemPropertyDiscCount
    // MPMediaItemPropertyDiscNumber
    // MPMediaItemPropertyGenre
    // MPMediaItemPropertyPersistentID
    // MPMediaItemPropertyPlaybackDuration
    // MPMediaItemPropertyTitle


    MPNowPlayingInfoCenter *infoCenter = [MPNowPlayingInfoCenter defaultCenter];
    NSMutableDictionary *info = [NSMutableDictionary dictionary];
    YHMusic *music = self.musics[self.currentMusicIndex];
    // 专辑
    info[MPMediaItemPropertyAlbumTitle] = music.album;
    // 艺术家
    info[MPMediaItemPropertyArtist] = music.singer;
    // 图片
    info[MPMediaItemPropertyArtwork] = [[MPMediaItemArtwork alloc] initWithImage:[UIImage imageNamed:music.image]];
    // 歌曲标题
    info[MPMediaItemPropertyTitle] = music.name;
    
    YHPlayManger *pm = [YHPlayManger shareManger];
    // 当前播放时间
    info[MPNowPlayingInfoPropertyElapsedPlaybackTime] = @(pm.currentTime);
    // 总时长
    info[MPMediaItemPropertyPlaybackDuration] = @(pm.duration);
    
    infoCenter.nowPlayingInfo = info;
}
  • 显示歌词的话 歌词要绘制在图片上 所以要提供绘制图片的方法
// 锁屏界面显示的图片
- (UIImage *) lyricImage {
    YHMusic *music = self.musics[self.currentMusicIndex];
    //  开启图片上下文
    CGSize sreenSize = [UIScreen mainScreen].bounds.size;
    //  获取最短的边
    CGFloat min = MIN(sreenSize.width, sreenSize.height) * 0.8;   
    CGSize size  = CGSizeMake(min, min);
    UIGraphicsBeginImageContext(size);
    
    UIImage *bgImage = [UIImage imageNamed:music.image];
    //  绘图片
    [bgImage drawInRect:CGRectMake(0, 0, min, min)];
    
    //  绘制背景图片
    CGFloat lyricBgImageH = 44;
    CGFloat lyricBgImageY = min - lyricBgImageH;
    UIImage *lyricBgImage = [UIImage imageNamed:@"lock_lyric_mask"];
    [lyricBgImage drawInRect:CGRectMake(0, lyricBgImageY, min, lyricBgImageH)];
    //  绘制歌词
    YHLyric *lyric = self.lyrics[self.currentLyricIndex];
    
    CGFloat lyricH = 21;
    CGFloat lyricY = min - lyricH - 10;
    //  把当前上下文设置为白色
    [[UIColor whiteColor] set];
    NSDictionary *dict = @{NSForegroundColorAttributeName: [UIColor whiteColor],
                           NSFontAttributeName: [UIFont systemFontOfSize:15]
                           };
    [lyric.content drawInRect:CGRectMake(0, lyricY,min, lyricY) withAttributes:dict];
    //  取出上下文中的图片
    UIImage *lyricImage  =  UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return lyricImage;
}
  • 锁屏界面按钮控制
- (void)remoteControlReceivedWithEvent:(UIEvent *)event {
    
    /*
     
     UIEventTypeTouches,  触摸事件
     UIEventTypeMotion,   运动事件(摇一摇)
     UIEventTypeRemoteControl, 远程控制
     
     event.subtype
     
     // for UIEventTypeRemoteControl, available in iOS 4.0
     UIEventSubtypeRemoteControlPlay                 = 100, 播放
     UIEventSubtypeRemoteControlPause                = 101, 暂停
     UIEventSubtypeRemoteControlStop                 = 102, 停止
     UIEventSubtypeRemoteControlTogglePlayPause      = 103, 播放到暂停切换
     UIEventSubtypeRemoteControlNextTrack            = 104, 下一曲
     UIEventSubtypeRemoteControlPreviousTrack        = 105, 上一曲
     UIEventSubtypeRemoteControlBeginSeekingBackward = 106, 快退
     UIEventSubtypeRemoteControlEndSeekingBackward   = 107, 结束快退
     UIEventSubtypeRemoteControlBeginSeekingForward  = 108, 快进
     UIEventSubtypeRemoteControlEndSeekingForward    = 109, 结束快进
     */

    switch (event.subtype) {
        case UIEventSubtypeRemoteControlPlay:
        case UIEventSubtypeRemoteControlPause:
            [self playBtnClick:nil];
            break;
        case UIEventSubtypeRemoteControlNextTrack:
            [self nextBtnClick:nil];
            break;
        case UIEventSubtypeRemoteControlPreviousTrack:
            [self preBtnClick:nil];
            break;
     
        default:
            break;
    }
}

此时 已经实现功能

你可能感兴趣的:(锁屏界面设置)