Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,13 @@ private BrowserAudioEnv() {
}

@Override
@JavaScriptBody(args = { "src" }, body = ""
+ "if (typeof Audio !== 'object') return null;"
+ "return new Audio(src);")
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

  • in 2013 I had to distinguish among old JDK7 JavaFX WebView without Audio
  • and newer WebViews (JDK8+) that had Audio object
  • or real browsers that had Audio too
  • at that time the typeof Audio was object
  • these days it is function
    • inspite of calling Audio("some.mp3") yields and error suggesting to use new Audio(...) instead!
  • I know the https://github.com/jtulach/minesweeper was playing its sounds in the past
  • there must have been an incompatible change in the browsers since then
  • that changed the typeof Audio
  • interesting way to break someone's code

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Me: I do remember that in 2013 the typeof Audio was object. When did it change?

AI: ... there was a major architectural transition across all major browsers that changed web API constructors like Audio, Image, and Option from returning "object" to "function" under the typeof operator.

  • Bug 829524 - DOM constructors should be typeof "function".
  • Bug 83849 - Make DOM constructors have typeof 'function'

I haven't found those bugs/commits, but the explanation matches my memories.

@JavaScriptBody(args = { "src" }, body = """
if (typeof Audio === 'undefined') {
return null;
} else {
return new Audio(src);
}
""")
public Object create(String src) {
// null if not running in browser
return null;
Expand Down
40 changes: 39 additions & 1 deletion sound/src/test/java/net/java/html/sound/AudioClipTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,13 @@
*/
package net.java.html.sound;

import net.java.html.js.JavaScriptBody;
import net.java.html.junit.BrowserRunner;
import static org.junit.Assert.assertTrue;
import org.junit.Assume;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.netbeans.html.boot.spi.Fn;

@RunWith(BrowserRunner.class)
public class AudioClipTest {
Expand All @@ -30,6 +34,40 @@ public AudioClipTest() {

@Test
public void testPlayNonExistingClip() {
AudioClip.create("non-existing.mp3").play();
var clip = AudioClip.create("non-existing.mp3");
clip.play();
}

@Test
public void testAudioSystemIsSupported() {
var clip = AudioClip.create("non-existing.mp3");
if (assumeAudioObject()) {
var p = Fn.activePresenter().getClass().getName();
assertTrue("Playing should be supported on " + p, clip.isSupported());
}
}

@JavaScriptBody(args = { }, body = """
try {
var audio = new Audio("any.mp3");
return null;
} catch (err) {
return "Error constructing new Audio: " + err.toString();
}
""")
private static String createAudioObjectOrFail() {
return "Not running in a browser at all!";
}

private static boolean assumeAudioObject() {
var errMsg = createAudioObjectOrFail();
var p = Fn.activePresenter().getClass().getName();
if (errMsg == null) {
return true;
} else {
// it is expected that ScriptPresenter doesn't have Audio
Assume.assumeTrue("No Audio for " + p + ":" + errMsg, p.contains("ScriptPresenter"));
return false;
}
}
}
5 changes: 5 additions & 0 deletions src/main/javadoc/overview.html
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,11 @@ <h3>Try it</h3>
vm.loadClass('org.apidesign.demo.minesweeper.MainBrwsr');
</script>

<h3>New in version 1.8.3</h3>
<p>
{@link net.java.html.sound.AudioClip} plays music again via internal browser audio system - see
<a target="_blank" href="https://github.com/apache/netbeans-html4j/pull/56">PR #56</a> for details.
</p>
<h3>New in version 1.8.2</h3>
<p>
When post-processing classes with <code>-javaagent</code> instrumenting agent
Expand Down
Loading