ios – 如果屏幕被锁定,当点击暂停按钮时,avplayer不会暂停

ios – 如果屏幕被锁定,当点击暂停按钮时,avplayer不会暂停,第1张

概述如果应用正在播放音频和手机屏幕被锁定,则控制屏幕如下所示.我无法对avplayer采取任何行动 在我的appdelegate中,我实现了: - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { MPRemoteCommand 如果应用正在播放音频和手机屏幕被锁定,则控制屏幕如下所示.我无法对avplayer采取任何行动

在我的appdelegate中,我实现了:

- (BOol)application:(UIApplication *)application dIDFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    MPRemoteCommandCenter *rcc = [MPRemoteCommandCenter sharedCommandCenter];[[rcc skipForwardCommand] setEnabled:NO];[[rcc skipBackwardCommand] setEnabled:NO];[[rcc nextTrackCommand] setEnabled:NO];[[rcc prevIoUsTrackCommand] setEnabled:NO];[[rcc skipForwardCommand] setEnabled:NO];[[rcc skipBackwardCommand] setEnabled:NO];rcc.playCommand.enabled = YES;rcc.pauseCommand.enabled = YES;[[MPRemoteCommandCenter sharedCommandCenter].playCommand addTarget:self action:@selector(play)];[[MPRemoteCommandCenter sharedCommandCenter].pauseCommand addTarget:self action:@selector(pause)];}- (voID) play {[[MyVIDeoController instance] play];}- (voID) pause {[[MyVIDeoController instance] pause];}

class MyVIDeoController包括:

- (voID) pause {    [self.avPlayer pause];}- (voID) play {    [self.avPlayer play];}

即使触发了这些方法(添加了要检查的断点),也不会对avplayer采取任何 *** 作.无论如何,avplayer都不会停顿.

有没有办法暂停avplayer?

编辑1:
添加完整代码

在我的AppDelegate中:

- (voID) remoteControlReceiveDWithEvent: (UIEvent *) event {[[ZVIDeoPlayerController instance] eventReceived:event];if (event.type == UIEventTypeRemoteControl) {    switch (event.subtype) {        case UIEventSubtypeRemoteControlTogglePlayPause: {            break;        }        case UIEventSubtypeRemoteControlPlay: {            [[ZVIDeoPlayerController instance] play];            break;        }        case UIEventSubtypeRemoteControlPause: {            [[ZVIDeoPlayerController instance] pause];            break;        }        default:            break;    }}

}

- (voID)applicationDIDEnterBackground:(UIApplication *)application {[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];[self becomeFirstResponder];}- (voID)applicationDIDBecomeActive:(UIApplication *)application {[[UIApplication sharedApplication] endReceivingRemoteControlEvents];}

我收到的事件,但音频不会在播放器上调用暂停方法.

编辑2:
PlayerController类中的实例声明

+ (instancetype)instance {static ID instance = nil;if (instance == nil){    static dispatch_once_t oncetoken = 0;    dispatch_once(&oncetoken,^(voID) {        NSAssert(instance == nil,@"Singleton instance is already allocated.");        instance = [[super allocWithZone:NulL] init];    });}return instance;}

初始化AVPlayer

AVURLAsset *avAsset = [AVURLAsset URLAssetWithURL:url options:nil];AVPlayerItem *playerItem = [AVPlayerItem playerItemWithAsset:avAsset];AVAudioSession *session = [AVAudioSession sharedInstance];[session setcategory:AVAudioSessioncategoryPlayback error:nil];NSError *activationError = nil;BOol success = [[AVAudioSession sharedInstance] setActive: YES error: &activationError];NSMutableDictionary *songInfo = [[NSMutableDictionary alloc] init];MPMediaItemArtwork *albumart = [[MPMediaItemArtwork alloc] initWithImage: [UIImage imagenamed:@"Audio_thumbnail_Play"]];[songInfo setobject:Title forKey:MPMediaItemPropertyTitle];[songInfo setobject:@"100" forKey:MPMediaItemPropertyPlaybackDuration];[songInfo setobject:albumart forKey:MPMediaItemPropertyArtwork];[[MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:songInfo];self.avPlayer = [AVPlayer playerWithPlayerItem:playerItem];self.avPlayerLayer = [AVPlayerLayer playerLayerWithPlayer:self.avPlayer];
解决方法 我找到了问题的解决方案.当我获得avPlayer的nil值时,我使用了我的PageVIEwController类来获取PlayerController的实例.然后我使用这个playerController的实例来播放和暂停我的avplayer,因为这个实例保存了对avPlayer的引用.

- (PlayerController *)getVIDeoController {NSArray *controllers = [UtiliyClass getNavigationController].vIEwControllers;PageVIEwController *pageController = nil;for (UIVIEwController *cont in controllers) {    if ([cont isKindOfClass:[PageVIEwController class]]) {        pageController = (PageVIEwController *)cont;        break;    }}if (pageController == nil) {    return nil;}NSArray *objectsController =pageController.pageController.vIEwControllers;PlayerController *vIDeoPlayerController = nil;for (UIVIEwController *item in objectsController) {    if ([item isKindOfClass:[PlayerController class]]) {        vIDeoPlayerController = (PlayerController *)item;        break;    }}return vIDeoPlayerController;}- (voID) pause {PlayerController *controller = [self getVIDeoController];[controller.avPlayer pause];}- (voID) play {PlayerController *controller = [self getVIDeoController];[controller.avPlayer play];}
总结

以上是内存溢出为你收集整理的ios – 如果屏幕被锁定,当点击暂停按钮时,avplayer不会暂停全部内容,希望文章能够帮你解决ios – 如果屏幕被锁定,当点击暂停按钮时,avplayer不会暂停所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1063007.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存