public void BeginStreaming(string url) { //Create a new task with a MjpegInputStream return Task.Factory.StartNew(() => { try { //inicialize streaming return(MjpegInputStream.Read(url)); } catch (Exception e) { //if something was wrong return null Console.WriteLine(e.Message); } return(null); }).ContinueWith((t) => { //check if the result was fine VisorVideo.SetSource(t.Result); if (t.Result != null) { //set skip to result t.Result.SetSkip(1); LblMensaje.Text = "Connected"; } else { LblMensaje.Text = "Disconnected"; } //set display mode VisorVideo.SetDisplayMode(MjpegView.SizeFullscreen); //set if you need to see FPS VisorVideo.ShowFps(verFPS); }); }
protected override void OnCreate(Bundle bundle) { RequestWindowFeature(WindowFeatures.NoTitle); base.OnCreate(bundle); // Set our view from the "main" layout resource var view = Resource.Layout.Main; SetContentView(view); VisorVideo = FindViewById <MjpegView>(Resource.Id.mjpegView); LblMensaje = FindViewById <TextView>(Resource.Id.textMensaje); BtnForward = FindViewById <ImageButton>(Resource.Id.imageButtonForward); BtnBackward = FindViewById <ImageButton>(Resource.Id.imageButtonBackward); BtnLeft = FindViewById <ImageButton>(Resource.Id.imageButtonLeft); BtnRight = FindViewById <ImageButton>(Resource.Id.imageButtonRight); BtnStop = FindViewById <ImageButton>(Resource.Id.imageButtonStop); BtnLuces = FindViewById <ImageButton>(Resource.Id.imageButtonLeds); BtnSettings = FindViewById <ImageButton>(Resource.Id.imageButtonSetting); BtnCentrarCamara = FindViewById <Button>(Resource.Id.btnCentrarCamara); HorizontalSeekBar = FindViewById <SeekBar>(Resource.Id.horizontalSeekBar); VerticalSeekBar = FindViewById <VerticalSeekBar>(Resource.Id.verticalSeekBar); VisorVideo?.SetResolution(widthVideoResolution, heightVideoResolution); BtnForward.Touch += (sender, args) => { if (args.Event.Action == MotionEventActions.Down) { BtnForward.SetImageResource(Resource.Drawable.sym_forward_1); SendData(CMD_Forward); } else if (args.Event.Action == MotionEventActions.Up) { SendData(CMD_Stop); BtnForward.SetImageResource(Resource.Drawable.sym_forward); } }; BtnBackward.Touch += (sender, args) => { if (args.Event.Action == MotionEventActions.Down) { BtnBackward.SetImageResource(Resource.Drawable.sym_backward_1); SendData(CMD_Backward); } else if (args.Event.Action == MotionEventActions.Up) { SendData(CMD_Stop); BtnBackward.SetImageResource(Resource.Drawable.sym_backward); } }; BtnLeft.Touch += (sender, args) => { if (args.Event.Action == MotionEventActions.Down) { BtnLeft.SetImageResource(Resource.Drawable.sym_left_1); SendData(CMD_TurnLeft); } else if (args.Event.Action == MotionEventActions.Up) { SendData(CMD_Stop); BtnLeft.SetImageResource(Resource.Drawable.sym_left); } }; BtnRight.Touch += (sender, args) => { if (args.Event.Action == MotionEventActions.Down) { BtnRight.SetImageResource(Resource.Drawable.sym_right_1); SendData(CMD_TurnRight); } else if (args.Event.Action == MotionEventActions.Up) { SendData(CMD_Stop); BtnRight.SetImageResource(Resource.Drawable.sym_right); } }; BtnStop.Touch += (sender, args) => { try { if (args.Event.Action == MotionEventActions.Down) { //BeginStreaming(CameraUrl); //RequestedOrientation = RequestedOrientation == ScreenOrientation.Landscape // ? ScreenOrientation.ReverseLandscape // : ScreenOrientation.Landscape; verFPS = !verFPS; VisorVideo.SetDisplayMode(MjpegView.SizeStandard); VisorVideo.SetDisplayMode(MjpegView.SizeFullscreen); VisorVideo.ShowFps(verFPS); } } catch (Exception ex) { LblMensaje.Text = ex.Message; } }; BtnLuces.Touch += (sender, args) => { try { if (args.Event.Action == MotionEventActions.Down) { ledsOn = !ledsOn; SendData(ledsOn ? CMD_ledon : CMD_ledoff); BtnLuces.SetImageResource(ledsOn ? Resource.Drawable.sym_light : Resource.Drawable.sym_light_off); } } catch (Exception ex) { LblMensaje.Text = ex.Message; } }; BtnSettings.Touch += (sender, args) => { try { if (args.Event.Action == MotionEventActions.Down) { StartActivity(typeof(Settings)); } } catch (Exception ex) { LblMensaje.Text = ex.Message; } }; HorizontalSeekBar.Max = MaxCameraServoValue; HorizontalSeekBar.Progress = 90; var waitingX = false; HorizontalSeekBar.ProgressChanged += (sender, args) => { if (args.Progress >= MinCameraServoValue) { if (!waitingX) { Task.Factory.StartNew(() => { waitingX = true; Thread.Sleep(50); waitingX = false; RunOnUiThread(() => SendData($"{CMD_SetServoX}{args.Progress}")); }); } } }; VerticalSeekBar.Max = MaxCameraServoValue; VerticalSeekBar.Progress = 90; VerticalSeekBar.LayoutParameters.Width = ViewGroup.LayoutParams.WrapContent; var waitingY = false; VerticalSeekBar.ProgressChanged += (sender, args) => { if (args.Progress >= MinCameraServoValue) { if (!waitingY) { Task.Factory.StartNew(() => { waitingY = true; Thread.Sleep(50); waitingY = false; RunOnUiThread(() => SendData($"{CMD_SetServoY}{args.Progress}")); }); } } }; BtnCentrarCamara.Click += (sender, args) => { VerticalSeekBar.Progress = 90; Thread.Sleep(100); HorizontalSeekBar.Progress = 90; }; // Get our button from the layout resource, // and attach an event to it //var button = FindViewById<Button>(Resource.Id.btnAlert); //LblMensaje = FindViewById<TextView>(Resource.Id.lblMessage); //button.Click += (sender, args) => //{ // //var textBox = FindViewById<TextView>(Resource.Id.txtBox1); // //Console.WriteLine(textBox.Text); // //var alert = new AlertDialog.Builder(this); // //alert.SetMessage(textBox.Text); // //alert.SetTitle("Toma mensajaco"); // //alert.SetNeutralButton("OK", new EventHandler<DialogClickEventArgs>((sender, args) => { })); // //alert.Show(); // DetectNetwork(); //}; BeginStreaming(CameraUrl); }