首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AVPlayerLayer锁屏控件

AVPlayerLayer锁屏控件
EN

Stack Overflow用户
提问于 2020-01-27 22:35:37
回答 1查看 41关注 0票数 0

我正在使用AVPlayerLayer播放视频,并在应用程序进入后台时播放视频的音频。有没有什么办法可以在屏幕锁定时管理控件。用户想要停止播放音频。

我的应用程序在后台工作,但控件不可见,因此用户必须打开应用程序并停止视频。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-02-12 13:57:15

我简单地使用了MPRemoteCommandCenter并添加了有关视频的详细信息,请参阅Objetive C++中的代码

//设置播放信息

代码语言:javascript
复制
- (void) setupNowPlayingInfoCenter{

    [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
    [self becomeFirstResponder];

    MPRemoteCommandCenter *commandCenter = [MPRemoteCommandCenter sharedCommandCenter];
    [commandCenter.togglePlayPauseCommand setEnabled:YES];
    [commandCenter.playCommand setEnabled:YES];
    [commandCenter.pauseCommand setEnabled:YES];
    [commandCenter.nextTrackCommand setEnabled:NO];
    [commandCenter.previousTrackCommand setEnabled:NO];
    [commandCenter.playCommand addTargetWithHandler: ^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent * _Nonnull event) {
        [self->_player play];
        return MPRemoteCommandHandlerStatusSuccess;
    }];

    [commandCenter.pauseCommand addTargetWithHandler: ^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent * _Nonnull event) {
        [self->_player pause];
        return MPRemoteCommandHandlerStatusSuccess;
    }];
}

//进入后台更新播放信息

代码语言:javascript
复制
- (void) updateNowPlayingInfoCenter {

    NSDictionary *metadata = [self.media metaData];
    MPNowPlayingInfoCenter *playingInfoCenter = [MPNowPlayingInfoCenter defaultCenter];
    NSMutableDictionary *songInfo = [[NSMutableDictionary alloc] init];
    [songInfo setObject:metadata[MIBMediaMetaDataTrackNameKey] forKey:MPMediaItemPropertyTitle];
    [songInfo setObject:metadata[MIBMediaMetaDataTrackNameKey] forKey:MPMediaItemPropertyArtist];
    [songInfo setObject:metadata[MIBMediaMetaDataTrackDurationKey] forKey:MPMediaItemPropertyPlaybackDuration];
    [songInfo setObject:[NSNumber numberWithDouble:(!self.playing ? 0.0f : 1.0f)] forKey:MPNowPlayingInfoPropertyPlaybackRate];
    [playingInfoCenter setNowPlayingInfo:songInfo];
}

//在init或viewdidload中添加此行代码

代码语言:javascript
复制
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
        [center addObserver:self selector:@selector(applicationDidEnterBackgroundNotification:)
                       name:UIApplicationDidEnterBackgroundNotification
                     object:nil];

在后台进入时调用这两个方法

代码语言:javascript
复制
- (void)applicationDidEnterBackgroundNotification:(NSNotification *)notification {

     [self setupNowPlayingInfoCenter];
     [self updateNowPlayingInfoCenter];
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59933240

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档