我正在处理富通知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。但那不会玩。如果有人有这个解决方案,请提供帮助。
感谢。
答案 0 :(得分: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];
}
}
}
视频播放。 Boooom ...
答案 1 :(得分:0)
解决 Ankur patel 的评论中提出的问题:
- (void)didReceiveNotification:(UNNotification *)notification
是UNNotificationContentExtension协议答案 2 :(得分:0)
这是在 Swift 5 中的通知中播放视频的解决方案。
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()
}
}
通知内容扩展的使用条件还有一些,希望大家都遵守。