【问题标题】:Rich Push Notification - Video won't play in Notification Content Extension丰富的推送通知 - 视频不会在通知内容扩展中播放
【发布时间】:2017-03-03 12:53:31
【问题描述】:

我正在处理丰富的通知Notification Content Extension,并且能够成功加载imagesgif,如下图所示:

现在我正在尝试播放视频,我正在执行以下代码来播放它。

- (void)didReceiveNotification:(UNNotification *)notification {
    //self.label.text = @"HELLO world";//notification.request.content.body;

    if(notification.request.content.attachments.count > 0)
    {

        UNNotificationAttachment *Attachen = notification.request.content.attachments.firstObject;


        NSLog(@"====url %@",Attachen.URL);
        AVAsset *asset = [AVAsset assetWithURL:Attachen.URL];
        AVPlayerItem *item = [AVPlayerItem playerItemWithAsset:asset];

        AVPlayer  *player = [AVPlayer playerWithPlayerItem:item];
        AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:player];
        playerLayer.contentsGravity = AVLayerVideoGravityResizeAspect;
        player.actionAtItemEnd = AVPlayerActionAtItemEndNone;
        playerLayer.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);

            [self.VideoPlayerView.layer addSublayer:playerLayer];
        [player play];
    }
}

在 NSLog 中,我也获得了视频的文件 url。但这不会玩。如果有人有这种解决方案,请提供帮助。

谢谢。

【问题讨论】:

    标签: ios objective-c push-notification apple-push-notifications rich-notifications


    【解决方案1】:

    这是我对代码所做的非常小的错误。如果我执行如下代码,我们必须与startAccessingSecurityScopedResource 核对

    - (void)didReceiveNotification:(UNNotification *)notification {
    
        if(notification.request.content.attachments.count > 0)
        {
                UNNotificationAttachment *Attachen = notification.request.content.attachments.firstObject;
    
                if(Attachen.URL.startAccessingSecurityScopedResource)
                {
    
                    NSLog(@"====url %@",Attachen.URL);
                    AVAsset *asset = [AVAsset assetWithURL:Attachen.URL];
                    AVPlayerItem *item = [AVPlayerItem playerItemWithAsset:asset];
    
                    AVPlayer  *player = [AVPlayer playerWithPlayerItem:item];
                    AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:player];
                    playerLayer.contentsGravity = AVLayerVideoGravityResizeAspect;
                    player.actionAtItemEnd = AVPlayerActionAtItemEndNone;
                    playerLayer.frame = CGRectMake(0, 0, self.VideoPlayerView.frame.size.width, self.VideoPlayerView.frame.size.height);
    
                    [self.VideoPlayerView.layer addSublayer:playerLayer];
                    [player play];
    
                }                
            }
    }
    

    视频正在播放。呜呜……

    【讨论】:

    • 大家好,我可以在通知服务扩展中播放视频吗?如果是,那么如何? - (void)didReceiveNotification:(UNNotification *)notification 你在 NotificationService.h 或 Appdelegate.h 中是否有这个方法我仍然对使用通知服务扩展感到困惑我成功下载视频但无法播放视频
    • @Nitin 嘿,我需要你的帮助。你能帮我吗?
    • 确保在 Skype nitin.gohel10 上连接
    【解决方案2】:
    1. 检查您的代码。确保资源已下载到您的磁盘。
    2. 如果你想播放一个url,你应该从通知userInfo中获取url并在你的Content Extension中播放

    解决 Ankur patel 在 cmets 中提出的问题:

    1. 您应该创建一个通知内容扩展。
    2. - (void)didReceiveNotification:(UNNotification *)notification 是一个 UNNotificationContentExtension 协议

    【讨论】:

      【解决方案3】:

      这是在 Swift 5 中的 Notification 中播放视频的解决方案。

      func didReceive(_ notification: UNNotification) {
          let content = notification.request.content
          titleLabel.text = content.title
          subtitleLabel.text = content.body
          
          playerController = AVPlayerViewController()
          preferredContentSize.height = 475
          
          // fetch your url string from notification payload.
          let urlString = "http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/ForBiggerBlazes.mp4"
          if let url = URL(string: urlString) {
              guard let playerController = self.playerController else { return }
              let player = AVPlayer(url: url)
              playerController.player = player
              playerController.view.frame = self.playerBackgroundView.bounds
              playerBackgroundView.addSubview(playerController.view)
              addChild(playerController)
              playerController.didMove(toParent: self)
              player.play()
          }
      }
      

      通知内容扩展的使用条件还有很多,希望大家都关注。

      【讨论】:

      • AVPlayerViewController() 未知。而且很多东西都没有描述。请描述一下。
      • 您可以查看我的文章以获取完整的详细信息。这是有效的解决方案。另外,本文附有完整代码。
      • 能否分享您的全文链接?
      • @YakirSayada 请在这里找到nitinagam17.medium.com/…
      猜你喜欢
      • 2018-04-12
      • 2018-04-28
      • 1970-01-01
      • 2018-08-01
      • 2020-03-24
      • 1970-01-01
      • 1970-01-01
      • 2022-01-16
      • 2017-07-20
      相关资源
      最近更新 更多