the easiest ways to play an audio clip are the discouraged ones
mark.yagnatinsky at barclays.com
mark.yagnatinsky at barclays.com
Sat Dec 23 21:07:04 UTC 2023
Also: the discouraged methods (applet-based and sun.audio.Player) are friendly enough to call Toolkit. getPCMConvertedAudioInputStream() for you.
Of course, you could do that yourself, except... this is ALSO not exposed.
So we need to re-implement that method. Sigh.
The "official" way to do things should NOT be this hard!
From: Yagnatinsky, Mark : Markets Pre Trade
Sent: Saturday, December 23, 2023 12:02 AM
To: 'client-libs-dev at openjdk.org' <client-libs-dev at openjdk.org>
Subject: the easiest ways to play an audio clip are the discouraged ones
Sorry for double-post; I think my fist attempt got blocked because I wasn't subscribed.
Also sorry if this is the wrong list; please redirect me if so.
ALSO ALSO sorry if this is a bit of a disorganized rant, but I'm a bit frustrated.
Suppose I have a sound file on my hard disk and want Java to play it.
By copying random code snippets off the internet, we can discover
sun.audio.AudioPlayer.player.start(java.io.InputStream file)
Easy! But wait, the sun.* packages are not officially exported and javac 8 complains, and with modules it likely complains even louder.
Then we discover JavaFX has a nifty MediaPlayer which is almost as easy. But JavaFX is "de-bundled" so that's a bit annoying.
No matter, we can use Applet.newAudioClip()! We don't want an applet, this API works fine without them. Nice!
But the entire applet namespace is deprecated, and even worse, it is deprecated for removal.
(For the record, I think deprecating it makes sense, but deprecating it for removal was a bad idea and should be undone. But I'm an outsider, so whatever.)
Depending on things that are threatened to go away on possibly short notice seems risky, so let's not.
I see something called JMF (Java Media Framework) but that's also not bundled, and hard to even find docs for. Sigh.
Let's looks at something nice and official: the sound tutorial:
https://docs.oracle.com/javase/tutorial/sound/playing.html
Oh dear, that looks complicated. I guess we need to somehow get Clip? How to do that?
The tutorial has a big warning on top that it hasn't been updated since Java 8 but I suspect it hasn't been touched since Java 5 or longer.
Well, in must have been touched a bit, since it uses Clip.class syntax which was added in Java 5. But what it doesn't have is any mention of these methods:
https://bugs.java.com/bugdatabase/view_bug.do?bug_id=4896221
My best explanation is that they got added after the tutorial was written, and then the tutorial was never updated, which I think reduces the amount of good those methods do.
But that's okay because we found those methods, one of which is called getClip(). Yay, we have Clip!
Now all we need to do is clip.open(AudioInputStream)... hmm. Where do I get one of those? The tutorial doesn't seem to say.
The official Javadocs have a neat feature where each interface lists all public classes that implement it.
What it does NOT have is a list of all methods whose return type "matches" the interface.
But that's okay, we have nice IDEs, so I can just "find usages" of AudioInputStream and filter down to those where it appears as a return type!
And there we go: I need AudioSystem.getAudioInputStream().
And then we're ready: we can call start() and it will play our clip!
Hmm... I wonder if the call returns when playback starts or only when it ends... maybe we need a scratch thread so it doesn't block us?
Oh and let's not forget to close() the clip when we're done!
This API seems to try pretty hard to give experts everything they need.
But it doesn't cater very well to the use case of "I don't know what a mixer is, and I wish I didn't have to care!"
The highly-nonofficial sun.audio.AudioPlayer does a pretty good job for this use-case; there should ideally be something at LEAST that easy in an official, non-deprecated API.
Just my two cents.
Mark
This message is for information purposes only. It is not a recommendation, advice, offer or solicitation to buy or sell a product or service, nor an official confirmation of any transaction. It is directed at persons who are professionals and is intended for the recipient(s) only. It is not directed at retail customers. This message is subject to the terms at: https://www.cib.barclays/disclosures/web-and-email-disclaimer.html.
For important disclosures, please see: https://www.cib.barclays/disclosures/sales-and-trading-disclaimer.html regarding marketing commentary from Barclays Sales and/or Trading desks, who are active market participants; https://www.cib.barclays/disclosures/barclays-global-markets-disclosures.html regarding our standard terms for Barclays Corporate and Investment Bank where we trade with you in principal-to-principal wholesale markets transactions; and in respect to Barclays Research, including disclosures relating to specific issuers, see: http://publicresearch.barclays.com.
__________________________________________________________________________________
If you are incorporated or operating in Australia, read these important disclosures: https://www.cib.barclays/disclosures/important-disclosures-asia-pacific.html.
__________________________________________________________________________________
For more details about how we use personal information, see our privacy notice: https://www.cib.barclays/disclosures/personal-information-use.html.
__________________________________________________________________________________
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/client-libs-dev/attachments/20231223/43a099f0/attachment-0001.htm>
More information about the client-libs-dev
mailing list