Hello all,
I am developign an application where a user come in front of camera and custom video will play and when user move out of camera range the video get stop. I have develope that motion detection app but unable to make video play and pause.
Please see code:
Window 1:
using System;
using System.Windows;
using System.Windows.Controls;
using Ozeki.Camera;
using Ozeki.Media;
namespace Basic_CameraViewer
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private DrawingImageProvider _provider;
private MediaConnector _connector;
private MotionDetector _detector;
private CameraURLBuilderWPF _myCameraURLBuilder;
private OzekiCamera _camera;
public MainWindow()
{
InitializeComponent();
_connector = new MediaConnector();
_provider = new DrawingImageProvider();
_detector = new MotionDetector();
videoViewer.SetImageProvider(_provider);
video MyWin = new video();
MyWin.Show();
}
void GuiThread(Action action)
{
Dispatcher.BeginInvoke(action);
}
private void StartMotionDetection()
{
_detector.HighlightMotion = HighlightMotion.Highlight;
_detector.MotionColor = MotionColor.Red;
_detector.MotionDetection += detector_MotionDetection;
_detector.Start();
}
void detector_MotionDetection(object sender, MotionDetectionEvent e)
{
GuiThread(() =>
{
if (e.Detection)
{
MotionEventLabel.Content = "Motion Detected";
}
else
MotionEventLabel.Content = "Motion Stopped";
video MyWin = new video(MotionEventLabel.Content.ToString());
});
}
private void StopMotionDetection()
{
_detector.Stop();
_detector.MotionDetection -= detector_MotionDetection;
_detector.Dispose();
}
private void MotionChecked(object sender, RoutedEventArgs e)
{
MotionEventLabel.Content = String.Empty;
var check = sender as CheckBox;
if (check != null)
{
if ((bool)check.IsChecked)
StartMotionDetection();
else
StopMotionDetection();
}
}
private void Compose_Click(object sender, RoutedEventArgs e)
{
_myCameraURLBuilder = new CameraURLBuilderWPF();
var result = _myCameraURLBuilder.ShowDialog();
if (result == true)
{
if (_camera != null)
{
_camera.Disconnect();
videoViewer.Stop();
}
_camera = new OzekiCamera(_myCameraURLBuilder.CameraURL);
_camera.CameraStateChanged += _camera_CameraStateChanged;
InvokeGuiThread(() =>
{
UrlTextBox.Text = _myCameraURLBuilder.CameraURL;
});
Disconnect();
}
}
private void Connect_Click(object sender, RoutedEventArgs e)
{
Streaming();
_connector.Connect(_camera.VideoChannel, _detector);
_connector.Connect(_detector, _provider);
_camera.Start();
videoViewer.Start();
}
private void Disconnect_Click(object sender, RoutedEventArgs e)
{
_connector.Disconnect(_camera.VideoChannel, _detector);
_connector.Disconnect(_detector, _provider);
_detector.Stop();
_camera.Disconnect();
videoViewer.Stop();
}
private void Disconnect()
{
btn_Connect.IsEnabled = true;
btn_Disconnect.IsEnabled = false;
}
private void Streaming()
{
btn_Connect.IsEnabled = false;
btn_Disconnect.IsEnabled = true;
}
void _camera_CameraStateChanged(object sender, CameraStateEventArgs e)
{
InvokeGuiThread(() =>
{
if (e.State == CameraState.Streaming)
{
Streaming();
}
if (e.State == CameraState.Disconnected)
{
Disconnect();
}
});
}
private void InvokeGuiThread(Action action)
{
Dispatcher.BeginInvoke(action);
}
}
}
Window 2:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Microsoft.DirectX.AudioVideoPlayback;
namespace Basic_CameraViewer
{
/// <summary>
/// Interaction logic for video.xaml
/// </summary>
public partial class video : Window
{
public video()
{
InitializeComponent();
VideoControl.Volume = 100;
}
public video(string content)
{
string Motion = "Motion Detected";
string MotionStopped= "Motion Stopped";
if (content.ToString() == Motion)
{
this.Button_Click(content);
}
else if(content.ToString() == MotionStopped)
{
this.Button_Click2(content);
}
}
private void Button_Click2(string dsfsdfsd)
{
string MotionStopped = "Motion Stopped";
if (dsfsdfsd == MotionStopped)
{
VideoControl.Pause();
}
}
private void Button_Click(string sendere)
{
string MotionStopped = "Motion Detected";
if (sendere == MotionStopped)
{
VideoControl.Play();
}
}
}
}
Please help.
Thanks in advance.