天天看點

iOS鎖屏顯示歌曲資訊

  1. 導入頭檔案#import <MediaPlayer/MediaPlayer.h>
  2. 遠端控制事件接收與處理

    - (void)viewWillAppear:(BOOL)animated

    {

    [super viewWillAppear:animated];

    [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];

    [self becomeFirstResponder];

    }

    -(void)viewDidDisappear:(BOOL)animated{

        [[UIApplication sharedApplication] endReceivingRemoteControlEvents];

    [self resignFirstResponder];

    }

    - (void)remoteControlReceivedWithEvent:(UIEvent *)event

    {

    if (event.type == UIEventTypeRemoteControl) {

            switch (event.subtype) {

    case UIEventSubtypeRemoteControlPlay:

                    [self play]; // 播放

                    break;

    case UIEventSubtypeRemoteControlPause:

                    [self pause];//暫停  

                    break;

    case UIEventSubtypeRemoteControlPreviousTrack:

                    [self forwardItem]; // 播放上一曲按鈕

                    break;

    case UIEventSubtypeRemoteControlNextTrack:

                    [self nextItem]; // 播放下一曲按鈕

                    break;

                default:

                    break;

            }

        }

    }

  3. 傳遞資訊到鎖屏狀态下- (void)configPlayingInfo 此方法在播放歌曲與切換歌曲時調用即可

    {

    if (NSClassFromString(@"MPNowPlayingInfoCenter")) {

            if ((lastPlayItem != self.player.currentItem) && !isRepeat) {

                lastPlayItem = self.player.currentItem;

    NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];

                [dict setObject:self.titleLabel.text forKey:MPMediaItemPropertyTitle];//歌曲名設定

                [dict setObject:self.artistLabel.text forKey:MPMediaItemPropertyArtist];//歌手名設定

                [dict setObject:[[MPMediaItemArtwork alloc] initWithImage:self.artwork.image]  forKey:MPMediaItemPropertyArtwork];//專輯圖檔設定

                [dict setObject:[NSNumber numberWithDouble:CMTimeGetSeconds(self.player.currentItem.currentTime)] forKey:MPNowPlayingInfoPropertyElapsedPlaybackTime]; //音樂目前已經播放時間

                [dict setObject:[NSNumber numberWithFloat:1.0] forKey:MPNowPlayingInfoPropertyPlaybackRate];//進度光标的速度 (這個随 自己的播放速率調整,我預設是原速播放)

                [dict setObject:[NSNumber numberWithDouble:CMTimeGetSeconds(self.player.currentItem.duration)] forKey:MPMediaItemPropertyPlaybackDuration];//歌曲總時間設定

                [[MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:dict];

            }

    }

    }

  4. 有幾個注意點是,每次你暫停時需要儲存目前的音樂播放進度和鎖屏下進度光标的速度設定為接近0的數(0.00001),以便下次恢複播放時鎖屏下進度光标位置能正常。如下代碼:NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithDictionary:[[MPNowPlayingInfoCenter defaultCenter] nowPlayingInfo]];

            [dict setObject:[NSNumber numberWithDouble:CMTimeGetSeconds(CMTimeMakeWithSeconds((mSlider.value/timess)*timess, self.player.currentItem.currentTime.timescale))] forKey:MPNowPlayingInfoPropertyElapsedPlaybackTime];

            [[MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:dict];

  5. 這樣就ok了,第一次發帖有很多不足,此上面的mSlider是個UISlider控制播放進度的。