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!!!

[http://javiermaura.wordpress.com/2013/08/03/playvideo-con-monodroid-indicando-un-archivo/](How to play video from a asset file)

Posts

  • MHLDMHLD BRBeta ✭✭

    Hey Christoph!
    Thanks for sharing this.
    Just correcting the link: javiermaura.wordpress.com/2013/08/03/playvideo-con-monodroid-indicando-un-archivo/

  • @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);
        play("TransData/video.3gp");
    }
    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)
    {
        Console.WriteLine("SurfaceCreated");
        player.SetDisplay(holder);
    }
    public void SurfaceDestroyed(ISurfaceHolder holder)
    {
        Console.WriteLine("SurfaceDestroyed");
    }
    public void SurfaceChanged(ISurfaceHolder holder, Android.Graphics.Format format, int w, int h)
    {
        Console.WriteLine("SurfaceChanged");
    }
    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
    http://androidexample.com/Play_Video_File_-_Android_Example/index.php?view=article_discription&aid=124&aaid=144

  • 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.