Skip to content

Commit

Permalink
NowPlaying API fire only during running
Browse files Browse the repository at this point in the history
  • Loading branch information
rocksdanister committed Feb 20, 2023
1 parent e762e4f commit ede4655
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
29 changes: 25 additions & 4 deletions src/Lively.PlayerCefSharp/Form1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,14 +158,27 @@ await Task.Run(async () =>
case MessageType.cmd_suspend:
if (chromeBrowser.CanExecuteJavascriptInMainFrame && startArgs.PauseEvent && !isPaused) //if js context ready
{
chromeBrowser.ExecuteScriptAsync("livelyWallpaperPlaybackChanged", JsonConvert.SerializeObject(new WallpaperPlaybackState() { IsPaused = true }), Formatting.Indented);
chromeBrowser.ExecuteScriptAsync("livelyWallpaperPlaybackChanged",
JsonConvert.SerializeObject(new WallpaperPlaybackState() { IsPaused = true }),
Formatting.Indented);
}
isPaused = true;
break;
case MessageType.cmd_resume:
if (chromeBrowser.CanExecuteJavascriptInMainFrame && startArgs.PauseEvent && isPaused) //if js context ready
if (chromeBrowser.CanExecuteJavascriptInMainFrame && isPaused)
{
chromeBrowser.ExecuteScriptAsync("livelyWallpaperPlaybackChanged", JsonConvert.SerializeObject(new WallpaperPlaybackState() { IsPaused = false }), Formatting.Indented);
if (startArgs.PauseEvent)
{
chromeBrowser.ExecuteScriptAsync("livelyWallpaperPlaybackChanged",
JsonConvert.SerializeObject(new WallpaperPlaybackState() { IsPaused = false }),
Formatting.Indented);
}
if (startArgs.NowPlaying)
{
//update media state
chromeBrowser.ExecuteScriptAsync("livelyCurrentTrack", JsonConvert.SerializeObject(nowPlayingService?.CurrentTrack, Formatting.Indented));
}
}
isPaused = false;
break;
Expand Down Expand Up @@ -486,9 +499,12 @@ private void ChromeBrowser_LoadingStateChanged(object sender, LoadingStateChange
nowPlayingService.NowPlayingTrackChanged += (s, e) => {
try
{
if (isPaused)
return;
if (chromeBrowser.CanExecuteJavascriptInMainFrame) //if js context ready
{
ExecuteScriptFunctionAsync("livelyCurrentTrack", JsonConvert.SerializeObject(e, Formatting.Indented));
chromeBrowser.ExecuteScriptAsync("livelyCurrentTrack", JsonConvert.SerializeObject(e, Formatting.Indented));
}
}
catch (Exception ex)
Expand Down Expand Up @@ -651,6 +667,11 @@ private async Task CaptureScreenshot(string filePath, ScreenshotFormat format)
}
}

/// <summary>
/// Supports arrays
/// </summary>
/// <param name="functionName"></param>
/// <param name="parameters"></param>
private void ExecuteScriptFunctionAsync(string functionName, params object[] parameters)
{
var script = new StringBuilder();
Expand Down
1 change: 1 addition & 0 deletions src/Lively.PlayerCefSharp/Services/INowPlayingService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace Lively.PlayerCefSharp.Services
public interface INowPlayingService
{
event EventHandler<NowPlayingEventArgs> NowPlayingTrackChanged;
NowPlayingEventArgs CurrentTrack { get; }
void Start();
void Stop();
}
Expand Down
2 changes: 2 additions & 0 deletions src/Lively.PlayerCefSharp/Services/NpsmNowPlayingService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public class NpsmNowPlayingService : INowPlayingService

public NpsmNowPlayingService() { }

public NowPlayingEventArgs CurrentTrack => model;

public void Start()
{
manager.SessionListChanged += SessionListChanged;
Expand Down

0 comments on commit ede4655

Please sign in to comment.