2015-04-06 52 views
1

我試圖讓我的播客應用程序在鎖定屏幕上顯示內容,以及尊重播放/暫停控件。這裏是我的代碼:鎖定屏幕不尊重現在信息字典

- (void)playEpisodeAtIndex:(NSInteger)index { 

    _currentIndex = index; 
    TalkPodEpisode *episode = self.playlist[index]; 
    self.player = [[AVPlayer alloc] initWithURL:episode.url]; 
    [self.player play]; 
    _playing = YES; 

    [[UIApplication sharedApplication] beginReceivingRemoteControlEvents]; 

    NSDictionary *lockScreenInfo = @{MPMediaItemPropertyTitle:self.currentEpisode.title, 
           MPMediaItemPropertyArtist: @"The Talk Pod", 
           MPNowPlayingInfoPropertyPlaybackRate:[NSNumber numberWithDouble:self.player.rate], 
              MPNowPlayingInfoPropertyElapsedPlaybackTime:[NSNumber numberWithDouble:[self currentPlaybackTime]]}; 
    [MPNowPlayingInfoCenter defaultCenter].nowPlayingInfo = lockScreenInfo; 


} 

古怪的是,這裏發生了什麼:

  1. 響應和處理事件只是正常
  2. 藝術家和標題顯示在鎖屏
  3. 的進展滑塊顯示 - : - ,並且沒有顯示播放進度
  4. 節目的持續時間也不會出現。

下面是代碼來獲得當前經過時間:

- (NSTimeInterval)currentPlaybackTime { 

    CMTime time = self.player.currentTime; 
    if (CMTIME_IS_VALID(time)) { 

     return time.value/time.timescale; 

    } 

    return 0; 

} 

下面是切換播放&暫停代碼:

- (void)playPause { 

    if (self.player) { 

     if (_playing) { 

      [self.player pause]; 
      _playing = NO; 

     } else { 

      [self.player play]; 
      _playing = YES; 

     } 

     NSMutableDictionary *lockScreenInfo = [[MPNowPlayingInfoCenter defaultCenter].nowPlayingInfo mutableCopy]; 
     lockScreenInfo[MPNowPlayingInfoPropertyPlaybackRate] = [NSNumber numberWithDouble:self.player.rate]; 
     lockScreenInfo[MPNowPlayingInfoPropertyElapsedPlaybackTime] = [NSNumber numberWithDouble:[self currentPlaybackTime]]; 
     [MPNowPlayingInfoCenter defaultCenter].nowPlayingInfo = lockScreenInfo; 

    } 

} 

任何人都知道如何解決這一問題?有什麼令我困惑的是,一些的東西出現在屏幕上,但當前的播放信息沒有。再次,接收和處理事件工作得很好。

回答