Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor main logic into async RunSample method #79

Draft
wants to merge 13 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
131 changes: 131 additions & 0 deletions Samples/Media SDK/BasicMediaPlayer/MediaPlayerApp.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
// Copyright 2024 Genetec Inc.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

namespace Genetec.Dap.CodeSamples;

using System;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using Sdk.Media;
using MediaPlayer = Sdk.Media.MediaPlayer;

public class MediaPlayerApp(string filePath)
{
private MediaPlayer m_player;
private Window m_window;

public void Run()
{
m_player = new MediaPlayer();
m_player.OpenFile(filePath);

m_window = new Window
{
Width = 800,
Height = 600,
Background = Brushes.Black,
Content = m_player
};
m_window.Loaded += (sender, arg) => m_player.PlayFile();
m_window.KeyDown += OnKeyDown;

m_player.PlayerStateChanged += (sender, arg) => UpdateTitle();
m_player.PlaySpeedChanged += (sender, arg) => UpdateTitle();

var application = new Application();
application.Run(m_window);
m_player.Dispose();
}

private void UpdateTitle()
{
m_window.Title = $"MediaPlayer - {filePath} - {m_player.State}" +
$"{(m_player.PlaySpeed != PlaySpeed.Speed1X ? $" ({m_player.PlaySpeed})" : "")}";
}

private void OnKeyDown(object sender, KeyEventArgs e)
{
switch (e.Key)
{
case Key.Space:
TogglePlayPause();
break;
case Key.Left:
SeekBackward();
break;
case Key.Right:
SeekForward();
break;
case Key.Up:
IncreasePlaySpeed();
break;
case Key.Down:
DecreasePlaySpeed();
break;
case Key.R:
m_player.Rewind();
break;
case Key.I:
m_player.ShowSpecialOverlay(OverlayType.Statistics);
break;
case Key.M:
m_player.IsAudioEnabled = !m_player.IsAudioEnabled;
break;
}
}

private void TogglePlayPause()
{
switch (m_player.State)
{
case PlayerState.Playing:
m_player.Pause();
break;
case PlayerState.Paused:
m_player.ResumePlaying();
break;
default:
m_player.PlayFile();
break;
}
}

private void SeekBackward()
{
if (m_player.LastRenderedFrameTime != DateTime.MinValue)
{
m_player.Seek(m_player.LastRenderedFrameTime.AddSeconds(-10));
}
}

private void SeekForward()
{
if (m_player.LastRenderedFrameTime != DateTime.MinValue)
{
m_player.Seek(m_player.LastRenderedFrameTime.AddSeconds(10));
}
}

private void IncreasePlaySpeed()
{
int currentIndex = Array.IndexOf(Enum.GetValues(typeof(PlaySpeed)), m_player.PlaySpeed);
if (currentIndex < Enum.GetValues(typeof(PlaySpeed)).Length - 1)
{
m_player.PlaySpeed = (PlaySpeed)Enum.GetValues(typeof(PlaySpeed)).GetValue(currentIndex + 1);
}
}

private void DecreasePlaySpeed()
{
int currentIndex = Array.IndexOf(Enum.GetValues(typeof(PlaySpeed)), m_player.PlaySpeed);
if (currentIndex > 0)
{
m_player.PlaySpeed = (PlaySpeed)Enum.GetValues(typeof(PlaySpeed)).GetValue(currentIndex - 1);
}
}
}
147 changes: 17 additions & 130 deletions Samples/Media SDK/BasicMediaPlayer/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,9 @@
// Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

using System;
using System.IO;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using Genetec.Sdk.Media;

namespace Genetec.Dap.CodeSamples;

using MediaPlayer = Sdk.Media.MediaPlayer;
using System;

class Program
{
Expand All @@ -24,118 +17,25 @@ class Program
static void Main()
{
DisplayControls();

string filePath = ReadFilePath();
var app = new MediaPlayerApp(filePath);
app.Run();
}

var player = new MediaPlayer();
player.OpenFile(filePath);

var window = new Window
{
Width = 800,
Height = 600,
Background = Brushes.Black,
Content = player
};
window.Loaded += (sender, arg) => player.PlayFile();
window.KeyDown += OnKeyDown;

player.PlayerStateChanged += (sender, arg) => UpdateTitle();
player.PlaySpeedChanged += (sender, arg) => UpdateTitle();

var application = new Application();
application.Run(window);
player.Dispose();

void UpdateTitle()
{
window.Title = $"MediaPlayer - {filePath} - {player.State}" +
$"{(player.PlaySpeed != PlaySpeed.Speed1X ? $" ({player.PlaySpeed})" : "")}";
}

void OnKeyDown(object sender, KeyEventArgs e)
{
switch (e.Key)
{
case Key.Space:
TogglePlayPause();
break;
case Key.Left:
SeekBackward();
break;
case Key.Right:
SeekForward();
break;
case Key.Up:
IncreasePlaySpeed();
break;
case Key.Down:
DecreasePlaySpeed();
break;
case Key.R:
player.Rewind();
break;
case Key.I:
player.ShowSpecialOverlay(OverlayType.Statistics);
break;
case Key.M:
player.IsAudioEnabled = !player.IsAudioEnabled;
break;
}
}

void TogglePlayPause()
{
switch (player.State)
{
case PlayerState.Playing:
player.Pause();
break;
case PlayerState.Paused:
player.ResumePlaying();
break;
default:
player.PlayFile();
break;
}
}

void SeekBackward()
{
if (player.LastRenderedFrameTime != DateTime.MinValue)
{
player.Seek(player.LastRenderedFrameTime.AddSeconds(-10));
}
}

void SeekForward()
{
if (player.LastRenderedFrameTime != DateTime.MinValue)
{
player.Seek(player.LastRenderedFrameTime.AddSeconds(10));
}
}

void IncreasePlaySpeed()
{
int currentIndex = Array.IndexOf(Enum.GetValues(typeof(PlaySpeed)), player.PlaySpeed);
if (currentIndex < Enum.GetValues(typeof(PlaySpeed)).Length - 1)
{
player.PlaySpeed = (PlaySpeed)Enum.GetValues(typeof(PlaySpeed)).GetValue(currentIndex + 1);
}
}

void DecreasePlaySpeed()
{
int currentIndex = Array.IndexOf(Enum.GetValues(typeof(PlaySpeed)), player.PlaySpeed);
if (currentIndex > 0)
{
player.PlaySpeed = (PlaySpeed)Enum.GetValues(typeof(PlaySpeed)).GetValue(currentIndex - 1);
}
}
private static void DisplayControls()
{
Console.WriteLine("Basic MediaPlayer sample");
Console.WriteLine("Space: Play/Pause");
Console.WriteLine("Left Arrow: Seek backward by 10 seconds");
Console.WriteLine("Right Arrow: Seek forward by 10 seconds");
Console.WriteLine("Up Arrow: Increase playback speed");
Console.WriteLine("Down Arrow: Decrease playback speed");
Console.WriteLine("R: Rewind to the beginning");
Console.WriteLine("I: Toggle statistics overlay");
Console.WriteLine("M: Toggle audio mute");
}

static string ReadFilePath()
private static string ReadFilePath()
{
while (true)
{
Expand All @@ -146,25 +46,12 @@ static string ReadFilePath()
Console.WriteLine("File path cannot be empty. Please try again.");
continue;
}
if (!File.Exists(filePath))
if (!System.IO.File.Exists(filePath))
{
Console.WriteLine($"The file {filePath} does not exist. Please try again.");
continue;
}
return filePath;
}
}

static void DisplayControls()
{
Console.WriteLine("Basic MediaPlayer sample");
Console.WriteLine("Space: Play/Pause");
Console.WriteLine("Left Arrow: Seek backward by 10 seconds");
Console.WriteLine("Right Arrow: Seek forward by 10 seconds");
Console.WriteLine("Up Arrow: Increase playback speed");
Console.WriteLine("Down Arrow: Decrease playback speed");
Console.WriteLine("R: Rewind to the beginning");
Console.WriteLine("I: Toggle statistics overlay");
Console.WriteLine("M: Toggle audio mute");
}
}
Loading