1

我正在流式传输视频文件,但仅使用 AVPlayer 为其播放音频。我成功地能够播放/暂停,在后台继续播放,并与锁定屏幕控件交互以控制播放器。唯一的问题是,如果我暂停音频然后锁定手机,现在会显示锁定屏幕控件。但是,如果我从锁定屏幕暂停音频,控件仍会显示。

代码库很广泛,所以这里是一些相关的代码。

-(void)viewDidLoad {
    ...
    [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error: nil];
    [[AVAudioSession sharedInstance] setActive:YES error:nil];
    [[AVAudioSession sharedInstance] setMode:AVAudioSessionModeMoviePlayback error:nil];
    [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
    ...
}
-(void) setUpAvPlayer {
    ...
    self.player = [AVPlayer playerWithPlayerItem:[AVPlayerItem playerItemWithAsset:mixComposition]];
    self.player.automaticallyWaitsToMinimizeStalling = NO;
    [self addObservers];
    [self setUpRemoteCommandCenter];
    [self.player.currentItem seekToTime:CMTimeMakeWithSeconds(videoSeekPosition, 60000)];
    ...
}

-(void)setUpRemoteCommandCenter {
    // Provides all audio data to be displayed to user in lock screen
    self.lockScreenInfo = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
                           lecture.topicName, MPMediaItemPropertyTitle,
                           [NSNumber numberWithDouble:CMTimeGetSeconds(self.player.currentItem.duration)], MPMediaItemPropertyPlaybackDuration,
                           [NSNumber numberWithDouble:CMTimeGetSeconds(self.player.currentItem.currentTime)], MPNowPlayingInfoPropertyElapsedPlaybackTime,
                           [NSNumber numberWithDouble:playerRate], MPNowPlayingInfoPropertyPlaybackRate, nil];
    [[MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:self.lockScreenInfo];

    // For lock screen & remote audio controls
    MPRemoteCommandCenter *remoteCommandCenter = [MPRemoteCommandCenter sharedCommandCenter];

    [remoteCommandCenter.playCommand setEnabled:YES];
    [remoteCommandCenter.playCommand addTarget:self action:@selector(playPauseVideo)];

    [remoteCommandCenter.pauseCommand setEnabled:YES];
    [remoteCommandCenter.pauseCommand addTarget:self action:@selector(playPauseVideo)];

    [remoteCommandCenter.skipBackwardCommand setEnabled:YES];
    [remoteCommandCenter.skipBackwardCommand addTarget:self action:@selector(skipBackward)];
    remoteCommandCenter.skipBackwardCommand.preferredIntervals = @[@(15)];

    [remoteCommandCenter.skipForwardCommand setEnabled:YES];
    [remoteCommandCenter.skipForwardCommand addTarget:self action:@selector(skipForward)];
    remoteCommandCenter.skipForwardCommand.preferredIntervals = @[@(15)];

    // Drag slider to change audio position
    // Check for iOS version here (later than iOS 9.0)
    if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_9_0) {
        [remoteCommandCenter.changePlaybackPositionCommand setEnabled:YES];
        [remoteCommandCenter.changePlaybackPositionCommand addTarget:self action:@selector(changedThumbSliderOnLockScreen:)];
    }
}

更新:我在评论中采纳了 Matt 的建议,现在我使用 mp3 文件来播放音频。我仍然遇到同样的问题,当音频暂停时,锁屏控件不显示。关于如何解决此问题的任何其他想法?正如我在评论中所说,我基本上想模仿 Apple 的 Podcast 应用程序。音频文件是流式传输的(而不是下载的),即使在暂停时也会显示锁定屏幕控件。

4

0 回答 0