Getting "composition" when asking for AVAssets

JonasRembrattJonasRembratt SEUniversity ✭✭✭

I'm pretty new to PhotoKit but I haven't found much intel in Apple's documentation as to the behavior of the APIs.

My problem is in this scenario:
1. user picks several videos from the Library (I'm using GMImagePicker
2. My code attempts getting the URL for each picked asset
3. In the callback method I sometimes get a AVUrlAsset but often I get a AVComposition object instead with multiple tracks and segments.

It seems I can simply get the first segment of the first track and pick that URL, at least for my needs right now but why do I smetimes get a AVComposition asset and sometimes a AVUrlAsset?

Answers

  • LucasZhangLucasZhang Member, Xamarin Team Xamurai

    AVComposition is a subclass of AVUrlAsset.In order to deal with video (editing mode) in slow motion more quickly, Apple does not rewrite video every time it is modified. Sometimes, it just changes its configuration. More detail about AVComposition you can refer https://developer.apple.com/documentation/avfoundation/avcomposition?changes=latest_minor&language=objc.

    So ,How to get the AVAsset ? Here is a similar issue https://stackoverflow.com/questions/32081057/how-to-convert-an-avmutablevideocomposition-to-an-avasset and you can convert the OC to C# .

  • JonasRembrattJonasRembratt SEUniversity ✭✭✭

    Thanks! I'll try the suggested approach. But if AVComposition was a subclass of AVUrlAsset I wouldn't have this problem, right? According to Apple's documentation (that you linked to) it's AVComposition : AVAsset.

  • JonasRembrattJonasRembratt SEUniversity ✭✭✭

    I'll test it and accept it. Thanks mate!

  • JonasRembrattJonasRembratt SEUniversity ✭✭✭

    BTW. Does this mean that I get an AVComposition because the video was edited or slow-mo? I'm pretty sure it's neither but I'm interested in why/when it happens.

  • JonasRembrattJonasRembratt SEUniversity ✭✭✭
    edited May 14

    (deleted double-post. This place needs a DELETE)

  • LucasZhangLucasZhang Member, Xamarin Team Xamurai

    Because of your video contains configuration information

  • JonasRembrattJonasRembratt SEUniversity ✭✭✭

    I just had a look and I'm not sure how I should use the accepted answer in the link you sent.

    What I sometimes get, when I RequestAvAsset is an AVComposition, not an AVMutableVideoComposition. So, if having an AVMutableVideoComposition would help then 1) how do I get one from an AVComposition and 2) when I have it how do I get a URL for the video file?

    What I do right now, and that appears to work (although I get the feeling it won't always work) is pretty much just getting the URL from the first segment of the first track of the composition. If I feed that to an AVPlayer it plays as expected.

  • LucasZhangLucasZhang Member, Xamarin Team Xamurai
    edited May 15
    picker.FinishedPickingAssets += (sender, args) => {
                    Console.WriteLine("User finished picking assets. {0} items selected.", args.Assets.Length);
    
                    PHAsset[] assets = args.Assets;
    
                    foreach( PHAsset asset in assets )
                    {
    
    
                        var assetResources = PHAssetResource.GetAssetResources(asset);
    
                        PHAssetResource resource=null ;
    
                        foreach(PHAssetResource assetResource in assetResources)
                        {
                            if (assetResource.ResourceType == PHAssetResourceType.PairedVideo || assetResource.ResourceType == PHAssetResourceType.Video)
                            {
                                resource = assetResource;
                            }
    
                        }
                        NSMutableString fileName=new NSMutableString();
    
                        if (resource.OriginalFilename != null)
                        {
                            fileName.SetString(new NSString(resource.OriginalFilename));
                        }
    
                        if(asset.MediaType== PHAssetMediaType.Video || asset.MediaSubtypes == PHAssetMediaSubtype.PhotoLive)
                        {
                            PHVideoRequestOptions options = new PHVideoRequestOptions();
                            options.Version = PHVideoRequestOptionsVersion.Current;
                            options.DeliveryMode = PHVideoRequestOptionsDeliveryMode.Automatic;
    
                            NSString savePath = new NSString("you can set a path here" + fileName);
    
                            PHAssetResourceManager.DefaultManager.WriteData(resource,new NSUrl(savePath),null,null);
    
    
                        }
                    }
    
                };
    

    savePath is the url.

  • JonasRembrattJonasRembratt SEUniversity ✭✭✭

    I finally got the time to get back to this. Thanks for posting the suggestion @LucasZhang but what I need is the path to the asset, not just the filename. As I understand the above example code it creates a new path, using the asset's filename, but I need the path used for the asset as found in the library.

  • LucasZhangLucasZhang Member, Xamarin Team Xamurai

    The AVAsset is available in the new filepath. So you can use the path and do something you want.

Sign In or Register to comment.