void SetSource() { isPrepared = false; bool hasSetSource = false; if (Element.Source is UriVideoSource) { string uri = (Element.Source as UriVideoSource).Uri; if (!String.IsNullOrWhiteSpace(uri)) { videoView.SetVideoURI(Android.Net.Uri.Parse(uri)); hasSetSource = true; } } else if (Element.Source is FileVideoSource) { string filename = (Element.Source as FileVideoSource).File; if (!String.IsNullOrWhiteSpace(filename)) { videoView.SetVideoPath(filename); hasSetSource = true; } } else if (Element.Source is ResourceVideoSource) { string package = Context.PackageName; string path = (Element.Source as ResourceVideoSource).Path; if (!String.IsNullOrWhiteSpace(path)) { string filename = Path.GetFileNameWithoutExtension(path).ToLowerInvariant(); string uri = "android.resource://" + package + "/raw/" + filename; videoView.SetVideoURI(Android.Net.Uri.Parse(uri)); hasSetSource = true; } } if (hasSetSource && Element.AutoPlay) { videoView.Start(); } }
public void SurfaceCreated(ISurfaceHolder holder) { if (_view.Url.StartsWith("http://") || _view.Url.StartsWith("https://")) { _videoview.SetVideoURI(Android.Net.Uri.Parse(_view.Url)); } else { _videoview.SetVideoPath(_view.Url); } _videoview.Start(); _controller.Show(3000); }
protected override void OnElementChanged(ElementChangedEventArgs <Xamarin.Forms.View> e) { base.OnElementChanged(e); //Get LayoutInflater and inflate from axml //Important note, this project utilizes a layout-land folder in the Resources folder //Android will use this folder with layout files for Landscape Orientations //This is why we don't have to worry about the layout of the play button, it doesn't exist in landscape layout file!!! var activity = Context as Activity; var viewHolder = activity.LayoutInflater.Inflate(Resource.Layout.Main, this, false); view = viewHolder; AddView(view); //Get and set Views videoView = FindViewById <Android.Widget.VideoView> (Resource.Id.SampleVideoView); playButton = FindViewById <Android.Widget.Button> (Resource.Id.PlayVideoButton); //Give some color to the play button, but not important playButton.SetBackgroundColor(Android.Graphics.Color.Aqua); if (playFromAssets) { //relative path to video in Assets folder var filePath = "SampleVideo.mp4"; ISurfaceHolder holder = videoView.Holder; holder.AddCallback(this); player = new Android.Media.MediaPlayer(); AssetFileDescriptor afd = Context.Assets.OpenFd(filePath); player.SetDataSource(afd.FileDescriptor, afd.StartOffset, afd.Length); } else { //uri for a free video //var uri = Android.Net.Uri.Parse ("https://www.dropbox.com/s/hi45psyy0wq9560/PigsInAPolka1943.mp4?dl=1"); var uri = Android.Net.Uri.Parse("android.resource://" + Forms.Context.PackageName + "/" + Resource.Raw.SampleVideo); //Set the videoView with our uri, this could also be a local video on device videoView.SetVideoURI(uri); } //Assign click event on our play button to play the video playButton.Click += PlayVideo; }
protected override void OnCreate(Bundle bundle) { base.OnCreate(bundle); this.Window.AddFlags(WindowManagerFlags.Fullscreen); _video = JsonConvert.DeserializeObject <Video>(Intent.GetStringExtra("video")); _basePath = Intent.GetStringExtra("path"); Android.Widget.VideoView videoView = new Android.Widget.VideoView(this); ViewGroup.LayoutParams param = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.MatchParent); videoView.LayoutParameters = param; //var uri = Android.Net.Uri.Parse(System.IO.Path.Combine(path, _video.Link)); if (_video.Link.StartsWith("http:")) { videoView.SetVideoURI(Android.Net.Uri.Parse(_video.Link)); //_videoView.SetBackgroundColor(Color.Fuchsia); } else { videoView.SetVideoPath(System.IO.Path.Combine(_basePath, _video.Link)); } //videoView.SetBackgroundColor(Color.Black); _contentView.SetBackgroundColor(Color.Black); MediaController mc = new MediaController(this); videoView.SetMediaController(mc); mc.RequestFocus(); _contentView.AddView(videoView); videoView.Start(); }
public VideoView(Context context, Video video, string path, ViewerScreen docView) : base(context) { _video = video; _basePath = path; _docView = docView; //this.SetBackgroundColor(Color.Aqua); View.Inflate(this.Context, Resource.Layout.VideoView, this); try { _videoView = FindViewById <Android.Widget.VideoView>(Resource.Id.videoView); _btnFull = FindViewById <ImageButton>(Resource.Id.btnFull); if (video.Link != "") { if (!File.Exists(System.IO.Path.Combine(_basePath, _video.Link))) { return; } _videoView.SetVideoPath(System.IO.Path.Combine(_basePath, _video.Link)); } else if (_video.UrlStream != "") { _videoView.SetVideoURI(Android.Net.Uri.Parse(_video.UrlStream)); } /*MediaController mc = new MediaController(context); * mc.SetMediaPlayer(_videoView); * mc.SetAnchorView(_videoView); * * _videoView.SetMediaController(mc);*/ _videoView.RequestFocus(); } catch (Exception ex) { Utils.WriteLog("Errore video", ex.Message); return; } _videoView.Error += (sender, e) => { return; }; //playstopclick if (this._video.PlayStopClick) { this.Click += (sender, e) => { this.PlayStop(); }; } if (_video.Fullscreen) { _btnFull.Click += (sender, e) => { Intent i = new Intent(); i.SetClass(this.Context, typeof(VideoViewScreen)); i.PutExtra("path", _basePath); i.PutExtra("video", JsonConvert.SerializeObject(_video)); //ActivitiesBringe.SetObject(zoom); this.Stop(); docView.StartActivity(i); }; } else { _btnFull.Visibility = ViewStates.Invisible; } //autoplay if (this._video.Autoplay) { //_videoView.Prepared -= Autoplay; //_videoView.Prepared += Autoplay; _videoView.Prepared += (sender, e) => { _isReady = true; }; if (_video.Delay > 0) { this.Hide(); _isReady = true; } } else { this.Hide(); } //loop if (_video.Loop) { _videoView.Completion += (sender, e) => { _videoView.Start(); }; } //finish _videoView.Completion += (sender, e) => { if (OnFinish != null) { OnFinish(); } }; }