Skip to content

Commit

Permalink
Added better exception handling for run loop
Browse files Browse the repository at this point in the history
- Discovered that the case for displaying "Nothing playing" would break things, or wouldn't refresh to playing something when something else came on. I think I fixed that.
- Added try/catch to try and handle an exception that can be thrown if the state of something playing changes on the moment we try to get the media properties.
  • Loading branch information
Esherymack committed Dec 28, 2022
1 parent 9744a16 commit 4504c35
Showing 1 changed file with 22 additions and 18 deletions.
40 changes: 22 additions & 18 deletions GameSenseTestApp/Program.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
using gamesense_net;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using Windows.Media;
using Windows.Media.Control;
using WindowsMediaController;
using static WindowsMediaController.MediaManager;

namespace GameSenseTestApp
{
Expand All @@ -23,7 +17,7 @@ public static async Task Main()
mediaMan = new MediaManager();

await mediaMan.StartAsync();

string handlerJson = $$"""
"datas":[
{
Expand All @@ -47,31 +41,41 @@ public static async Task Main()

while (true)
{
var session = mediaMan.GetFocusedSession();
var res = await session.ControlSession.TryGetMediaPropertiesAsync();

if(res.Title == string.Empty || res.Title == null)
try
{
dataJson = $$"""
var session = mediaMan.GetFocusedSession();
if (session == null)
{
dataJson = $$"""
"value": {{c.GetNextValueFromCycler()}},
"frame": {
"song-name": "Nothing Playing",
"album-name": " "
}
""";
}
else
{
dataJson = $$"""
}
else
{
var res = await session.ControlSession.TryGetMediaPropertiesAsync();

dataJson = $$"""
"value": {{c.GetNextValueFromCycler()}},
"frame": {
"song-name": "{{res.Title}}",
"album-name": "{{res.Artist}}"
}
""";
}


c.RunEvent("SONGTEXT", dataJson);
await Task.Delay(1500);
}
catch(Exception ex)
{
// Don't do anything if an exception is thrown
}

c.RunEvent("SONGTEXT", dataJson);

}
}
}
Expand Down

0 comments on commit 4504c35

Please sign in to comment.