Fix skip song issue
This commit is contained in:
parent
09808a2136
commit
a8e0dcaf37
@ -26,6 +26,8 @@ public class LmsSessionController : ISessionController, IDisposable
|
|||||||
private Timer? _progressTimer;
|
private Timer? _progressTimer;
|
||||||
private bool _disposed;
|
private bool _disposed;
|
||||||
private BaseItem? _currentItem;
|
private BaseItem? _currentItem;
|
||||||
|
private Guid[] _playlist = [];
|
||||||
|
private int _playlistIndex;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Initializes a new instance of the <see cref="LmsSessionController"/> class.
|
/// Initializes a new instance of the <see cref="LmsSessionController"/> class.
|
||||||
@ -137,14 +139,31 @@ public class LmsSessionController : ISessionController, IDisposable
|
|||||||
await _lmsClient.PowerOnAsync(_player.MacAddress).ConfigureAwait(false);
|
await _lmsClient.PowerOnAsync(_player.MacAddress).ConfigureAwait(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
// For now, play the first item
|
|
||||||
// TODO: Support playlists/queues
|
|
||||||
if (playRequest.ItemIds.Length > 0)
|
if (playRequest.ItemIds.Length > 0)
|
||||||
{
|
{
|
||||||
var itemId = playRequest.ItemIds[0];
|
// Store the full playlist
|
||||||
var streamUrl = BuildStreamUrl(itemId);
|
_playlist = playRequest.ItemIds;
|
||||||
|
_playlistIndex = playRequest.StartIndex ?? 0;
|
||||||
|
|
||||||
|
// Play the item at the start index
|
||||||
|
await PlayItemAtIndexAsync(_playlistIndex, playRequest.StartPositionTicks ?? 0).ConfigureAwait(false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task PlayItemAtIndexAsync(int index, long startPositionTicks = 0)
|
||||||
|
{
|
||||||
|
if (index < 0 || index >= _playlist.Length)
|
||||||
|
{
|
||||||
|
_logger.LogWarning("Invalid playlist index {Index}, playlist has {Count} items", index, _playlist.Length);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var itemId = _playlist[index];
|
||||||
|
_playlistIndex = index;
|
||||||
|
|
||||||
|
var streamUrl = BuildStreamUrl(itemId);
|
||||||
|
_logger.LogInformation("Playing item {Index}/{Total}: {Url}", index + 1, _playlist.Length, streamUrl);
|
||||||
|
|
||||||
_logger.LogInformation("Playing stream URL: {Url}", streamUrl);
|
|
||||||
await _lmsClient.PlayUrlAsync(_player.MacAddress, streamUrl).ConfigureAwait(false);
|
await _lmsClient.PlayUrlAsync(_player.MacAddress, streamUrl).ConfigureAwait(false);
|
||||||
|
|
||||||
// Track current playback state
|
// Track current playback state
|
||||||
@ -156,10 +175,8 @@ public class LmsSessionController : ISessionController, IDisposable
|
|||||||
_currentItem = _libraryManager.GetItemById(itemId);
|
_currentItem = _libraryManager.GetItemById(itemId);
|
||||||
|
|
||||||
// Seek to start position if specified
|
// Seek to start position if specified
|
||||||
var startPositionTicks = 0L;
|
if (startPositionTicks > 0)
|
||||||
if (playRequest.StartPositionTicks.HasValue && playRequest.StartPositionTicks.Value > 0)
|
|
||||||
{
|
{
|
||||||
startPositionTicks = playRequest.StartPositionTicks.Value;
|
|
||||||
var positionSeconds = startPositionTicks / TimeSpan.TicksPerSecond;
|
var positionSeconds = startPositionTicks / TimeSpan.TicksPerSecond;
|
||||||
await _lmsClient.SeekAsync(_player.MacAddress, positionSeconds).ConfigureAwait(false);
|
await _lmsClient.SeekAsync(_player.MacAddress, positionSeconds).ConfigureAwait(false);
|
||||||
}
|
}
|
||||||
@ -170,7 +187,6 @@ public class LmsSessionController : ISessionController, IDisposable
|
|||||||
// Start progress reporting timer (every 2 seconds)
|
// Start progress reporting timer (every 2 seconds)
|
||||||
StartProgressTimer();
|
StartProgressTimer();
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
private async Task ReportPlaybackStartAsync(Guid itemId, long positionTicks)
|
private async Task ReportPlaybackStartAsync(Guid itemId, long positionTicks)
|
||||||
{
|
{
|
||||||
@ -363,9 +379,33 @@ public class LmsSessionController : ISessionController, IDisposable
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case PlaystateCommand.NextTrack:
|
case PlaystateCommand.NextTrack:
|
||||||
|
if (_playlistIndex < _playlist.Length - 1)
|
||||||
|
{
|
||||||
|
_logger.LogInformation("Skipping to next track (index {Index})", _playlistIndex + 1);
|
||||||
|
await PlayItemAtIndexAsync(_playlistIndex + 1).ConfigureAwait(false);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
_logger.LogInformation("Already at last track, stopping playback");
|
||||||
|
await _lmsClient.StopAsync(_player.MacAddress).ConfigureAwait(false);
|
||||||
|
await ReportPlaybackStoppedAsync().ConfigureAwait(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
|
||||||
case PlaystateCommand.PreviousTrack:
|
case PlaystateCommand.PreviousTrack:
|
||||||
// TODO: Implement playlist navigation
|
if (_playlistIndex > 0)
|
||||||
_logger.LogDebug("Track navigation not yet implemented");
|
{
|
||||||
|
_logger.LogInformation("Skipping to previous track (index {Index})", _playlistIndex - 1);
|
||||||
|
await PlayItemAtIndexAsync(_playlistIndex - 1).ConfigureAwait(false);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// At first track, restart from beginning
|
||||||
|
_logger.LogInformation("At first track, restarting from beginning");
|
||||||
|
await _lmsClient.SeekAsync(_player.MacAddress, 0).ConfigureAwait(false);
|
||||||
|
}
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user