Forum Xamarin.Android
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

Example code about playing a video from an asset with VideoView

Hi!!! I would like to share this piece of code with all of you. The entry is in spanish, but the code is universal :)

Ok, I was trying to play a video file from an asset, using VideoView, and it was impossible for me. I thought that using SetVideoPath would solve all the problems, but i didn't get it work!!!

There is not a lot of xamarin examples, so i'm glad to share this code with you!!!

[](How to play video from a asset file)


  • MHLDMHLD BRBeta ✭✭

    Hey Christoph!
    Thanks for sharing this.
    Just correcting the link:

  • @Christoph Hafner

    I am also trying to play a video file from an asset, using VideoView and I am trying to view a 3gp file in emulator.

    I followed the link

    I created an folder TransData at the same level as Assets, Resources, Propertires.

    and then following is my code.

    public class Activity1 : Activity, MediaPlayer.IOnPreparedListener, ISurfaceHolderCallback
       MediaPlayer player;
       VideoView videoView;
     protected override void OnCreate (Bundle bundle)
        base.OnCreate (bundle);
        SetContentView (Resource.Layout.Main);
        videoView = FindViewById<VideoView> (Resource.Id.SampleVideoView);
    void play(string fullPath)
        ISurfaceHolder holder = videoView.Holder;
         holder.SetType (SurfaceType.PushBuffers);
         holder.AddCallback( this );
         player = new  MediaPlayer ();
         Android.Content.Res.AssetFileDescriptor afd = this.Assets.OpenFd(fullPath);
         if  (afd != null )
             player.SetDataSource (afd.FileDescriptor, afd.StartOffset, afd.Length);
             player.Prepare ();
             player.Start ();
    public void SurfaceCreated(ISurfaceHolder holder)
    public void SurfaceDestroyed(ISurfaceHolder holder)
    public void SurfaceChanged(ISurfaceHolder holder, Android.Graphics.Format format, int w, int h)
    public void OnPrepared(MediaPlayer player)
  • cowthebuwacowthebuwa USMember

    @mario.henrique, @tinku, @‌chrisoph thanks guys for your code samples.

  • GugaMachadoGugaMachado BRMember ✭✭

    Android.Content.Res.AssetFileDescriptor afd = this.Assets.OpenFd("/sdcard/Video.avi");

    I tried using this code that Tinku showed, and is giving error in the media file

  • Android123Android123 INMember

    hello friends i have find the new example of Play Video File - Android Example

  • NadeemAbbasNadeemAbbas USMember

    @Android123 indian ass**le dont spam shit in xamarin forum. ur posted link is for java android studio

  • When you will write this code which is mentioned in the link or mentioned by @tinku.efferns
    A green curly line appears below this statement

    holder.SetType (SurfaceType.PushBuffers);

    which will say that

    ISurfaceHolder.SetType(SurfaceType) is obsolete: 'deprecated'

    Yeah, This method is deprecated. this is ignored, this value is set automatically when needed.
    so if you will write this statement then you will face an exception, just ignore (delete) this statement and it's all Good ^_^

    I hope it will help.

  • Chris_AChris_A Member

    I am just venturing into the xamarin world; And I would love if someone cared to explain exactly how this is supposed to be implemented?

Sign In or Register to comment.