Forum Xamarin.iOS

AvAssetExportSession duplicating files

Ryn9011Ryn9011 Member ✭✭
edited January 30 in Xamarin.iOS

The below code exports an avasset type to an mp4 file. The file is then uploaded to a server (upload code omitted). The files that get uploaded are duplicates however even though the list of avassets are different.

On the my server, the file names and dates are different as they should be but it's just the actual file that is exported from the avasset, it's like the first one doesn't get cleared from memory and uses the same file for the next video item in the loop

Have I incorrectly used avexportsession?

List videos
foreach (var video in videos)
{

using (MemoryStream ms = new MemoryStream())
{
var documents = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
var tmp = Path.Combine(documents, "tmp") + ".MP4";
var outputURL = new NSUrl(tmp);

                                NSError error = new NSError();

                                AVAssetExportSession export = new AVAssetExportSession(video.AVAsset, AVAssetExportSessionPreset.MediumQuality);
                                export.OutputFileType = AVFileType.Mpeg4;
                                export.ShouldOptimizeForNetworkUse = true;
                                export.OutputUrl = NSUrl.FromFilename(outputURL.ToString());                                                                        

                                Task t = Task.Run(async () => await export.ExportTaskAsync());
                                t.Wait();                                   
                                Stream pst = File.OpenRead(tmp);

                                ms.Position = 0;
                                pst.CopyTo(ms);
                                ms.Position = 0;
                                var file = new StreamContent(ms); /**/ this is the same file each time in the loop**

                                NSFileManager.DefaultManager.Remove(outputURL, out error);
                                ms.Dispose();
                                pst.Dispose();
                                export.Dispose();

            // below code included just for context for what the file is used for

                                using (var requestMultiPart = new MultipartFormDataContent())
                                {
                                    var key = new StringContent(mediaUploadModel.fields.Key);
                                    var awSAccessToken = new StringContent(mediaUploadModel.fields.AWSAccessToken);
                                    var xAmzSecurityToken = new StringContent(mediaUploadModel.fields.XAmzSecurityToken);
                                    var policy = new StringContent(mediaUploadModel.fields.Policy);
                                    var signature = new StringContent(mediaUploadModel.fields.Signature);

                                    requestMultiPart.Add(key, "key");
                                    requestMultiPart.Add(awSAccessToken, "AWSAccessKeyId");
                                    requestMultiPart.Add(xAmzSecurityToken, "x-amz-security-token");
                                    requestMultiPart.Add(policy, "policy");
                                    requestMultiPart.Add(signature, "signature");
                                    requestMultiPart.Add(file, "file"); // this is always the same file
                                    NSFileManager.DefaultManager.Remove(outputURL, out error);
                                    using (HttpRequestMessage requestContent = new HttpRequestMessage())
                                    {
                                        var name = video.VideoName;
                                        requestContent.Content = requestMultiPart;
                                        requestContent.Method = new HttpMethod("POST");
                                        requestContent.RequestUri = new Uri(mediaUploadModel.url);

                                        //Console.WriteLine(requestMultiPart.Headers);
                                        var uploadResponse = Task.Run(async () => await client.SendAsync(requestContent)).Result;
                                        string contentResult = uploadResponse.Content.ReadAsStringAsync().Result;
                                        Application.Current.Properties["UploadSuccessful"] = uploadResponse.StatusCode == System.Net.HttpStatusCode.NoContent ? true : false;
                                        Application.Current.Properties["isSyncing"] = false;
                                        export.Dispose();
                                    }                                                                              
                                }
                            }

}

public class IosVideoModel
{
public AVAsset? AVAsset { get; set; }
public string CreationDate { get; set; }
public string VideoName { get; set; }
public string FolderName { get; set; }
}

Best Answer

  • ColeXColeX Member, Xamarin Team Xamurai
    edited February 1 Accepted Answer

    Try to set different file name for each output video .

    int index = 0;
    foreach (var video in videos)
    {
    
    //xxxx
        var tmp = Path.Combine(documents, "tmp") + index.ToString()+ ".MP4";
         index ++;
    

    Xamarin forums are migrating to a new home on Microsoft Q&A!
    We invite you to post new questions in the Xamarin forums’ new home on Microsoft Q&A!
    For more information, please refer to this sticky post.

Answers

  • ColeXColeX Member, Xamarin Team Xamurai
    edited February 1 Accepted Answer

    Try to set different file name for each output video .

    int index = 0;
    foreach (var video in videos)
    {
    
    //xxxx
        var tmp = Path.Combine(documents, "tmp") + index.ToString()+ ".MP4";
         index ++;
    

    Xamarin forums are migrating to a new home on Microsoft Q&A!
    We invite you to post new questions in the Xamarin forums’ new home on Microsoft Q&A!
    For more information, please refer to this sticky post.

Sign In or Register to comment.