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

Add HowlGlobal Volume (With example) #19

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
40 changes: 38 additions & 2 deletions examples/Howler.Blazor-WASM-AudioPlayer/Pages/Example.razor
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,25 @@
<td><button class="btn btn-primary oi oi-arrow-bottom" @onclick="SpeedDown"></button></td>
<td>Speed Down</td>
</tr>
<tr>
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please also update all 3 example projects?
image

<td>
<button class="btn btn-primary oi oi-arrow-top" @onclick="VolumeUp"></button>
</td>
<td>Volume Up</td>
</tr>
<tr>
<td>
<button class="btn btn-primary oi oi-arrow-bottom" @onclick="VolumeDown"></button>
</td>
<td>Volume Down</td>
</tr>
</table>
<br />

<pre>TotalTime : @TotalTime</pre>
<pre>State : @State</pre>
<pre>Playback Rate : @Rate</pre>
<pre>Volume: @Volume</pre>
<pre>Supported Codes : @SupportedCodes</pre>
<pre>ErrorMessage : @ErrorMessage</pre>
</div>
Expand All @@ -62,6 +75,7 @@
private const double MinRate = 0.25;
protected TimeSpan TotalTime;
protected double Rate = 1.0;
private double Volume = 0.5;
protected string State = "-";
protected string SupportedCodes;
public string ErrorMessage = "";
Expand Down Expand Up @@ -168,12 +182,13 @@
{
ErrorMessage = string.Empty;
Rate = 1.0;
Volume = 0.5;

var options = new HowlOptions
{
Sources = new[] { "https://lookandstore.blob.core.windows.net/863da396-6e44-4b3b-8db1-e447d87b121f/instrumental_mp3_637141046337946343_99lfo/instrumental_mp3?sv=2018-03-28&sr=b&sig=96LJ7bycF3lrtiWbVP5tK6%2BOOIJKfq7eYPO%2FjOT72ns%3D&se=2022-10-04T18%3A23%3A53Z&sp=rl" },
Formats = new[] { "mp3" },
Volume = 0.5
Volume = Volume
};

SoundIds.Add(await Howl.Play(options));
Expand All @@ -183,12 +198,13 @@
{
ErrorMessage = string.Empty;
Rate = 1.0;
Volume = 0.25;

var options = new HowlOptions
{
Sources = new[] { "https://lookandstore.blob.core.windows.net/863da396-6e44-4b3b-8db1-e447d87b121f/instrumental_mp3_637141046337946343_99lfo/instrumental_mp3?sv=2018-03-28&sr=b&sig=96LJ7bycF3lrtiWbVP5tK6%2BOOIJKfq7eYPO%2FjOT72ns%3D&se=2022-10-04T18%3A23%3A53Z&sp=rl" },
Formats = new[] { "mp3" },
Volume = 0.25
Volume = Volume
};

SoundIds.Add(await Howl.Play(options));
Expand Down Expand Up @@ -245,4 +261,24 @@
await Howl.Pause(id);
}
}
private async Task VolumeUp()
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add a new line before this method

{
Volume += 0.1;
// await HowlGlobal.Volume(Volume);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe keep the await HowlGlobal.Volume(Volume); and put the foreach loop in comment?

foreach (int id in SoundIds)
{
await Howl.Volume(Volume, id);
}
}

private async Task VolumeDown()
{
Volume -= 0.1;
// await HowlGlobal.Volume(Volume);
foreach (int id in SoundIds)
{
await Howl.Volume(Volume, id);
}
}

}
7 changes: 6 additions & 1 deletion src/Howler.Blazor/Components/Howl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public ValueTask<int> Play(byte[] audio, string mimetype)

var options = new HowlOptions
{
Sources = new[] { html5AudioUrl }
Sources = new[] {html5AudioUrl}
};

return _runtime.InvokeAsync<int>("howl.play", _dotNetObjectReference, options);
Expand Down Expand Up @@ -105,6 +105,11 @@ public ValueTask<bool> IsPlaying(int soundId)
return _runtime.InvokeAsync<bool>("howl.getIsPlaying", soundId);
}

public ValueTask<double> Volume(double volume, int soundId)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

soundId should be first argument

{
return _runtime.InvokeAsync<double>("howl.volume", volume, soundId);
}

public async ValueTask<double> GetRate(int soundId)
{
return await _runtime.InvokeAsync<double>("howl.getRate", soundId);
Expand Down
7 changes: 6 additions & 1 deletion src/Howler.Blazor/Components/HowlGlobal.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Threading.Tasks;
using Microsoft.JSInterop;

Expand Down Expand Up @@ -30,5 +30,10 @@ public ValueTask<bool> IsCodecSupported(string? extension)
{
return _runtime.InvokeAsync<bool>("howler.isCodecSupported", extension);
}

public ValueTask Volume(double volume)
{
return _runtime.InvokeVoidAsync("howler.volume", volume);
}
}
}
2 changes: 2 additions & 0 deletions src/Howler.Blazor/Components/IHowl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ public interface IHowl : IHowlEvents
ValueTask<TimeSpan> GetTotalTime(int soundId);

ValueTask<bool> IsPlaying(int soundId);
ValueTask<double> Volume(double volume, int soundId);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add a newline before


Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove newline after

#endregion
}
}
1 change: 1 addition & 0 deletions src/Howler.Blazor/Components/IHowlGlobal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ public interface IHowlGlobal
ValueTask<string[]> GetCodecs();

ValueTask<bool> IsCodecSupported(string? extension);
ValueTask Volume(double volume);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add new line before

}
}
13 changes: 12 additions & 1 deletion src/Howler.Blazor/wwwroot/JsInteropHowl.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,14 @@ window.howl = {

return 0;
},
volume: function (volume, id) {
const howl = getHowl(id);
if (howl) {
return howl.volume(volume, id);
}

return 0;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't return 0 here, that is not needed

},
destroy: function () {
Object.keys(howlInstances).forEach(key => {
try {
Expand Down Expand Up @@ -160,7 +168,10 @@ window.howler = {
},
isCodecSupported: function (extension) {
return extension ? Howler._codecs[extension.replace(/^x-/, '')] : false;
}
},
volume: function (volume) {
Howler.volume(volume);
},
};

function getHowl(id) {
Expand Down