AudioInputStream from InputStream ( load from resource directory) - java

I try to load my sounds from my resource folder when trying out my Application in the IDE.
For images and other stuff that uses InputStreams I use this method:
#Override
public InputStream readAsset(String fileName) throws IOException {
ClassLoader classloader = Thread.currentThread().getContextClassLoader();
InputStream is = classloader.getResourceAsStream(fileName);
return is;
}
this lets me open an Inputstream of which I can pull Images.
As soon as I would try to cast this InputStream to an Audio InputStream I get errors. Also if I would try to make a new AudioInputStream passing the above InputStream as the parameter.
This is my current way to load sounds from external paths:
public class JavaSound implements Sound {
private Clip clip;
public JavaSound(String fileName){
try {
File file = new File(fileName);
if (file.exists()) {
//for external storage Path
AudioInputStream sound = AudioSystem.getAudioInputStream(file);
// load the sound into memory (a Clip)
clip = AudioSystem.getClip();
clip.open(sound);
}
else {
throw new RuntimeException("Sound: file not found: " + fileName);
}
}
catch (MalformedURLException e) {
e.printStackTrace();
throw new RuntimeException("Sound: Malformed URL: " + e);
}
catch (UnsupportedAudioFileException e) {
e.printStackTrace();
throw new RuntimeException("Sound: Unsupported Audio File: " + e);
}
catch (IOException e) {
e.printStackTrace();
throw new RuntimeException("Sound: Input/Output Error: " + e);
}
catch (LineUnavailableException e) {
e.printStackTrace();
throw new RuntimeException("Sound: Line Unavailable Exception Error: " + e);
}
}
#Override
public void play(float volume) {
// Get the gain control from clip
FloatControl gainControl = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN);
// set the gain (between 0.0 and 1.0)
float gain = volume;
float dB = (float) (Math.log(gain) / Math.log(10.0) * 20.0);
gainControl.setValue(dB);
clip.setFramePosition(0); // Must always rewind!
clip.start();
}
#Override
public void dispose() {
clip.close();
}
}
how can i exchange the AudioInputStream part to work like the first code, pulling the files out of my resource directory?
EDIT :
this way of creating a new AudioInputStream by passing an InputStream
File file = new File(fileName);
if (file.exists()) {
ClassLoader classloader = Thread.currentThread().getContextClassLoader();
InputStream is = classloader.getResourceAsStream(fileName);
//for external storage Path
AudioInputStream sound = new AudioInputStream(is);
// load the sound into memory (a Clip)
clip = AudioSystem.getClip();
clip.open(sound);
}
also throws errors before even running it

this made it work in my above code:
public JavaSound(String fileName){
try {
ClassLoader classloader = Thread.currentThread().getContextClassLoader();
InputStream is = classloader.getResourceAsStream(fileName);
AudioInputStream sound = AudioSystem.getAudioInputStream(new BufferedInputStream(is));
// load the sound into memory (a Clip)
clip = AudioSystem.getClip();
clip.open(sound);
}
catch (MalformedURLException e) {
e.printStackTrace();
throw new RuntimeException("Sound: Malformed URL: " + e);
}
catch (UnsupportedAudioFileException e) {
e.printStackTrace();
throw new RuntimeException("Sound: Unsupported Audio File: " + e);
}
catch (IOException e) {
e.printStackTrace();
throw new RuntimeException("Sound: Input/Output Error: " + e);
}
catch (LineUnavailableException e) {
e.printStackTrace();
throw new RuntimeException("Sound: Line Unavailable Exception Error: " + e);
}
}
just had to start a new bufferedInputStream with my inputStream to have the AudioInputStream... :D still thanks a lot ;)

You cannot cast InputStream to AudioInputStream (you could do the inverse). The Clip.open() wants an AudioInputStream.
An approach, suggested by this answer here is to use the URL from the .getResource() call, rather than attempting to open the InputStream and then pass that in.
Therefore, try:
URL soundURL = classloader.getResource(fileName);
AudioInputStream ais = AudioSystem.getAudioInputStream(soundURL);

Related

How to use sound effects in Java 8? Snake eats apple

I wrote Snake code, and want to add a sound effect when snake eats an apple. I copyied a code from some guy on YT, but it doesn't work to me. Can somebody explain me how to do this?
Code:
import com.sun.tools.javac.Main;
import javax.sound.sampled.*;
import java.io.IOException;
import java.net.URL;
public class AppleEatSoundEffect {
public static Mixer mixer;
public static Clip clip;
public static void main(String[] args) {
Mixer.Info[] mixInfos = AudioSystem.getMixerInfo();
mixer = AudioSystem.getMixer(mixInfos[0]);
DataLine.Info dataInfo = new DataLine.Info(Clip.class, null);
try {
clip = (Clip) mixer.getLine(dataInfo);
} catch (LineUnavailableException lue) {
lue.printStackTrace();
}
try {
URL soundURL = Main.class.getResource("NotBad.wav");
AudioInputStream audioStream = AudioSystem.getAudioInputStream(soundURL);
clip.open(audioStream);
} catch (LineUnavailableException lue) {
lue.printStackTrace();
} catch (UnsupportedAudioFileException uafe) {
uafe.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
}
clip.start();
do {
try {
Thread.sleep(100);
} catch (InterruptedException ie) {
ie.printStackTrace();
}
} while (clip.isActive());
}
}
Compiler says thas something wrong with clip = (Clip) mixer.getLine(dataInfo);:
Exception in thread "main" java.lang.IllegalArgumentException: Line unsupported: interface Clip
at java.desktop/com.sun.media.sound.PortMixer.getLine(PortMixer.java:131)
Below is a method which allows you to play audio files, in particular the common WAV or MIDI files. Try it...if you get your desired audio then let me know.
public Thread playWav(final File wavFile, final boolean... loopContinuous) {
String ls = System.lineSeparator();
// Was a file object supplied?
if (wavFile == null) {
throw new IllegalArgumentException(ls + "playWav() Method Error! "
+ "Sound file object can not be null!" + ls);
}
// Does the file exist?
if (!wavFile.exists()) {
throw new IllegalArgumentException(ls + "playWav() Method Error! "
+ "The sound file specified below can not be found!" + ls
+ "(" + wavFile.getAbsolutePath() + ")" + ls);
}
// Play the Wav file from its own Thread.
Thread t = null;
try {
t = new Thread("Audio Thread") {
#Override
public void run() {
try {
Clip clip = (Clip) AudioSystem.getLine(new Line.Info(Clip.class));
audioClip = clip;
clip.addLineListener((LineEvent event) -> {
if (event.getType() == LineEvent.Type.STOP) {
clip.drain();
clip.flush();
clip.close();
}
});
clip.open(AudioSystem.getAudioInputStream(wavFile));
// Are we to loop the audio?
if (loopContinuous.length > 0 && loopContinuous[0]) {
clip.loop(Clip.LOOP_CONTINUOUSLY);
}
clip.start();
}
catch (LineUnavailableException | UnsupportedAudioFileException | IOException ex) {
ex.printStackTrace();
}
}
};
t.start();
Thread.sleep(100);
}
catch (InterruptedException e) {
e.printStackTrace();
}
return t;
}
To use this method simply call it wherever and whenever you want a particular sound effect to be played:
File file = new File("resources/NotBad.wav");
playWav(file);
Make sure the file object points to the correct file location. If you want to loop the audio file as for perhaps game background music then supply boolean true to the optional loopContinuous parameter:
File file = new File("resources/BackgroundMusic.mid");
playWav(file, true);

Error loading audio in java (illegal call to open() in interface Clip)

I'm writing an new audio system for my game and i have come across this error and can not seem to find and solution anywhere,
java.lang.IllegalArgumentException: illegal call to open() in interface Clip
at com.sun.media.sound.DirectAudioDevice$DirectClip.implOpen(Unknown Source)
at com.sun.media.sound.AbstractDataLine.open(Unknown Source)
at com.sun.media.sound.AbstractDataLine.open(Unknown Source)
This is the code i use to load and play my audio.
private Clip load(String filename) {
try {
//Loads the file
InputStream in = new FileInputStream(new File("res/" + filename + FILE_EXT));
//Create the input buffer
InputStream bufferedIn = new BufferedInputStream(in);
//Convert into an audio stream
AudioInputStream audioStream = AudioSystem.getAudioInputStream(bufferedIn);
//Get the audio format
AudioFormat format = audioStream.getFormat();
//Get the data line info
DataLine.Info info = new DataLine.Info(Clip.class, format);
//Return the clip
Clip audioClip = (Clip) AudioSystem.getLine(info);
audioClip.addLineListener(this);
return this.clip = audioClip;
} catch (FileNotFoundException e) {
System.err.println("Failed to load audio! " + filename + " not found!");
throw new RuntimeException(e);
} catch (UnsupportedAudioFileException e) {
System.err.println("Failed to load audio! " + filename + " is unsupported!");
throw new RuntimeException(e);
} catch (IOException e) {
System.err.println("Failed to load audio! " + filename + " caused an IO Exception!");
throw new RuntimeException(e);
} catch (LineUnavailableException e) {
System.err.println("Failed to load audio! " + filename + " line is unavalible!");
e.printStackTrace();
}
throw new RuntimeException("Failed to load audio! input == null!");
}
private void startClip() {
if(this.clip != null) this.clip.start();
else throw new RuntimeException("Failed to start audio clip! The clip appears to be null.");
}
private void stopClip() {
if(this.clip != null) this.clip.close();
else throw new RuntimeException("Failed to close audio clip! The clip appears to be null.");
}
#Override
public void play() {
try {
if(isPlaying()) return;
else {
startClip();
this.clip.open();
this.playing = true;
}
} catch (LineUnavailableException e) {
e.printStackTrace();
}
}
The error occurs at this.clip.open();
Can anyone help me?
You don't pass anything to the Clip to play.
Line#open:
IllegalArgumentException - if this method is called on a Clip instance.
You need to call clip.open(audioStream) instead of clip.open(). Also, you need to do this before starting the Clip.

Java: Program stopping for ~1s occasionally with JavaSound

I'm getting some weird behavior from JavaSound in Java 8 in which I have a Sound class and randomly (every 3-5 instances or so) when being created the thread halts for 0-2s
This is constructor method
public Sound(String fileName) {
// specify the sound to play
// (assuming the sound can be played by the audio system)
// from a wave File
try {
File file = new File(fileName);
if (file.exists()) {
AudioInputStream sound = AudioSystem.getAudioInputStream(file);
// load the sound into memory (a Clip)
clip = AudioSystem.getClip();
clip.open(sound);
length = clip.getMicrosecondLength();
System.out.println("length: " + length);
}
else {
throw new RuntimeException("Sound: file not found: " + fileName);
}
}
catch (MalformedURLException e) {
e.printStackTrace();
throw new RuntimeException("Sound: Malformed URL: " + e);
}
catch (UnsupportedAudioFileException e) {
e.printStackTrace();
throw new RuntimeException("Sound: Unsupported Audio File: " + e);
}
catch (IOException e) {
e.printStackTrace();
throw new RuntimeException("Sound: Input/Output Error: " + e);
}
catch (LineUnavailableException e) {
e.printStackTrace();
throw new RuntimeException("Sound: Line Unavailable Exception Error: " + e);
}
}
My current thoughts are either it is having issues allocating memory(seeing as there are multiple of these instances in an ArrayList), the file is taking long to open from as the coding isn't ideal or it's having trouble removing the objects.
Here's the ArrayList code just in case
public static void playSound(String file){
sounds.add(new Sound("res/" + file));
Sound sound = sounds.get(sounds.size()-1);
sound.setVolume(Screen.music.getVolume());
sound.play();
}
I've tried running the above section of code in a new thread and the main thread continues but the sounds don't always play.
Edit: I do have some sort of garbage collection to clear up most of the memory for the Sound objects but I don't think the problem is related to that, however reusing the objects might be a valid idea?
public void checkStatus(){
if(clip.getMicrosecondPosition() >= length){
new Thread(){
public void run(){
clip.stop();
clip.close();
clip.flush();
clip.drain();
Screen.sounds.remove(this);
}
}.start();
}
}

how to include sound in java

i have made a jar program that need to run an audio file
this is how i open the audio file(not in jar file)
Thread sound = new Thread(){
public void run(){
MakeSound.playSound("Raef.wav");
}
};
i run it with
sound.start();
and end it with
sound.stop();
when i run it on blue j its worked but the sound isnt play on the jar files
can someone solve this ?
i need to make a program with jar
nb : MakeSound is another class i used to play the sound
public class MakeSound {
private static final int BUFFER_SIZE = 128000;
private static File soundFile;
private static AudioInputStream audioStream;
private static AudioFormat audioFormat;
private static SourceDataLine sourceLine;
/**
* #param filename the name of the file that is going to be played
*/
public static void playSound(String filename){
String strFilename = filename;
// buka file
try {
soundFile = new File(strFilename);
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
try {
audioStream = AudioSystem.getAudioInputStream(soundFile);
} catch (Exception e){
e.printStackTrace();
System.exit(1);
}
audioFormat = audioStream.getFormat();
DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFormat);
try {
sourceLine = (SourceDataLine) AudioSystem.getLine(info);
sourceLine.open(audioFormat);
} catch (LineUnavailableException e) {
e.printStackTrace();
System.exit(1);
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
sourceLine.start();
int nBytesRead = 0;
byte[] abData = new byte[BUFFER_SIZE];
while (nBytesRead != -1) {
try {
nBytesRead = audioStream.read(abData, 0, abData.length);
} catch (IOException e) {
e.printStackTrace();
}
if (nBytesRead >= 0) {
#SuppressWarnings("unused")
int nBytesWritten = sourceLine.write(abData, 0, nBytesRead);
}
}
sourceLine.drain();
sourceLine.close();
}
The exact steps to put an audio file in a jar depends on the IDE you are using.
What I normally do is to make a subfolder "audio" and put the audio files there. The subfolder is a subfolder of the code source.
Then, in the code, I create a URL that points to this subfolder.
URL url = this.getClass().getResource("audio/" + fileName);
I don't know about the specifics of MakeSound.playSound(). Hopefully it accepts a URL as a parameter. If it only accepts file names, you might need to rewrite it. Operating Systems generally aren't set up to find files that are packed in jars. URLs, though, are able to point inside of a jar.
Key point: the calling code is in a folder. I used "this" instead of invoking the class name of the calling code which is also possible. If this folder has a subfolder named "audio", the above line of code should find the file.

Can't load sound files LibGdx

I would like to load files directly from expansion OBB file by using AssetManager. I implemented my own FileHandleResolver
public class CustomFileHandleResolver implements FileHandleResolver
{
#Override
public FileHandle resolve(String fileName) {
return new CustomFileHandle(fileName);
}
}
I set it to my AssetManager. I created my own FileHandle and I override read() function
#Override
public InputStream read()
{
InputStream input = null;
try {
input = GameInfo.expansionFile.getInputStream(file.getPath().replace('\\', '/'));
} catch (IOException e) {
e.printStackTrace();
}
return input;
}
It loads all the files like .PNG, .PACK, .FNT, except .OGG files, so I guess that all sound files won't be loaded. I'm getting this error:
com.badlogic.gdx.utils.GdxRuntimeException: com.badlogic.gdx.utils.GdxRuntimeException: Couldn't load dependencies of asset: SFx/button_click.ogg
And this error:
com.badlogic.gdx.utils.GdxRuntimeException: java.lang.ClassCastException: com.solidgamesstudio.threedefendersn.framework.CustomFileHandle cannot be cast to com.badlogic.gdx.backends.android.AndroidFileHandle
I read that zip can not be compressed. In 7zip I selected compression to "Store" so that it's not compressed at all, but still this problem occurs.
I traversed what is happening when files are being loaded and I found that AssetManager calls my CustomFileHandleResolver which creates CustomFileHandle. For every file that is not .OGG it calls InputStream read(). In this function it loads the file from the zip and it's fine. But as I said when it comes to loading .OGG it doesn't call this function. So it's not even trying yet to get the file from the zip. Question is, why .OGG file doesn't call InputStream read() in CustomFileHandle()?
UPDATE
I traversed more and I found out that it won't call InputStream read() because it can't create a Sound from FileHandle somehow. Clue to this is
CustomFileHandle cannot be cast to AndroidFileHandle
While to create a sound you need to pass fileHandle.
public Sound newSound (FileHandle fileHandle);
This is called from SoundLoader
#Override
public void loadAsync (AssetManager manager, String fileName, FileHandle file, SoundParameter parameter) {
sound = Gdx.audio.newSound(file);
}
And that soundLoader uses my CustomFileHandleResolver. I don't know if Sounds are handled differently then other types of files. But by default AssetManager uses
public class InternalFileHandleResolver implements FileHandleResolver {
#Override
public FileHandle resolve (String fileName) {
return Gdx.files.internal(fileName);
}
}
I can't get into Gdx.files.internal to see if there are any special handling for Sounds.
UPDATE
Further analysis give me clue that the main problem is this as mentioned before.
CustomFileHandle cannot be cast to AndroidFileHandle
I don't know why it's casting my FileHandle to AndroidFileHandle while loading OGG file. If it loads fine other type of files, that probably means it doesn't do casting for them. That means that OGG is special and it needs casting. Any clues?
I have not found a way to load sound files from the zip file. Problem is that AssetManager loads sound files differently than other file types. Problem was that it is casting FileHandle to AndroidFileHandle, and since CustomFileHandle extends FileHandle it's impossible to cast it to AndroidFileHandle. I found no way to go around this, because it's deeply rooted.
CustomFileHandle cannot be cast to AndroidFileHandle
In this situation I had to take out all sound files from the OBB file and put them together with the app. I created another instance of AssetManager just for loading sounds. So, sounds are loaded normally as you would with AssetManager and for any other type of file I used AssetManager that uses my own FileHandlerResolver which uses my own FileHandle class that returns a file from the zip. The only problem with this approach is that you are limited to having sounds files only up to 50 MB.
I solved this problem extracting the zip to a specific folder,
and then reading from that external folder.
The extraction of the zip is done by these methods:
public void extract(){
String packageName = getPackageName();
File root = Environment.getExternalStorageDirectory();
File expPath = new File(root.toString() + "/Android/obb/" + packageName);
if (expPath.exists()) {
String strMainPath = null;
try {
strMainPath = expPath + File.separator + "main."
+ getPackageManager().getPackageInfo(
getPackageName(), 0).versionCode + "."
+ packageName + ".obb";
Log.e("Extract File path", "===>"+strMainPath);
File f=new File(strMainPath);
if(f.exists()){
Log.e("Extract From File path", "===> not exist");
}
else
{
Log.e("Extract From File path", "===> exist");
}
String pathToExtract = Environment.getExternalStorageDirectory()+"/"+Cons.FOLDERNAME;
Log.e("Extract to path", "===>"+pathToExtract);
flag = extractZip(strMainPath,pathToExtract);
Log.e("After Extract Zip", "===>"+flag);
} catch (NameNotFoundException e) {
e.printStackTrace();
}
}
}
private boolean extractZip(String pathOfZip,String pathToExtract)
{
int BUFFER_SIZE = 1024;
int size;
byte[] buffer = new byte[BUFFER_SIZE];
try {
File f = new File(pathToExtract);
if(!f.isDirectory()) {
f.mkdirs();
}
ZipInputStream zin = new ZipInputStream(new BufferedInputStream(new FileInputStream(pathOfZip), BUFFER_SIZE));
fileNum=0;
try {
ZipEntry ze = null;
while ((ze = zin.getNextEntry()) != null) {
String path = pathToExtract +"/"+ ze.getName();
if (ze.isDirectory()) {
File unzipFile = new File(path);
if(!unzipFile.isDirectory()) {
unzipFile.mkdirs();
}
}
else {
updateFileNum();
FileOutputStream out = new FileOutputStream(path, false);
BufferedOutputStream fout = new BufferedOutputStream(out, BUFFER_SIZE);
try {
while ( (size = zin.read(buffer, 0, BUFFER_SIZE)) != -1 ) {
fout.write(buffer, 0, size);
}
zin.closeEntry();
}catch (Exception e) {
Log.e("Exception", "Unzip exception 1:" + e.toString());
}
finally {
fout.flush();
fout.close();
}
}
}
}catch (Exception e) {
Log.e("Exception", "Unzip exception2 :" + e.toString());
}
finally {
zin.close();
}
return true;
}
catch (Exception e) {
Log.e("Exception", "Unzip exception :" + e.toString());
}
return false;
}
Note: Extract it to .Android folder, otherwhise users will have direct acces to the assets. For example they will see the images in the Gallery app.
Well, I'm doing this currently. Whenever you need to get a real FileHandle in order for the sound loading mechanism to work (or in any other case were the casting to AndroidFileHandle is bothering you), unzip that file to a local directory and reuse it if needed:
public static FileHandle getRealFileHandle(String zipEntryPath, ZipFile zipFile) {
if (Gdx.files.local(zipEntryPath).exists()) {
return Gdx.files.local(zipEntryPath);
} else {
Gdx.app.log(TAG, "Unzipping file '" + zipEntryPath + "'...");
try {
FileHandle unzippedFile;
ZipEntry entry = zipFile.getEntry(zipEntryPath);
if (entry != null) {
unzippedFile = Gdx.files.local(zipEntryPath);
InputStream is = zipFile.getInputStream(entry);
byte[] buffer = new byte[65536];
int readLength;
while ((readLength = is.read(buffer)) >= 0) {
unzippedFile.writeBytes(buffer, 0, readLength, true);
}
return unzippedFile;
} else {
Gdx.app.error(TAG, "Entry '" + zipEntryPath + "' not found inside zip file.");
}
} catch (IOException ioe) {
Gdx.app.error(TAG, "A problem occurred while writing to the local file.");
}
}
return null;
}

Categories