Forum Xamarin.iOS

Using AVPlayer to play a new created video file from bytes

Luca.6558Luca.6558 USMember ✭✭

Hi all here my question, hope someone can help me.
I have a stream of bytes that represents a video file.
I'd like reproduce this with a AVPlayerViewController.
So this what I do (with no luck):

  • save a file in the documents folder (file from bytes array)
  • pass the filepath to the Avplayer
  • try to play the video

I can see the Avplayercontroller but with the play icon barred and stream video doesn't start.
I tried this on simulator and if I navigate to the documents folder I can see the created file, plus if I get this file to the Mac I can play it, plus if I put the file in the simulator (drag it to simulator) the file goes in the video folder of the gallery and from here I can play the video too.

                        var videoData = NSData.FromUrl(new NSUrl("path of a video")); //the path here can be a path to a video on the camera roll

                        string documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
                        string localFilename = "test.MOV"; //same if I save the file as .mp4
                        string localPath = Path.Combine(documentsPath, localFilename);

                        File.WriteAllBytes(localPath, videoData.ToArray());
                        NSUrl url = new NSUrl(localPath);
                        AVPlayer player = new AVPlayer(url);
                        playerController = new AVPlayerViewController();
                        playerController.Player = player;
                        PresentViewController(playerController, true, null);

Best Answer


Sign In or Register to comment.