DubyaDude / WindowsMediaController

Allows developers to more easily get information from and interact with the Windows 10/11 OS media interface. (Also referred to Windows System Media Transport Controls (SMTC))
https://nuget.org/packages/Dubya.WindowsMediaController
MIT License
129 stars 9 forks source link

currentSession is null to control session #16

Closed Dark-night45 closed 9 months ago

Dark-night45 commented 9 months ago

I just got acquainted with this package and I tried to make an app with the help of the example, but I had a problem skipping or stopping the media. I noticed that currentSession is null and I don't know why. I'm using .NET Core 3.1

Code:

private readonly MediaManager mediaManager = new MediaManager();
private MediaSession? currentSession = null;

//some code...

private async void controlerPlay_Click(object sender, EventArgs e)
        {
            var controlsInfo = currentSession?.ControlSession.GetPlaybackInfo().Controls;

            if (controlsInfo?.IsPauseEnabled == true)
            {
                await currentSession?.ControlSession.TryPauseAsync();
            }
            else if (controlsInfo?.IsPlayEnabled == true)
            {
                await currentSession?.ControlSession.TryPlayAsync();
            }
        }

        private async void ControlerNext_Click(object sender, EventArgs e)
        {
            await currentSession?.ControlSession.TrySkipNextAsync();
        }

Error:

System.ArgumentNullException: Value cannot be null. (Parameter 'source')
   at System.WindowsRuntimeSystemExtensions.AsTask[TResult](IAsyncOperation`1 source, CancellationToken cancellationToken)
   at System.WindowsRuntimeSystemExtensions.AsTask[TResult](IAsyncOperation`1 source)
   at System.WindowsRuntimeSystemExtensions.GetAwaiter[TResult](IAsyncOperation`1 source)
DubyaDude commented 9 months ago

Generally, I would recommend against developing .NET Core 3.1, and would recommend moving to .NET 6+. While I build for older versions, .NET Core 3.1 has been EOL since December 2022.

However, even with that, I'm not entirely sure why that's coming up null, I think I'd need more context. Specifically how currentSession is being set.

Aytackydln commented 9 months ago

Well this is a problem with @Dark-night45 's coding here. If you split await currentSession?.ControlSession.TryPauseAsync() into

if (currentSession != null)
{
    await null;
}
else
{
    await currentSession.ControlSession.TryPauseAsync()
}

You can see you are awaiting on null

you should return the fuction as soon as currentSession is null

DubyaDude commented 9 months ago

Okay, wait, I see this is code directly taken from Sample.UI, and @Aytackydln is correct in the fact that I handled currentSession being null incorrectly.

I have committed changes to properly handle this now.

The way the sample UI works is that it doesn't select the user's current/selected session by default. The user selects it, therefore it is initially null. It can also be null if the user-selected session is killed.

Edit: currentSession is the user-selected session in the app, NOT the windows focused session (which is denoted by a # in the UI)

Dark-night45 commented 9 months ago

The reason I'm using .NET Core 3.1 is that on my first attempt at the project, I ran into an error when using .NET 6.0 that resulted in WindowsMediaController not being supported. So, I switched to using .NET Core 3.1 instead.

I realized that the currentSession variable was empty because I intended to display only one MediaSession. I didn't see the MediaManager_OnAnySessionOpened and MediaManager_OnAnySessionClosed functions. Thank you for your assistance.

I closed the issue by mistake, I want to know why WindowsMediaController is not supported in .NET 6.0

DubyaDude commented 9 months ago

@Dark-night45 May want to read up on this, the way WinRT is handled after NET 5 changed: https://github.com/DubyaDude/WindowsMediaController#net-5

DubyaDude commented 9 months ago

@Dark-night45 I see you still have this open, do let me know if you have any issues setting it up, I'm more than happy to help more or even a quick 1 on 1 on Discord!

Dark-night45 commented 9 months ago

I appreciate your help, I will let you know if there are any problems or questions.