How to play video from Cloudkit on tvOS?

I have a problem that I tried to solve during the day, and I was tired of beating my head on the wall. Does anyone know how to do this?

Number 1 works fine

  • Works great

    - (void) viewDidLoad {
        [super viewDidLoad];
        NSURL *url = [NSURL URLWithString:@"http://techslides.com/demos/sample-videos/small.mp4"];
        AVPlayer *avPlayer = [[AVPlayer alloc] initWithURL:url];
        self.player = avPlayer;
        [self.player play];
    }
    
  • Does not work

    - (void) viewDidLoad {
        [super viewDidLoad];
        CKAsset *asset = record[@"VideoFile"];
        AVPlayer *avPlayer = [[AVPlayer alloc] initWithURL:asset.fileURL];
        self.player = avPlayer;
        [self.player play];
    }
    
  • Does not work

    - (void) viewDidLoad {
        [super viewDidLoad];
        CKAsset *asset = record[@"VideoFile"];
        NSString *fileName = record[@"videoFileKey"];
        NSData *data = [NSData dataWithContentsOfURL:asset.fileURL];
        NSString *cachesDirectoryPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
        NSString *path = [cachesDirectoryPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.mp4", fileName]];
        [data writeToFile:path atomically:YES];
    
        if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
             NSURL *url = [NSURL URLWithString:path];
             AVPlayer *avPlayer = [[AVPlayer alloc] initWithURL:url];
             self.player = avPlayer;
             [self.player play];
        }
     }
    
+4
source share

Source: https://habr.com/ru/post/1610134/


All Articles