Skip to content

Commit

Permalink
enhance(Audio): 一時停止機能を追加
Browse files Browse the repository at this point in the history
  • Loading branch information
EbiseLutica committed Oct 30, 2024
1 parent 962245c commit 5ac055f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
10 changes: 8 additions & 2 deletions Promete.Example/examples/audio/ogg_vorbis.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public override void OnUpdate()
Location: {audio.Time / 1000f:0.000} / {audio.Length / 1000f:0.000}
Location in Samples: {audio.TimeInSamples} / {audio.LengthInSamples}
Loaded: {bgm.LoadedSize} / {bgm.Samples}
Is Playing: {audio.IsPlaying}
Is Pausing: {audio.IsPausing}
[↑] Volume Up
[↓] Volume Down
[←] Pitch Down
Expand All @@ -49,9 +51,13 @@ PRESS ESC TO RETURN

if (keyboard.Space.IsKeyDown)
{
if (audio.IsPlaying)
if (audio is { IsPlaying: true, IsPausing: false })
{
audio.Stop();
audio.Pause();
}
else if (audio.IsPausing)
{
audio.Resume();
}
else
{
Expand Down
29 changes: 29 additions & 0 deletions Promete/Audio/AudioPlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,25 @@ public async void Play(IAudioSource source, int? loop = default)
await PlayAsync(source, loop, stopToken);
}

/// <summary>
/// 一時停止します。
/// </summary>
public void Pause()
{
if (!IsPlaying) return;
IsPausing = true;
}

/// <summary>
/// 一時停止を解除します。
/// </summary>
public void Resume()
{
if (!IsPausing) return;
IsPausing = false;
Console.WriteLine("set IsPausing to false");
}

/// <summary>
/// 再生を停止します。
/// </summary>
Expand Down Expand Up @@ -277,6 +296,16 @@ private async ValueTask PlayAsync(IAudioSource source, int? loop, StopTokenSourc

await Task.Delay(1).ConfigureAwait(false);

if (IsPausing)
{
al.SourcePause(alSource.Handle);
while (IsPausing)
{
await Task.Delay(1).ConfigureAwait(false);
}
al.SourcePlay(alSource.Handle);
}

// 外部から再生停止が要求された場合、再生を終了する
if (st.IsStopRequested)
{
Expand Down

0 comments on commit 5ac055f

Please sign in to comment.