play video in Android using Xamarin
10,782
Solution 1
I found the answer:
public class MainActivity : Activity, MediaPlayer.IOnPreparedListener, ISurfaceHolderCallback
{
MediaPlayer mediaPlayer;
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
SetContentView (Resource.Layout.Main);
var videoView = (VideoView)FindViewById<VideoView> (Resource.Id.videoView);
ISurfaceHolder holder = videoView.Holder;
holder.SetType (SurfaceType.PushBuffers);
holder.AddCallback( this );
var descriptor = Assets.OpenFd("test.ogv");
mediaPlayer = new MediaPlayer ();
mediaPlayer.SetDataSource(descriptor.FileDescriptor, descriptor.StartOffset, descriptor.Length);
mediaPlayer.Prepare();
mediaPlayer.Looping = true;
mediaPlayer.Start();
}
public void SurfaceCreated(ISurfaceHolder holder)
{
Console.WriteLine("SurfaceCreated");
mediaPlayer.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)
{
}
}
Solution 2
var uri= Android.Net.Uri.Parse("android.resource://" + Application.PackageName + "/" + Resource.Raw.demo);
video.SetVideoURI(uri);
video.Start();
Author by
Salome Tsiramua
Updated on July 21, 2022Comments
-
Salome Tsiramua 10 months
In Xamarin for Android using VideoView I want to play a video from resources
Here is code:
var videoView = (VideoView)FindViewById<VideoView> (Resource.Id.videoView); var Path = ("/Resources/raw/video.mp4"); var uri = Android.Net.Uri.Parse (Path); videoView.SetVideoURI (uri); videoView.Start ();
After calling videoView.Start() video doesn't play and all I get is this message: Can't play this video