5

我正在处理丰富的通知Notification Content Extension,并且能够像以下屏幕截图一样加载images并成功:gif

在此处输入图像描述

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

- (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。但这不会玩。如果有人有该解决方案,请提供帮助。

谢谢。

4

3 回答 3

4

这是我对代码所做的非常小的错误。我们必须检查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];

            }                
        }
}

视频正在播放。呜呜……

于 2017-03-03T13:31:09.307 回答
0

这是在 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()
    }
}

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

于 2021-02-15T09:45:38.613 回答
0
  1. 检查你的代码。确保资源已下载到您的磁盘。
  2. 如果你想播放一个 url,你应该从通知 userInfo 中获取 url 并在你的 Content Extension 中播放

为了解决Ankur patel在评论中提出的问题:

  1. 您应该创建一个通知内容扩展。
  2. - (void)didReceiveNotification:(UNNotification *)notification 是一个 UNNotificationContentExtension 协议
于 2018-05-29T07:28:40.170 回答