Skip to content

Commit

Permalink
Add 'Formats' to HowlOptions (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
StefH authored Jan 9, 2020
1 parent 924debe commit 78b2fa3
Show file tree
Hide file tree
Showing 9 changed files with 154 additions and 117 deletions.
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
</PropertyGroup>

<PropertyGroup>
<VersionPrefix>0.9.1</VersionPrefix>
<VersionPrefix>0.9.2</VersionPrefix>
</PropertyGroup>

<Choose>
Expand Down
2 changes: 1 addition & 1 deletion GitHubReleaseNotes.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
https://github.com/StefH/GitHubReleaseNotes

GitHubReleaseNotes.exe --output ReleaseNotes.md --skip-empty-releases --exclude-labels question invalid doc --version 0.9.1
GitHubReleaseNotes.exe --output ReleaseNotes.md --skip-empty-releases --exclude-labels question invalid doc --version 0.9.2
236 changes: 130 additions & 106 deletions examples/Howler.Blazor-AudioPlayer/Pages/Example.razor
Original file line number Diff line number Diff line change
@@ -1,107 +1,131 @@
@page "/"
@using Howler.Blazor.Components

<!-- Inject services -->
@inject IHowl Howl
@inject IHowlGlobal HowlGlobal

<div>
<button class="btn btn-primary oi oi-media-play" @onclick="Play"></button>
<button class="btn btn-primary oi oi-media-stop" @onclick="Stop"></button>
<button class="btn btn-primary oi oi-media-pause" @onclick="Pause"></button>
@*<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>Supported Codes : @SupportedCodes</pre>
<pre>ErrorMessage : @ErrorMessage</pre>
</div>

@code {
protected TimeSpan TotalTime;
protected string State = "-";
protected string SupportedCodes;
public string ErrorMessage = "";

/// <summary>
/// InvalidOperationException: JavaScript interop calls cannot be issued at this time. This is because the component is being statically rendererd. When prerendering is enabled, JavaScript interop calls can only be performed during the OnAfterRenderAsync lifecycle method.
/// </summary>
protected override async Task OnAfterRenderAsync(bool firstRender)
{
await base.OnAfterRenderAsync(firstRender);

if (firstRender)
{
// Display all supported codecs
var codecs = await HowlGlobal.GetCodecs();
SupportedCodes = string.Join(", ", codecs);

StateHasChanged();
}
}

protected override void OnInitialized()
{
base.OnInitialized();

// Register callbacks
Howl.OnPlay += e =>
{
ErrorMessage = string.Empty;
State = "Playing";
TotalTime = e.TotalTime;

StateHasChanged();
};

Howl.OnStop += e =>
{
State = "Stopped";

StateHasChanged();
};

Howl.OnPause += e =>
{
State = "Paused";

StateHasChanged();
};

Howl.OnPlayError += e =>
{
ErrorMessage = $"OnPlayError : {e.Error}";

StateHasChanged();
};

Howl.OnLoadError += e =>
{
ErrorMessage = $"OnLoadError : {e.Error}";

StateHasChanged();
};
}

protected async Task Play()
{
ErrorMessage = string.Empty;

var 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",
"https://www.healingfrequenciesmusic.com/wp-content/uploads/2015/03/Love-Abounds-Sample.mp3?_=1"
};
await Howl.Play(sources);
}

protected async Task Stop()
{
await Howl.Stop();
}

protected async Task Pause()
{
await Howl.Pause();
}
@page "/"
@using Howler.Blazor.Components

<!-- Inject services -->
@inject IHowl Howl
@inject IHowlGlobal HowlGlobal

<div>
<table>
<tr>
<td><button class="btn btn-primary oi oi-media-play" @onclick="Play1"></button></td>
<td>Play an audio file with source location</td>
</tr>
<tr>
<td><button class="btn btn-primary oi oi-media-play" @onclick="Play2"></button></td>
<td>Play an audio file with source location and format</td>
</tr>
<tr>
<td><button class="btn btn-primary oi oi-media-pause" @onclick="Pause"></button></td>
<td>Pause</td>
</tr>
<tr>
<td><button class="btn btn-primary oi oi-media-stop" @onclick="Stop"></button></td>
<td>Stop</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>Supported Codes : @SupportedCodes</pre>
<pre>ErrorMessage : @ErrorMessage</pre>
</div>

@code {
protected TimeSpan TotalTime;
protected string State = "-";
protected string SupportedCodes;
public string ErrorMessage = "";

/// <summary>
/// InvalidOperationException: JavaScript interop calls cannot be issued at this time. This is because the component is being statically rendererd. When prerendering is enabled, JavaScript interop calls can only be performed during the OnAfterRenderAsync lifecycle method.
/// </summary>
protected override async Task OnAfterRenderAsync(bool firstRender)
{
await base.OnAfterRenderAsync(firstRender);

if (firstRender)
{
// Display all supported codecs
var codecs = await HowlGlobal.GetCodecs();
SupportedCodes = string.Join(", ", codecs);

StateHasChanged();
}
}

protected override void OnInitialized()
{
base.OnInitialized();

// Register callbacks
Howl.OnPlay += e =>
{
ErrorMessage = string.Empty;
State = "Playing";
TotalTime = e.TotalTime;

StateHasChanged();
};

Howl.OnStop += e =>
{
State = "Stopped";

StateHasChanged();
};

Howl.OnPause += e =>
{
State = "Paused";

StateHasChanged();
};

Howl.OnPlayError += e =>
{
ErrorMessage = $"OnPlayError : {e.Error}";

StateHasChanged();
};

Howl.OnLoadError += e =>
{
ErrorMessage = $"OnLoadError : {e.Error}";

StateHasChanged();
};
}

protected async Task Play1()
{
ErrorMessage = string.Empty;

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

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

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" }
};
await Howl.Play(options);
}

protected async Task Stop()
{
await Howl.Stop();
}

protected async Task Pause()
{
await Howl.Pause();
}
}
4 changes: 4 additions & 0 deletions examples/Howler.Blazor-AudioPlayer/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public Startup(IConfiguration configuration)
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
services.AddCors();
services.AddRazorPages();
services.AddServerSideBlazor();

Expand All @@ -44,6 +45,9 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)

app.UseHttpsRedirection();

// Needed to play files from blob-storage ???
app.UseCors(x => x.AllowAnyHeader().AllowAnyMethod().AllowAnyOrigin());

ConfigureStaticFiles(app);

app.UseRouting();
Expand Down
14 changes: 8 additions & 6 deletions src/Howler.Blazor/Components/Howl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ public ValueTask<int> Play(params string[] sources)
{
Guard.HasNoNulls(sources, nameof(sources));

var options = new HowlSettings
var options = new HowlOptions
{
Sources = sources // .Select(WebUtility.UrlEncode).ToArray()
Sources = sources
};

return Play(options);
Expand All @@ -49,19 +49,21 @@ public ValueTask<int> Play(byte[] audio, string mimetype)
var audioAsBase64 = Convert.ToBase64String(audio);
string html5AudioUrl = $"data:{mimetype};base64,{audioAsBase64}";

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

return _runtime.InvokeAsync<int>("howl.play", _dotNetObjectReference, options);
}

public ValueTask<int> Play(HowlSettings settings)
public ValueTask<int> Play(HowlOptions options)
{
Guard.NotNull(settings, nameof(settings));
Guard.HasNoNulls(options.Sources, nameof(options.Sources));
Guard.Condition(options.Sources, sources => sources.Length > 0, nameof(options.Sources));
// Guard.Condition(options.Formats, formats => formats == null || formats.Length == options.Sources.Length, nameof(options.Formats));

return _runtime.InvokeAsync<int>("howl.play", _dotNetObjectReference, settings);
return _runtime.InvokeAsync<int>("howl.play", _dotNetObjectReference, options);
}

public ValueTask Stop()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace Howler.Blazor.Components
{
public class HowlSettings
public class HowlOptions
{
/// <summary>
/// The sources to the track(s) to be loaded for the sound (URLs or base64 data URIs).
Expand All @@ -9,6 +9,12 @@ public class HowlSettings
/// </summary>
public string[] Sources { get; set; }

/// <summary>
/// Howler.js automatically detects your file format from the extension,
/// but you may also specify a format in situations where extraction won't work (such as with a SoundCloud stream).
/// </summary>
public string[] Formats { get; set; }

/// <summary>
/// Set to true to force HTML5 Audio.
/// This should be used for large audio files so that you don't have to wait for the full file to be downloaded and decoded before playing.
Expand Down
2 changes: 1 addition & 1 deletion src/Howler.Blazor/Components/IHowl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public interface IHowl : IHowlEvents

ValueTask<int> Play(byte[] audio, string mimeType);

ValueTask<int> Play(HowlSettings settings);
ValueTask<int> Play(HowlOptions options);

ValueTask Stop();

Expand Down
2 changes: 1 addition & 1 deletion src/Howler.Blazor/Validation/Guard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public static string NullButNotEmpty(string value, [InvokerParameterName] [NotNu
return value;
}

public static IList<T> HasNoNulls<T>(IList<T> value, [InvokerParameterName] [NotNull] string parameterName)
public static IEnumerable<T> HasNoNulls<T>(IEnumerable<T> value, [InvokerParameterName] [NotNull] string parameterName)
where T : class
{
NotNull(value, parameterName);
Expand Down
1 change: 1 addition & 0 deletions src/Howler.Blazor/wwwroot/JsInteropHowl.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ window.howl = {

howl = new Howl({
src: options.sources,
format: options.formats,
html5: options.html5,
onplay: async function (id) {
const duration = Math.round(howl.duration());
Expand Down

0 comments on commit 78b2fa3

Please sign in to comment.