Redeclaring Audio Assets

What is the proper way to destroy Audio Assets before loading a new one. My application seems to freeze when i attempt to reuse an AudioNode and redeclare the value for another. I also attempted to set the audio data however it still gives Freezes the application. Any idea what is causing this?

Code:
if(currentClip!=null){ currentClip = new AudioNode(SceneDirector.getInstance().getManager(), "Deck/SoundEffects/" + m, false); } else { AudioKey audioKey = new AudioKey("Deck/SoundEffects/" + m, false, false); AudioData data = SceneDirector.getInstance().getManager().loadAsset(audioKey); currentClip.setAudioData(data, audioKey); }

[java]

audioNode.stop();

audioNode.detachFromParent();

[/java]



xD ?

1 Like

I have tried

[java]if (currentClip != null) {

currentClip.removeFromParent();

currentClip.detachAllChildren();

} [/java]

however It still freezes. I even tried enqueuing it

Wait…wait …wait.



[java]

if(currentClip!=null){

…

}

else {

currentClip.throwsNewNullPointerException();

}

[/java]



xD.

lol srry I was trying to only give what was relevant :roll:



[java]try {

SceneApplication.getApplication().enqueue(new Callable() {



@Override

public Object call() throws Exception {



String m = f.getPath().split("/Deck/SoundEffects/")[1];

// if (currentClip == null) {

if (currentClip != null) {

currentClip.stop();

currentClip.detachAllChildren();

currentClip.removeFromParent();

}

currentClip = new AudioNode(SceneDirector.getInstance().getManager(), "Deck/SoundEffects/" + m, false);



// } else {

// AudioKey audioKey = new AudioKey("Deck/SoundEffects/" + m, false, false);

// AudioData data = SceneDirector.getInstance().getManager().loadAsset(audioKey);

// currentClip.setAudioData(data, audioKey);

//

// }



return null;

}

});

} catch (Exception e) {

currentClip = null;

fileLabel.setText("Error loading clip.");

JOptionPane.showMessageDialog(null, "Error loading clip.");

}[/java]

So you are saying the problem is that you are reusing the currentClip reference?



That is 100000000% not your issue. I don’t know why you are freezing but reusing a Java reference will not cause it.



Have you tried putting a debug message in your exception catch? If your showMessageDialog pops up behind the 3D window then it will seem like your app has frozen… though that code isn’t even relevant here, I guess since it’s the callable that’s doing the work.

Well actually this is for a filechooser thats made to find and if needed play an audio file; It’s this filechooser that actually freezes. I’ve tried debugging and it seems the only time it freezes is when I redeclare currentClip if its commented out or made to only be declared once then it works 100% of the time. Possibly their is something else interfering however but i’m not sure?



thanks for the responses

FileChooser = Swing = needs to be manipulated on AWT thread

1 Like

I tried wrapping the filechooser in an AWT EventQueue and after even though it made little sense wrapping the code above in the EventQueue as well however nothing changed. The first sound still plays and anything after freezes



placed it in the run method below

[java]Runnable call = new Runnable() {



@Override

public void run() {

try {



java.awt.EventQueue.invokeLater(new Runnable() {



@Override

public void run() {



}

});



} finally {

}

}

};

new Thread(call).start();[/java]

And you only call currentClip.play() from the JME render thread?

Well to be a little more specific currentClip exists in an Accessory class made for the the filechooser. I enqueued it in the SceneApplication as above so technically it should be in the JME render thread else the very first sound that I hear would not play I think

There was no play() in and of your code samples so I couldn’t be sure where you were calling it.



AudioNode.play() only works from the JME render thread but should throw a pretty decent exception if it isn’t called there.

Do you ever wait for your enqueued things to return? That could be bad. Like Future.get()?

no XD I actually though jst throwing it into a Callable and enqueuing it was all thats needed =p. I don’t seem to get any exceptions thrown at all.

Since I don’t know where you are calling play()… if you comment that out does it still hang?

I don’t seem to get any exceptions thrown at all.


It's because you put your code snippet inside a callable object xD!? And it's automatically caught by application in enqueue method xD!? I had the same problem in another thread when I was using the SaveGame.load() method. I was using it inside a callable object, and enqueuing it in application, and when my cade failed inside the callable, any exceptions wasn't thrown at all, and I couldn't know what was going on, and my app fought xD. Then the solution was *debug* that callable to know what's going on. threads = problems.

I commented out anytime i called play() or stop() from the audioNode as well as the JOptionPane.showmessage jst for good measure however it did not remedy the issue

As I said, it’s probably an exception that you don’t see. DEBUG your application to see what’s the wrong line.

Your app is hanging somewhere. You will have to figure out where with debugging… either printlns or by stepping through the code in a debugger.

Well I tried debugging for a couple days I also removed the throws exception on the callable and eventually the callable all together as well as commenting out the play() and stop() methods all together.

Finally even though I was told the issue was not when I declared a new AudioNode for the second time I tried using



[java]JOptionPane.showMessageDialog(null, “Check 1”);

currentClip = new AudioNode(asset, s, false);

JOptionPane.showMessageDialog(null, “Check 2”); [/java]



The Result was the first time both JOptionPane messages show, however; when I choose a second file in the fileChooser even if I close the file chooser and reopen it only “Check 1” shows. Any third or successive change file attempts results in nether “Check 1” or “Check 2” showing.