Forum Xamarin.Android

Announcement:

The Xamarin Forums have officially moved to the new Microsoft Q&A experience. Microsoft Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

To create new threads and ask questions head over to Microsoft Q&A for .NET and get involved today.

Can't play this video Xamarin.Android

daalboodaalboo Member ✭✭✭
edited July 2020 in Xamarin.Android

I have downloaded video from url, encrypted with aes and saved it to System.Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData) /temp folder. I decrypt the file before playing. For playing video I am using CrossMediaManager. I can't play with this nuget and tried videoPlayer from xamarin documentation. I get Can't play this video Xamarin.Android with this method. Where did i went wrong? Can anyone help me with this?

Here is my code for encryption

 public void SaveFile(string name, Stream data, string location = "temp")
        {
            var baseName = "temp";
            var dbpath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData);
            var path = Path.Combine(dbpath, baseName);
            Directory.CreateDirectory(path);
            path = GetStorageFolderPath();
            string filePath = Path.Combine(path, name);
            bool doesExist = System.IO.File.Exists(filePath);
            if (doesExist)
            {
                name = name + "1";
                filePath = Path.Combine(path, name);
            }

            // Length is 16 byte
            Cipher cipher = Cipher.GetInstance("AES/CBC/PKCS5Padding");
            byte[] raw = System.Text.Encoding.Default.GetBytes(sKey);
            SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
            IvParameterSpec iv = new IvParameterSpec(System.Text.Encoding.Default.GetBytes(ivParameter));//
            cipher.Init(CipherMode.EncryptMode, skeySpec, iv);
            CipherInputStream fis = new CipherInputStream(data, cipher);        
            var buffer = new byte[1024];
            int numberOfBytedRead;
            using (var fileStream = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None, 1024))
            {

                while ((numberOfBytedRead = fis.Read(buffer)) >= 0)
                {

                    fileStream.WriteAsync(buffer, 0, numberOfBytedRead);
                }
            }

            fis.Close();
            data.Close();
        }

This is code for decryption

public class FileDecryption : Decrypt
    {
        private string sKey = "0123456789abcdef";//key,
        private string ivParameter = "1020304050607080";
        public void DecriptFile(string filename, string name)
        {
            var baseName = "temp";
            var dbpath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData);
            var extStore = Path.Combine(dbpath, baseName);


            Android.Util.Log.Error("Decryption Started", extStore + "");
            FileInputStream fis = new FileInputStream(extStore + "/" + filename);

            // createFile(filename, extStore);
            //FileOutputStream fos = new FileOutputStream(extStore + "/" + filename, false);

            using (var fs = System.IO.File.OpenWrite(extStore + "/" + name))
            {
                Cipher cipher = Cipher.GetInstance("AES/CBC/PKCS5Padding");
                byte[] raw = System.Text.Encoding.Default.GetBytes(sKey);
                SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
                IvParameterSpec iv = new IvParameterSpec(System.Text.Encoding.Default.GetBytes(ivParameter));//
                cipher.Init(CipherMode.DecryptMode, skeySpec, iv);
                CipherOutputStream cos = new CipherOutputStream(fs, cipher);
                int b;
                byte[] d = new byte[1024 * 1024];
                while ((b = fis.Read(d)) != -1)
                {
                    cos.Write(d, 0, b);
                }
                // System.IO.File.Delete(extStore + "/" + "decrypted" + filename);
                Android.Util.Log.Error("Decryption Ended", extStore + "/" + name);
                //cos.Close();
                // fis.Close();
                //fis.Dispose();
                //return d;
            }
        }


    }

Video Player in PCL

 <mm:VideoView
                x:Name="vid"
                HorizontalOptions="FillAndExpand"
                ShowControls="True"
                VerticalOptions="FillAndExpand" />
 <video:VideoPlayer
                x:Name="videoPlayer"
                IsVisible="false"
                VerticalOptions="FillAndExpand" />

C#

 var path = DependencyService.Get<IFileService>().GetStorageFolderPath();
                    DependencyService.Get<Decrypt>().DecriptFile(videoUrl,"name");
                    string full = path + "/name");
                    bool istrue = DependencyService.Get<IFileService>().checkFile("name");
                    //await CrossMediaManager.Current.Play(full); (Tried this too)
                    videoPlayer.IsVisible = true;
                    videoPlayer.Source = new FileVideoSource
                    {
                        File = full
                    };

Posts

  • bananaprinterbananaprinter USMember ✭✭

    What is your error? Also, is there any reason to encrypt a video that you want to play? Seems unnecessary to me.

  • daalboodaalboo Member ✭✭✭

    Actually, my client seems to be more concerned about video security. They are paid videos and I need to some how protect them from sharing.

  • daalboodaalboo Member ✭✭✭

    Well well well, this encryption and decryption seems to be working fine. The problem was getting path of video before playing. I tried to get video directory path and concat name with it and tried playing video. This gave error "cannot play this video".
    Hope this will be useful for anybody in future.

Sign In or Register to comment.