Upload Media

This documentation provides guidelines on uploading images and video to server using the TruVideo Media SDK. The SDK offers simple methods to integrate upload functionalities into your application.

Prerequisites

  • Ensure you have integrated the TruVideo Core SDK into your project and completed with the authentication process

  • Camera module will provide MediaItem or simply provide filepath to upload (optional)

Step 1 : Import Classes

#if IOS
using UIKit;
using Foundation;
using TruvideoMediaiOS;
#endif

#if ANDROID
using Application = Android.App.Application;
using TruVideoMediaAndroidBinding;
using JsonSerializer = System.Text.Json.JsonSerializer;
using TruVideoMediaLib = TruVideoMediaAndroid.DotnetTruvideoMedia;
#endif

Step 2 : Create Async Callback Wrapper

#if ANDROID
private Task<T> ExecuteMediaCallbackAsync<T>(Action<MediaCallback> action)
{
    var tcs = new TaskCompletionSource<T>();

    action.Invoke(new MediaCallback(
        success => MainThread.BeginInvokeOnMainThread(() =>
            tcs.TrySetResult((T)Convert.ChangeType(success, typeof(T)))),
        failure => MainThread.BeginInvokeOnMainThread(() => tcs.TrySetException(new Exception(failure)))
    ));

    return tcs.Task;
}
#endif

#if IOS
private Task<T> ExecuteCallbackAsync<T>(Action<Action<string, NSError>> nativeCall) {
    var tcs = new TaskCompletionSource<T>();
    nativeCall((nsResult, error) => {
        if (error != null)
        {
            tcs.SetException(new Exception(error.LocalizedDescription));
        }
        else {
            // Convert NSString to string
            string result = nsResult.ToString();
            tcs.TrySetResult((T)Convert.ChangeType(result, typeof(T)));
           // tcs.SetResult(result);
        }
    });
    return tcs.Task;
}
#endif

Step 3 : Crete TruvideoMediaUploadHandler class to get the callback for iOS specific and Initialise it

Step 4 : Set tags or metaData mediaBuilder object if required

Step 5 : Call UploadMedia Function

Step 6 : Create Listener for event

Finalize :

After completing this steps the function should look like

Last updated

Was this helpful?