Audio Not Working (User Malfunction)

Hello. I am currently attempting to add sound effects to my game. I decided to keep the implementation of this in an AudioManager class on which I could just call playSound() from other parts of my program. However, I am having an issue with AudioNodes. From messing around with it, I have figured that I can only call the AudioNode constructor once, and only once, even by putting it on different references. When I attempt to construct another AudioNode, the game stops responding.

I am simply at a loss of what to do. I tried looking at helloAudio but to no avail.

[java]

public class AudioManager
{

private Main app;

private AudioNode book;
private AudioNode cabinet;
private AudioNode correct;
private AudioNode door;
private AudioNode incorrect;
private AudioNode idCard;
private AudioNode select;
private AudioNode waterGulp;





public AudioManager(Main app)
{
    this.app = app;

}




public void prepareAudio()
{
    book = new AudioNode(app.getAssetManager(), "Sounds/Book.ogg", false);
    app.getRootNode().attachChild(book);
    
    
    cabinet = new AudioNode(app.getAssetManager(), "Sounds/Cabinet.ogg", false);
    app.getRootNode().attachChild(cabinet);

    
    correct = new AudioNode(app.getAssetManager(), "Sounds/Correct.ogg", false);
    app.getRootNode().attachChild(correct);

    
    door = new AudioNode(app.getAssetManager(), "Sounds/Door.ogg", false);
    app.getRootNode().attachChild(door);

    
    idCard = new AudioNode(app.getAssetManager(), "Sounds/IDCard.ogg", false);
    app.getRootNode().attachChild(idCard);

    
    incorrect = new AudioNode(app.getAssetManager(), "Sounds/Incorrect.ogg", false);
    app.getRootNode().attachChild(incorrect);

    
    select = new AudioNode(app.getAssetManager(), "Sounds/Select.ogg", false);
    app.getRootNode().attachChild(select);

    
    waterGulp = new AudioNode(app.getAssetManager(), "Sounds/Water Gulp.ogg", false);
    app.getRootNode().attachChild(waterGulp);

}




public void playBook()
{
    book.play();
}
public void playCabinet()
{
    cabinet.play();  
}
public void playCorrect()
{
    correct.play();   
}
public void playDoor()
{
    door.play();   
}
public void playIDCard()
{
    idCard.play();   
}
public void playIncorrect()
{
    incorrect.play();   
}
public void playSelect()
{
    select.play();   
}
public void playWaterGulp()
{
    waterGulp.play();   
}

}

The constructor is called, then prepareAudio(), then each time I want a sound played I call the playSound() methods.

Ahh… It looks like I forgot to add the last code tag in there. Sorry. Here is a cleaner view of my code:

[java]
public class AudioManager
{

private Main app;

private AudioNode book;
private AudioNode cabinet;
private AudioNode correct;
private AudioNode door;
private AudioNode incorrect;
private AudioNode idCard;
private AudioNode select;
private AudioNode waterGulp;

public AudioManager(Main app)
{
this.app = app;

}

public void prepareAudio()
{
book = new AudioNode(app.getAssetManager(), “Sounds/Book.ogg”, false);
app.getRootNode().attachChild(book);

cabinet = new AudioNode(app.getAssetManager(), “Sounds/Cabinet.ogg”, false);
app.getRootNode().attachChild(cabinet);

correct = new AudioNode(app.getAssetManager(), “Sounds/Correct.ogg”, false);
app.getRootNode().attachChild(correct);

door = new AudioNode(app.getAssetManager(), “Sounds/Door.ogg”, false);
app.getRootNode().attachChild(door);

idCard = new AudioNode(app.getAssetManager(), “Sounds/IDCard.ogg”, false);
app.getRootNode().attachChild(idCard);

incorrect = new AudioNode(app.getAssetManager(), “Sounds/Incorrect.ogg”, false);
app.getRootNode().attachChild(incorrect);

select = new AudioNode(app.getAssetManager(), “Sounds/Select.ogg”, false);
app.getRootNode().attachChild(select);

waterGulp = new AudioNode(app.getAssetManager(), “Sounds/Water Gulp.ogg”, false);
app.getRootNode().attachChild(waterGulp);

}

public void playBook()
{
book.play();
}
public void playCabinet()
{
cabinet.play();
}
public void playCorrect()
{
correct.play();
}
public void playDoor()
{
door.play();
}
public void playIDCard()
{
idCard.play();
}
public void playIncorrect()
{
incorrect.play();
}
public void playSelect()
{
select.play();
}
public void playWaterGulp()
{
waterGulp.play();
}

}
[/java]

Hmmm… here is what I do with audio nodes… maybe something in here will help?

[java]
AudioNode audioNode = new AudioNode(app.getAssetManager(), audioPath, false);
audioNode.setPositional(false);
audioNode.setReverbEnabled(false);
someNode.attachChild(audioNode);

audioNode.setVolume(someVolume);
audioNode.playInstance();
[/java]

Maybe you’re missing one of these steps?

And actually, I usually store them in a HashMap, so I can call a single play method, like so:

[java]
Map<String, AudioNode> audios = new HashMap();

// When creating the nodes
audios.put(“someKey”,audioNode);

// the method for playing them:
public void playAudioNode(String key, float volume) {
AudioNode audioNode = audios.get(key);
if (audioNode != null) {
audioNode.setVolume(volume);
audioNode.playInstance();
}
}
[/java]

Okay. I tried that and I’m still coming up with the same error. Here is a look at my code. It seems perfectly fine to me.

[java]

package mygame;

public class AudioManager
{

private Main app;
private AssetManager assetManager;

private AudioNode book;
private AudioNode cabinet;
private AudioNode correct;
private AudioNode door;
private AudioNode incorrect;
private AudioNode idCard;
private AudioNode select;
private AudioNode waterGulp;

private AudioNode source;

Map&lt;String, AudioNode&gt; audios;


public AudioManager(Main app, AssetManager assetManager)
{
    this.app = app;
    this.assetManager = assetManager;
    
    source = new AudioNode();
    app.getRootNode().attachChild(source);
    audios = new HashMap();

}




public void prepareAudio()
{
    book = new AudioNode(assetManager, "Sounds/Book.ogg", false);
    book.setPositional(false);
    book.setReverbEnabled(false); 
    app.getRootNode().attachChild(book);
    audios.put("book",book);
    
    
    cabinet = new AudioNode(assetManager, "Sounds/Cabinet.ogg", false);
    cabinet.setPositional(false);
    cabinet.setReverbEnabled(false); 
    app.getRootNode().attachChild(cabinet);
    audios.put("cabinet",cabinet);



}




public void playAudioNode(String key, float volume)
{
    AudioNode audioNode = audios.get(key);
    if (audioNode != null)
    {
        audioNode.setVolume(volume);
        audioNode.playInstance();
    }
}

}

[/java]

I am trying this just with the book and cabinet sounds. Additionally, The problem seems to be with the initial loading of it, not the call method. I figure this because my game is unable to complete the loading sequence. It simply stops responding after I initiate it.

You mentioned an error this time. What is the error?

Oh, whoops, I am sorry. There is no error. It just stops responding, although I am getting this in the output:

Java Result: -805306369

Not sure what it’s from or if it even provides useful info.

@BrianMan said: Oh, whoops, I am sorry. There is no error. It just stops responding, although I am getting this in the output:

Java Result: -805306369

Not sure what it’s from or if it even provides useful info.

Stops responding as in “program is locked up and must be shut down through task manager”?

Or stops responding meaning no input is allowed but the stats keep updating and rendering keeps happening?

Where do you get that “Java Result”?

Are you by any chance swallowing exceptions to the calls?

Is it really the second audio node or just that particular audio node that you have second? (ie: if you swap the init order of the first two does it fail earlier or the same?)

Can you show us the code that calls prepareAudio()?

Alright, when it stops responding, it is the freezing where the task manager has to end it, although normally I directly end program through the little box that pops up.

I have absolutely no idea where that java result is coming from. I have my error messages set to severe. The message is red and it shows up exactly when I close the program.

I am not sure what swallowing exceptions to the calls is. I have an idea about how exceptions work but I haven’t intentionally included them in any parts of my program.

Switching the order of those two test audio nodes does not make it work. Additionally, I tried having only the second one activate rather than the first and the cabinet noise worked fine in game.
[java]

public void loadGame()
{

    layoutManager = new LayoutManager();
    layoutManager.setUsers(this, assetManager);

    audioManager = new AudioManager(this, assetManager);
    audioManager.prepareAudio();

    viewPort.setBackgroundColor(new ColorRGBA(.7f, .8f, 1f, 1f) );
    flyCam.setMoveSpeed( 0f );
    
    initParticles();        
    setUpLight();
    setUpScene();        
    setUpKeys();
    setUpCharacter();
    initHUD();

    
    layoutManager.generateGame();
    layoutManager.generateNewFloor();

    
    
    stateManager.detach(started);
    stateManager.attach(running);
    gameInitialized = true;
    
}[/java]

Here is some sample code illustrating the case. It’s a simple app so I think you could copy, paste and run it and it should work.
I know I should be adding tpf to time rather than increment, but then doing the IF statements would be a little trickier. It doesn’t even get that far anyways.

[java]
package mygame;

import com.jme3.app.SimpleApplication;
import com.jme3.audio.AudioNode;

public class AudioTest extends SimpleApplication
{

private AudioNode book;
private AudioNode cabinet;
private AudioNode correct;
private AudioNode door;
private AudioNode incorrect;
private AudioNode idCard;
private AudioNode select;
private AudioNode waterGulp;

private float time;


public static void main(String[] args)
{
    AudioTest app = new AudioTest();
    app.start();
}

public void simpleInitApp()
{
    prepareAudio();
    time = 0f;
        
}

public void simpleUpdate(float tpf)
{
    time++; 
    
    if (time == 10000)
    {
        playBook();
    }
    if (time == 20000)
    {
        playCabinet();
    }
       
}

public void prepareAudio()
{
    
    cabinet = new AudioNode(assetManager, "Sounds/Cabinet.ogg", false);
    rootNode.attachChild(cabinet);

    
    book = new AudioNode(assetManager, "Sounds/Book.ogg", false);
    rootNode.attachChild(book);

    
    correct = new AudioNode(assetManager, "Sounds/Correct.ogg", false);
    rootNode.attachChild(correct);

    
    door = new AudioNode(assetManager, "Sounds/Door.ogg", false);
    rootNode.attachChild(door);

    
    idCard = new AudioNode(assetManager, "Sounds/IDCard.ogg", false);
    rootNode.attachChild(idCard);

    
    incorrect = new AudioNode(assetManager, "Sounds/Incorrect.ogg", false);
    rootNode.attachChild(incorrect);

   
    select = new AudioNode(assetManager, "Sounds/Select.ogg", false);
    rootNode.attachChild(select);

    
    waterGulp = new AudioNode(assetManager, "Sounds/Water Gulp.ogg", false);
    rootNode.attachChild(waterGulp);

}


public void playBook()
{
    book.playInstance();
}
public void playCabinet()
{
    cabinet.playInstance();  
}
public void playCorrect()
{
    correct.play();   
}
public void playDoor()
{
    door.play();   
}
public void playIDCard()
{
    idCard.play();   
}
public void playIncorrect()
{
    incorrect.play();   
}
public void playSelect()
{
    select.play();   
}
public void playWaterGulp()
{
    waterGulp.play();   
}

}
[/java]

Now that I think about it you don’t have the sound files anyways… hmm. I’m not sure how to show you a sample. I guess if you tested that with your own sound files and it works that might mean that my files themselves are the issue. I know I got the file names right. Maybe their file size? Or their content? Is there some special formatting that needs to be done beforehand?

I resolved my problem. After looking at my last post I figured it couldn’t hurt to try changing my files to .wav format. For some reason, this worked and .ogg didn’t. I’m not sure what I was doing wrong, maybe something with how I was formatting them, or it could be something to look further into.

1 Like
@BrianMan said: I resolved my problem. After looking at my last post I figured it couldn't hurt to try changing my files to .wav format. For some reason, this worked and .ogg didn't. I'm not sure what I was doing wrong, maybe something with how I was formatting them, or it could be something to look further into.

Do you have the ogg libraries as a dependency in your project? Still, I’d have expected an error in that case… at least to the log.

@pspeed said: Do you have the ogg libraries as a dependency in your project? Still, I'd have expected an error in that case... at least to the log.

JOAL should have this as a dependancy already, as there are ogg specific classes for loading and streaming. So likely it was a formatting issue.

@BrianMan
What software did you use to convert the files to .ogg? Or what is the reported format for the download .ogg files? And… what software did you use to convert them to .wav?

Only curious because I’d like to avoid the same issue in the future.

@t0neg0d said: JOAL should have this as a dependancy already, as there are ogg specific classes for loading and streaming. So likely it was a formatting issue.

@BrianMan
What software did you use to convert the files to .ogg? Or what is the reported format for the download .ogg files? And… what software did you use to convert them to .wav?

Only curious because I’d like to avoid the same issue in the future.

This is what I meant:

Just checking to see if he changed the default dependencies. If you remove the ogg support (ie: remove those libraries) then ogg files won’t load… but you will get an error. JOAL doesn’t really have anything to do with it.

So it’s probably bad ogg files… but it never hurts to check. After all, I wouldn’t have expected someone to end up with their guiNode as a child of the rootNode but stuff happens when you are trying to sort out problems.

@pspeed said: So it's probably bad ogg files... but it never hurts to check. After all, I wouldn't have expected someone to end up with their guiNode as a child of the rootNode but stuff happens when you are trying to sort out problems.

True true!

At them level I’m at, I have no idea about how I formatted them. I can tell you I used audacity for the ogg and wav files. Unfortunately I know next to nothing about different formatting terms and techniques, I just opened them up, trimmed them to the audio I wanted, and exported them as ogg. Anyways, my problem is now fixed! Thank you.