我正在研究丰富的通知Notification Content Extension
并且能够加载images
and 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。但这不会发挥作用。如果有人有该解决方案,请提供帮助。
Thanks.
我在代码中犯了一个非常小的错误。我们必须核实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];
}
}
}
视频正在播放。嘘……
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)