Playing video from within app?

后端 未结 1 1242
隐瞒了意图╮
隐瞒了意图╮ 2020-12-21 08:59

I am trying to play a local video from my app using AVPlayer. I have looked everywhere but can\'t find any useful tutorials/demos/documentation. Here is what I am trying, bu

相关标签:
1条回答
  • 2020-12-21 09:22

    I believe the problem is that you're assuming that after executing this line AVURLAsset *asset = [AVURLAsset assetWithURL: fileURL]; the asset is ready to use. This is not the case as noted in the AV Foundation Programming Guide:

        You create an asset from a URL using AVURLAsset. Creating the asset, however, 
        does not necessarily mean that it’s ready for use. To be used, an asset must 
        have loaded its tracks.
    

    After creating the asset try loading it's tracks:

    AVURLAsset *asset = [AVURLAsset URLAssetWithURL:fileURL options:nil];
    NSString *tracksKey = @"tracks";
    
    [asset loadValuesAsynchronouslyForKeys:@[tracksKey] completionHandler:
     ^{
         NSError *error;
         AVKeyValueStatus status = [asset statusOfValueForKey:tracksKey error:&error];
    
         if (status == AVKeyValueStatusLoaded) { 
    
             // At this point you know the asset is ready
             self.playerItem = [AVPlayerItem playerItemWithAsset:asset];
    
             ...
         }
     }];
    

    Please refer to this link to see the complete example.

    Hope this helps!

    0 讨论(0)
提交回复
热议问题