ios - 丰富的推送通知 - 视频不会在通知内容扩展中播放

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

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

enter image description here

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

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

谢谢。

最佳答案

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

            }                
        }
}

视频正在播放。呜呜……

关于ios - 丰富的推送通知 - 视频不会在通知内容扩展中播放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42579421/

相关文章:

ios - 用于单元测试的核心数据临时数据库

ios - 当 UIImageView 显示图像时,如何防止 UIToolbar 调整大小?

ios - tableview segue 到另一个 View Controller

iphone - 推送通知错误: "Unable to set local cert chain file"

android - 多个用户的 Worklight 推送通知

iphone - 请求失败 'JSON'

ios - Expo AV 音频无法在 iOS/iPhone 上播放

ios - NSCountedSet 按计数排序

iphone - uitextfield positionFromPosition :offset not working emojis

php - iOs 推送通知与 PHP 5.2 或更低版本兼容