Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change playback rate #7

Merged
merged 3 commits into from
May 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions examples/Howler.Blazor-AudioPlayer/Pages/Example.razor
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,31 @@
<td><button class="btn btn-primary oi oi-media-stop" @onclick="Stop"></button></td>
<td>Stop</td>
</tr>
<tr>
<td><button class="btn btn-primary oi oi-arrow-top" @onclick="SpeedUp"></button></td>
<td>Speed Up</td>
</tr>
<tr>
<td><button class="btn btn-primary oi oi-arrow-bottom" @onclick="SpeedDown"></button></td>
<td>Speed Down</td>
</tr>
</table>
<br />

@*<button class="btn btn-primary oi oi-media-step-backward" @onclick="Previous"></button>
<button class="btn btn-primary oi oi-media-step-forward" @onclick="Next"></button>*@
<pre>TotalTime : @TotalTime</pre>
<pre>State : @State</pre>
<pre>Playback Rate : @Rate</pre>
<pre>Supported Codes : @SupportedCodes</pre>
<pre>ErrorMessage : @ErrorMessage</pre>
</div>

@code {
private const double MaxRate = 4.0;
private const double MinRate = 0.25;
protected TimeSpan TotalTime;
protected double Rate = 1.0;
protected string State = "-";
protected string SupportedCodes;
public string ErrorMessage = "";
Expand Down Expand Up @@ -85,6 +97,13 @@
StateHasChanged();
};

Howl.OnRate += e =>
{
Rate = e.CurrentRate;

StateHasChanged();
};

Howl.OnPlayError += e =>
{
ErrorMessage = $"OnPlayError : {e.Error}";
Expand All @@ -103,13 +122,15 @@
protected async Task Play1()
{
ErrorMessage = string.Empty;
Rate = 1.0;

await Howl.Play("https://www.healingfrequenciesmusic.com/wp-content/uploads/2015/03/Love-Abounds-Sample.mp3?_=1");
}

protected async Task Play2()
{
ErrorMessage = string.Empty;
Rate = 1.0;

var options = new HowlOptions
{
Expand All @@ -124,6 +145,22 @@
await Howl.Stop();
}

protected async Task SpeedUp()
chris1411 marked this conversation as resolved.
Show resolved Hide resolved
{
if (Rate < MaxRate && State == "Playing")
{
await Howl.Rate(Rate + .25);
}
}

protected async Task SpeedDown()
{
if (Rate > MinRate && State == "Playing")
{
await Howl.Rate(Rate - .25);
}
}

protected async Task Pause()
{
await Howl.Pause();
Expand Down
37 changes: 37 additions & 0 deletions examples/Howler.Blazor-WASM-AudioPlayer/Pages/Example.razor
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,31 @@
<td><button class="btn btn-primary oi oi-media-stop" @onclick="Stop"></button></td>
<td>Stop</td>
</tr>
<tr>
<td><button class="btn btn-primary oi oi-arrow-top" @onclick="SpeedUp"></button></td>
<td>Speed Up</td>
</tr>
<tr>
<td><button class="btn btn-primary oi oi-arrow-bottom" @onclick="SpeedDown"></button></td>
<td>Speed Down</td>
</tr>
</table>
<br />

@*<button class="btn btn-primary oi oi-media-step-backward" @onclick="Previous"></button>
<button class="btn btn-primary oi oi-media-step-forward" @onclick="Next"></button>*@
<pre>TotalTime : @TotalTime</pre>
<pre>State : @State</pre>
<pre>Playback Rate : @Rate</pre>
<pre>Supported Codes : @SupportedCodes</pre>
<pre>ErrorMessage : @ErrorMessage</pre>
</div>

@code {
private const double MaxRate = 4.0;
private const double MinRate = 0.25;
protected TimeSpan TotalTime;
protected double Rate = 1.0;
protected string State = "-";
protected string SupportedCodes;
public string ErrorMessage = "";
Expand Down Expand Up @@ -85,6 +97,13 @@
StateHasChanged();
};

Howl.OnRate += e =>
{
Rate = e.CurrentRate;

StateHasChanged();
};

Howl.OnPlayError += e =>
{
ErrorMessage = $"OnPlayError : {e.Error}";
Expand All @@ -103,13 +122,15 @@
protected async Task Play1()
{
ErrorMessage = string.Empty;
Rate = 1.0;

await Howl.Play("https://www.healingfrequenciesmusic.com/wp-content/uploads/2015/03/Love-Abounds-Sample.mp3?_=1");
}

protected async Task Play2()
{
ErrorMessage = string.Empty;
Rate = 1.0;

var options = new HowlOptions
{
Expand All @@ -124,6 +145,22 @@
await Howl.Stop();
}

protected async Task SpeedUp()
{
if (Rate < MaxRate && State == "Playing")
{
await Howl.Rate(Rate + .25);
}
}

protected async Task SpeedDown()
{
if (Rate > MinRate && State == "Playing")
{
await Howl.Rate(Rate - .25);
}
}

protected async Task Pause()
{
await Howl.Pause();
Expand Down
9 changes: 9 additions & 0 deletions src/Howler.Blazor/Components/Events/HowlRateEventArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System;

namespace Howler.Blazor.Components.Events
{
public class HowlRateEventArgs : HowlEventArgs
{
public double CurrentRate { get; set; }
}
}
2 changes: 1 addition & 1 deletion src/Howler.Blazor/Components/Howl.Events.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public partial class Howl
public event Action<HowlEventArgs> OnPause;
public event Action<HowlEventArgs> OnMute;
public event Action<HowlEventArgs> OnVolume;
public event Action<HowlEventArgs> OnRate;
public event Action<HowlRateEventArgs> OnRate;
public event Action<HowlEventArgs> OnSeek;
public event Action<HowlEventArgs> OnFade;
public event Action<EventArgs> OnUnlock;
Expand Down
6 changes: 6 additions & 0 deletions src/Howler.Blazor/Components/Howl.Interop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ public void OnPauseCallback(int soundId)
OnPause?.Invoke(new HowlEventArgs { SoundId = soundId });
}

[JSInvokable]
public void OnRateCallback(int soundId, double currentRate)
{
OnRate?.Invoke(new HowlRateEventArgs { SoundId = soundId, CurrentRate = currentRate });
}

[JSInvokable]
public void OnEndCallback(int soundId)
{
Expand Down
10 changes: 10 additions & 0 deletions src/Howler.Blazor/Components/Howl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ public ValueTask Seek(TimeSpan position)
return _runtime.InvokeVoidAsync("howl.seek", position.TotalSeconds);
}

public ValueTask Rate(double rate)
{
return _runtime.InvokeVoidAsync("howl.rate", rate);
}

public ValueTask Load()
{
return _runtime.InvokeVoidAsync("howl.load");
Expand All @@ -96,6 +101,11 @@ public ValueTask<bool> IsPlaying()
return _runtime.InvokeAsync<bool>("howl.getIsPlaying", _dotNetObjectReference);
}

public async ValueTask<double> GetRate()
{
return await _runtime.InvokeAsync<double>("howl.getRate");
}

public async ValueTask<TimeSpan> GetCurrentTime()
{
int timeInSeconds = await _runtime.InvokeAsync<int>("howl.getCurrentTime");
Expand Down
2 changes: 2 additions & 0 deletions src/Howler.Blazor/Components/HowlGlobal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ namespace Howler.Blazor.Components
public class HowlGlobal : IHowlGlobal
{
private readonly IJSRuntime _runtime;
public const double MaxRate = 4.0;
public const double MinRate = 0.25;

public HowlGlobal(IJSRuntime runtime)
{
Expand Down
4 changes: 4 additions & 0 deletions src/Howler.Blazor/Components/IHowl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ public interface IHowl : IHowlEvents

ValueTask Seek(TimeSpan position);

ValueTask Rate(double rate);

/// <summary>
/// This is called by default, but if you set preload to false, you must call load before you can play any sounds.
/// </summary>
Expand All @@ -39,6 +41,8 @@ public interface IHowl : IHowlEvents
/// </summary>
ValueTask Unload();

ValueTask<double> GetRate();

ValueTask<TimeSpan> GetCurrentTime();

ValueTask<TimeSpan> GetTotalTime();
Expand Down
2 changes: 1 addition & 1 deletion src/Howler.Blazor/Components/IHowlEvents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public interface IHowlEvents
/// <summary>
/// Fires when the sound's playback rate has changed.
/// </summary>
event Action<HowlEventArgs> OnRate;
event Action<HowlRateEventArgs> OnRate;

/// <summary>
/// Fires when the sound has been seeked.
Expand Down
16 changes: 16 additions & 0 deletions src/Howler.Blazor/wwwroot/JsInteropHowl.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ window.howl = {
onpause: async function (id) {
await dotnetReference.invokeMethodAsync('OnPauseCallback', id);
},
onrate: async function (id) {
const currentRate = howl.rate();
await dotnetReference.invokeMethodAsync('OnRateCallback', id, currentRate);
},
onend: async function (id) {
await dotnetReference.invokeMethodAsync('OnEndCallback', id);
},
Expand Down Expand Up @@ -64,6 +68,11 @@ window.howl = {
howl.seek(position);
}
},
rate: function (rate) {
if (howl) {
howl.rate(rate);
}
},
load: function () {
if (howl) {
howl.load();
Expand All @@ -84,6 +93,13 @@ window.howl = {

return false;
},
getRate: function () {
if (howl) {
return howl.rate();
}

return 0;
},
getCurrentTime: function () {
if (howl && howl.playing()) {
const seek = howl.seek();
Expand Down