I've been struggling with this for a while now and I decided it was time I needed to ask for help.
So basically IVONA is a TTS(text-to-speech) service that has a way of using it in Java with a key(which I have).
Though, there is a sample class that they give you, Link to Sample Class . Which shows you how to use the API and save it as an MP3.
I'm asking how would I modify this class to rather than saving the MP3 it reads, how can I make it so it plays it via Clips and AudioSystem's.
I appreciate all and any responses, thank you very much!
InputStream is = null;//Actual InputStream here
Clip clip = AudioSystem.getClip();
clip.open(AudioSystem.getAudioInputStream(is));
clip.start();
Related
So I'm coding a Java game and I want to add a sound effect when a certain point is reached. I have the sound effect in a .wav file in the same directory as the Java file itself.
I used one of the answers to this question: Best way to get Sound on Button Press for a Java Calculator? to get the audio playing - which it does completely fine (so my code works). However, the problem is that my compiler says that I am using or overriding a Deprecated API, and I am not sure if I want it to happen.
Here is the relevant code (which works but uses a deprecated API):
String soundName = "NoGodNo.wav";
URL soundbyte = new File(soundName).toURI().toURL();
java.applet.AudioClip clip = java.applet.Applet.newAudioClip(soundbyte);
clip.play();
I did some research and found out that AudioClip was deprecated: https://docs.oracle.com/en/java/javase/11/docs/api/java.desktop/java/applet/AudioClip.html, and it has no replacement according to this link.
Is there a way to get past the Deprecated API message by replacing the code entirely? (Because AudioClip has no replacements).
Thanks in advance!
Never mind, I got it. Here's the answer that DOESN'T use Deprecated APIs:
try
{
String soundName = "theme_audio.wav";
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new File(soundName).getAbsoluteFile());
Clip clip = AudioSystem.getClip();
clip.open(audioInputStream);
clip.start();
} catch (Exception e) {}
Thanks!
This question already has an answer here:
javax.sound.sampled.UnsupportedAudioFileException: could not get audio input stream from input file when loading wav file
(1 answer)
Closed 8 years ago.
I want to play a audio clip from my computer while a game is playing. But i can only use very very short sounds. Is there any similar way to playing songs like i play sound effects?
Im using swing graphics for the game if that matters.
The error i get when i try to use a song
"javax.sound.sampled.UnsupportedAudioFileException: could not get audio input stream from input file"
public static void main(String args[]) {
Sound s = new Sound();
s.playSound("C:/Users/isac/Desktop/banjos.wav");
}
}
public void playSound(String file) {
try {
AudioInputStream audio = AudioSystem.getAudioInputStream(new File(
file));
Clip clip = AudioSystem.getClip();
clip.open(audio);
clip.start();
}
catch (UnsupportedAudioFileException uae) {
System.out.println(uae);
} catch (IOException ioe) {
System.out.println(ioe);
} catch (LineUnavailableException lua) {
System.out.println(lua);
}
}
}
The error message you are getting indicates the problem is probably with the format of the file, not its length.
You can check the format of an audio file by looking at it's properties--usually requires a right click on Windows. The properties that matter may be on an "Advanced" tab. Java can read many formats, but where I've most often seen it hang up is with the following:
a person tries to load a .mp3 or .ogg or other form of compression but hasn't implemented any libraries that can decompress those files (not your situation, since your banjo.wav is a wav).
the .wav is not the standard "CD Quality" format (44100 fps, 16-bit encoding, stereo) but rather something like 24-bit or 32-bit encoding or 48000 or 96000 fps.
Current DAWs often make it easy to record in formats that are superior to "CD Quality" but Java doesn't support them yet.
For the most part, you can convert audio files that are not readable with Java to one that is with Audacity (free), if you aren't working from another home studio program. Be careful where you obtain Audacity as some sites that provide it (other than the official site) will include adware or malware or viruses.
As a side note, for a longer file, it would be better to load into a SourceDataLine for playback instead of a Clip. With a SourceDataLine, you don't have to wait for the entire file to load before it will start playing back, and it won't take up anywhere near as much RAM. The Java Tutorials has a section for Java Sound and a page there specifically on playback.
I am having some problems with this code, I am trying to play the sound file but it is not working.
import java.io.*;
import java.net.URL;
import javax.sound.sampled.*;
public class DiscoMusic
{
public static void main(String[] args) throws Exception
{
URL url = new URL("http://www.lecons-guitare.com/Audio/Backingtracks/Stayin_alive.mp3");
AudioInputStream audioIn = AudioSystem.getAudioInputStream(url);
Clip clip = AudioSystem.getClip();
clip.open(audioIn);
clip.start();
}
}
According to the JavaSound info. page.
MP3 decoding support
The Java Sound API does not support many formats of sampled sound
internally. In a 1.6.0_24 Oracle JRE getAudioFileTypes() will
generally return {WAVE, AU, AIFF}. An MP3 decoder at least, is close
by. The mp3plugin.jar of the Java Media Framework supports decoding
MP3s.
I can vouch for that information since I've successfully loaded MP3s using Java Sound and the MP3 SPI (& also wrote the info. page ;) ). The JMF installer download is becoming hard to find, but you can get the mp3plugin.jar direct from where I put it for use in JWS apps.
I want just to perform a simple task. (I'm a java newbie). I want to play an audio clip when a button is clicked.
here's the part of my code(which I did exactly by copying a tutorial from Youtube.)
private void btnPlayActionPerformed(java.awt.event.ActionEvent evt) {
InputStream in;
try{
in=new FileInputStream(new File("C:\\Users\\Matt\\Documents\\dong.wav"));
AudioStream timeupsound=new AudioStream(in);
AudioPlayer.player.start(timeupsound);
}
catch(Exception e){
JOptionPane.showMessageDialog(null, e);
}
}
But the problem is, this doesn't work.
It throws and IOException saying: "could not create audio stream from input stream".
My question is, what am I doing wrong? (as I clearly saw this code work in that youtube video, and I've used the same code. Please help. and once again, I'm a newbie);
The sun package classes should be causing some informative warnings at compile time. Heed them. Don't use classes in that package hierarchy. They are undocumented, are not guaranteed from one Java version to the next, and will probably not be available in a non-Oracle JRE at all.
Instead use the Java Sound based Clip to play audio. See the info. page for working examples.
Note
It might be the WAV is encoded in a format that Java Sound does not support. Media formats are typically 'container formats' that might be encoded using any number of different Codecs. It is likely that WAV is using a more compressive Codec such as MP3 or OGG internally.
I don't know of a Service Provider Interface for the OGG format, but if they are encoded as MP3, you might be able to get it working using the MP3 SPI. See the info. page linked above for details.
Tip
Also change code of the form:
catch (Exception e) { ..
To
catch (Exception e) {
e.printStackTrace(); // very informative! ...
I'm using JAAD with SPI to play m4a files through JavaSound, which I have working fine.
However, I'd like to support a number of formats in this way - but whenever I try and play another format, JAAD seems to try to deal with it and then fail (obviously because it only deals with AAC.)
I'm assuming this is a bug in JAAD, since all the other SPI libraries play fine with each other. Is there a nice way to work around it until it's fixed, or is there another AAC library that works with Javasound which I can use?
There is a workaround using jaad created by Michael Berry: this is the url
https://code.google.com/p/quelea-projection/source/browse/Quelea/src/org/quelea/sound/AudioTrack.java?spec=svn0352523f49cf20d41d1a7dc098af1db38000cc6d&r=0352523f49cf20d41d1a7dc098af1db38000cc6d
Since it took me a while to find berry150's code, here is the solution:
First, you have to order the jars in the classpath so that JLayer, MP3SPI and Tritonous Share are loaded before JAAD. Then for getting the AudioInputStream, use the following code:
if (getAudioFormat().equals(".mp3")) {
audioStream = AudioSystem.getAudioInputStream(file); // Obtains an audio input stream of the song
}
else if (getAudioFormat().equals(".m4a")){
audioStream = new AACAudioFileReader().getAudioInputStream(file);
}
So what happens is that if the audio is mp3, the getAudioStreamMethod() from Javasound will be called first since its JAR was loaded first. If the audio is .m4a, a new instance of the ACCAudioFileReader() is created and the getAudioInputStream() of the JAAD library is called.