音樂類型APP:如何添加正播放的音樂進度,歌手名,圖片等資訊顯示 到鎖屏和控制中心

來源:互聯網
上載者:User

標籤:

  1. 使用的是豆瓣的音頻播放類
  2. 匯入標頭檔#import <MediaPlayer/MediaPlayer.h>
  3. #import <MediaPlayer/MPNowPlayingInfoCenter.h>
  4. #import <MediaPlayer/MPMediaItem.h>
  5. 遠端控制事件接收與處理
    - (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;
            }
        }
    }
    1. 傳遞資訊到鎖屏狀態下- (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];
             }
      }
      }
    2. 有幾個注意點是,每次你暫停時需要儲存當前的音樂播放進度和鎖屏下進度游標的速度設定為接近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];
    3. 此上面的mSlider是個UISlider控制播放進度的。

音樂類型APP:如何添加正播放的音樂進度,歌手名,圖片等資訊顯示 到鎖屏和控制中心

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.