Thanks in advance for your help!
I am building an Arkanoid/Breakout style game, and am running into some issues on account of body removal (at least that's where I suspect that the issue is). When I run the game, most of the time everything works just fine, but every now and then, the game crashes. during this crash, all of the sprites attached to bodies disappear, and any sprite without a body is left on the screen. There are no LogCat errors, but there is a drastic slowdown in FPS. Also, I noticed that sometime the contacts are registered twice before the code in the contact listener is fired (you can see where I implement the log file output in the ContactListener below).
In the game, instead of bricks, I am using bolts, and the code that I used to generate the bolts is listed below. After that, I show how I am using a ContactListener, and after that I give the code for removing the body/sprite combination. Any help or suggestions are greatly appreciated!
Thanks,
Adam
Creating the body/sprite items that will be removed when they are contacted.
//BOLTS
final FixtureDef boltFixtureDef = PhysicsFactory.createFixtureDef(1, 1.0f, 0.1f);
final Body[] boltBody=new Body[BOLT_NUMBER];
final Sprite[] boltSprite=new Sprite[BOLT_NUMBER];
final PhysicsConnector[] facePhysicsConnector= new PhysicsConnector[BOLT_NUMBER];
String[] bodyNames=new String[BOLT_NUMBER];
for(int i=0; i<BOLT_NUMBER; i++){
boltSprite[i] = new Sprite(-100,-100, sceneManager.mBolt, sceneManager.activity.getVertexBufferObjectManager());
boltSprite[i].setCullingEnabled(true);
boltBody[i] = PhysicsFactory.createBoxBody(mPhysicsWorld, boltSprite[i], BodyType.KinematicBody, boltFixtureDef);
mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(boltSprite[i], boltBody[i], true, true));
bodyNames[i]="bolt"+Integer.toString(i);
boltBody[i].setUserData(bodyNames[i]);
facePhysicsConnector[i] = mPhysicsWorld.getPhysicsConnectorManager().findPhysicsConnectorByShape(boltSprite[i]);
boltBody[i].setTransform(xLocations[i]/PMR, yLocations[i]/PMR, 0);}
for(int i=0; i<BOLT_NUMBER; i++){
scene.attachChild(boltSprite[i]);}
The ContactListener:
final ContactListener contact = new ContactListener(){
#Override
public void postSolve(Contact contact, ContactImpulse impulse) {
String a = (String) contact.getFixtureA().getBody().getUserData();
String b = (String) contact.getFixtureB().getBody().getUserData();
if(a != null && b != null) {
for(int i=0; i<BOLT_NUMBER; i++){
if(a.equals("wrench") && b.equals(facePhysicsConnector[i].getBody().getUserData()) ||
a.equals(facePhysicsConnector[i].getBody().getUserData()) && b.equals("wrench")) {
//boltCollision[i]=true;
Log.i("contact","bolt contact" + i);
myDestroyer(facePhysicsConnector[i], true);
Log.i("contact","bolt POSTcontact" + i);}
else if((a.equals("wrench") && b.equals("paddle")) ||
(a.equals("paddle") && b.equals("wrench"))) {
Log.i("contact","paddle contact");
SpringNoise=true;}
else if((a.equals("wrench") && b.equals("door")) ||
(a.equals("door") && b.equals("wrench"))) {
Log.i("contact","door contact");
WoodNoise=true;}
}}
}
#Override
public void endContact(final Contact contact) {}
#Override
public void beginContact(Contact contact) {}
#Override
public void preSolve(Contact contact, Manifold oldManifold) {}
};
And here is the code to remove the body/sprite combo:
private void myDestroyer(final PhysicsConnector facePhysicsConnector2, final Boolean bodyToo){
if(killingInProcess == false){
killingInProcess = true;
//final PhysicsConnector facePhysicsConnector2 = mPhysicsWorld.getPhysicsConnectorManager().findPhysicsConnectorByShape(myShape);
sceneValue.engine.registerUpdateHandler(new IUpdateHandler(){
#Override
public void onUpdate(float pSecondsElapsed) {
sceneValue.engine.unregisterUpdateHandler(this);
sceneValue.engine.runOnUpdateThread(new Runnable(){
#Override
public void run() {
Log.i("contact","removing bolt");
mPhysicsWorld.unregisterPhysicsConnector(facePhysicsConnector2);
Vibrator v = (Vibrator) sceneValue.activity.getSystemService(Context.VIBRATOR_SERVICE);
//myFixture.getBody().destroyFixture(myFixture);
if(bodyToo == true){
mPhysicsWorld.destroyBody(facePhysicsConnector2.getBody());
}
facePhysicsConnector2.getShape().registerEntityModifier(new ParallelEntityModifier(new RotationModifier(3,180,0),
new ScaleModifier(3, 1, 0)));
sceneValue.mBlockSound.play();
v.vibrate(75);
//mScene.detachChild(mySprite);
System.gc();
killingInProcess = false;
}
});
}
#Override
public void reset() {
}
});
}}
I'm not sure about the crashing problem, but I do know why the removal of bodies happens too late.
The method postSolve is called from the UpdaeThread. But instead of removing the body there, you register another update handler (So it's code runs in the next game update). And in this code, you run another code in runOnUpdateThread so it runs again in the next game update. You skipped 2 updates for no reason :) This might create problems (It probably does)
I suggest you to fix this, this look up for another problems (This may serious and it's easily solved)
I'm not sure, but I think that the problem might be in your removing of sprites. I met the problem and now I'm trying to kill bodies in update handler like this(method onUpdate in IUpdateHandler):
final PhysicsConnector physicsConnector = physicsWorld
.getPhysicsConnectorManager().findPhysicsConnectorByShape(
body);
body.clearEntityModifiers();
physicsWorld.unregisterPhysicsConnector(physicsConnector );
physicsWorld.destroyBody(physicsConnector.getBody());
scene.detachChild(body);
activity.getEngine().unregisterUpdateHandler(this);
Related
#EventHandler
public void playerInteraction(PlayerInteractEvent event)
{
Action action = event.getAction();
Player player = event.getPlayer();
Block block = event.getClickedBlock();
if (action.equals(Action.RIGHT_CLICK_BLOCK))
{
if (block.getType().equals(Material.NETHER_WART_BLOCK))
{
player.setHealth(player.getHealth() -1);
player.playSound(player.getLocation(), Sound.ENTITY_PLAYER_HURT, 10, 1);
}
else if (block.getType().equals(Material.DIAMOND_BLOCK))
{
player.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 1000, 2));
player.playSound(player.getLocation(), Sound.ENTITY_SPLASH_POTION_BREAK, 10, 1);
}
else if(block.getType().equals(Material.EMERALD_BLOCK))
{
if (player.getHealth() != 20)
{
player.setHealth(player.getHealth() + 1);
player.playSound(player.getLocation(), Sound.ENTITY_EXPERIENCE_ORB_PICKUP, 10, 1);;
}
if (player.getHealth() == 20)
{
player.sendMessage(ChatColor.DARK_RED + "You are already at full health!");
}
}
}
}
For some reason, all of these things happen twice whenever I right click the designated blocks. Anyone know why? I have posted the entire method, it's a player interaction event.
Thanks :)
First of all, make sure yo haven't registered the Listener class containing the event handler twice.
If that's not the case, according to this thread on the spigot forums, since Mojang added the left hand slot to Minecraft some events like PlayerInteractEvent or InventoryClickEvent will be called twice (once for each hand).
One possible fix is to "disable" the left hand on the event handler:
#EventHandler
public void onPlayerInteraction(PlayerInteractEvent event) {
if(event.getAction() == Action.RIGHT_CLICK_BLOCK && event.getHand() == EquipmentSlot.HAND) {
//Do something once
}
}
If you require that both hands could be used to trigger the event you could do the following:
First time the code gets executed you add the player to a list.
Before executing the code you check if the player is in the list. If it's in the list it means the code was executed once so you can skip it.
Schedule a task to remove the player from the list some ticks later.
The code could be as follows:
public class Foo implements Listener {
//An instance of the main plugin class
private MainClass plugin;
private List<UUID> playerBlacklist = new ArrayList<>();
#EventHandler
public void onPlayerInteractEvent(PlayerInteractEvent event) {
if(playerBlacklist.contains(event.getPlayer().getUniqueId)) {
return;
} else {
blacklistPlayer(event.getPlayer());
}
//Do something
}
private void blacklistPlayer(UUID uuid) {
playerBlacklist.add(uuid);
BukkitRunnable runnable = new BukkitRunnable(){
#Override
public void run() {
playerBlacklist.remove(uuid);
}
}
runnable.runTaskLaterAsynchronously(plugin, 5L);
}
}
Let me know if this solved your issue.
I'm using a recursive method which implements the use of the SwingWorker class to do a research in one folder and all its subfolders - in the local hard drive.
Basically works fine but I'm stuck when I want to stop the SwingWorker method: when the user change the 'source folder' (I'm using a JTree - JAVAFX - to show all the folders in the local hard drive), I want to stop the current 'SwingWorker research' in that folder and start a new one, with the newest 'source path' results choosed from the user.
All the results of the research are stored in a private ObservableList - and updated everytime in the done() method, just by filling one TableView - JavaFX: so, when the user change the 'source path' I have to clean the results of the previous research.
Start method:
private static ObservableList<msg> data = FXCollections.observableArrayList();
private static SwingWorker<Void, Void> worker;
private static String currentFolder;
#Override
public void start(Stage primaryStage) throws Exception {
// TODO Auto-generated method stub
stage = primaryStage;
primaryStage.setScene(new Scene(createContent()));
styleControls();
primaryStage.initStyle(StageStyle.UNDECORATED);
primaryStage.setMaximized(true);
primaryStage.setFullScreen(false);
primaryStage.show();
msgp = new MsgParser();
}
createContent() method- recursive function its called here:
public Parent createContent() {
tree.getSelectionModel().selectedItemProperty().addListener( new ChangeListener<Object>() {
#Override
public void changed(ObservableValue observable, Object oldValue,
Object newValue) {
TreeItem<File> selectedItem = (TreeItem<File>) newValue;
currentFolder = selectedItem.getValue().getAbsolutePath();
// I want to stop here the previous SwingWorker call : the tree
// ChangeListener event is called when the user change the
// source folder of the research, by selecting one TreeItem on it.
if(worker!= null)
worker.cancel(true);
//Here I clean previous results
data.clear();
TV.setItems(data);
//And I call again the method with the new source Folder
ListMail(new File(currentFolder));
}
});
}
ListMail() method: [recursive SwingWorker]
private void ListMail(File dir) {
worker = new SwingWorker<Void, Void>() {
#Override
protected Void doInBackground() throws Exception {
File[] directoryListing = dir.listFiles();
if (directoryListing != null) {
for (File child : directoryListing) {
if(!worker.isCancelled()) {
if(child != null){
if(!child.isDirectory()) {
if(child.getAbsolutePath().substring(child.getAbsolutePath().lastIndexOf('.')+1).equals("msg")) {
Message message = msgp.parseMsg(child.getPath());
String percorsoMail = child.getAbsolutePath().toUpperCase();
if(message != null) {
String fromEmail = message.getFromEmail();
String fromName = message.getFromName();
String subject = message.getSubject();
String received = message.getDate().toString();
String name;
if(fromEmail != null)
name = fromName + "(" + fromEmail + ")";
else name = fromName;
msg Message = new msg(name, subject, received);
if(!data.contains(Message))
data.add(Message);
//I use the Platform.runLater to
// take count of the number of results found
//It updates the GUI - works fine
Platform.runLater(new Runnable() {
#Override public void run() {
if(data != null && data.size() > 0)
setStatusLabel(data.size());
else
setStatusLabel(0);
}
});
}
}
} else {
/**
* Recursive call here : I do the research
* for the subfolders
*/
ListMail(child);
}
} else {
}
}
}
}
return null;
}
// Update GUI Here
protected void done() {
// I refresh here the TableView: works fine on-the-fly added results
TableView.setItems(data);
TableView.refresh();
}
};
//This doesn't do anything
if(!worker.isCancelled())
worker.execute();
}
Basically, the issue is that the SwingWorker thread never stop, I'm thinking because of the recursive calls which creates new pid process at every run or something ?
Also by using a dedicated external button, which I prefer to avoid, gives no results:
refreshBtn.setOnAction(e -> {
//Handle clicks on refreshBtn button
worker.cancel(true);
});
After I click on TreeItem to change source-folder, it just delete all the ObservableList elements created at that moment, but the previous research don't stop.
Everything works fine instead if I wait the research its finished - but this can works only when I'm in a deep-level folder, while I can't obviously wait when the research start with the "C:\" folder.
Ok so that's here how I managed this by using javafx.concurrent.
Just to point my experience with this, it seems using a recursive background Task for potentially long computations, such as scanning the Whole local drive like in my example, it's very memory consuming - also because I stored some results of this background computation in static local variables to access them faster: the result was a data-structure (ObservableList) with over 5000+ instances of a custom class to represent that specific data computed and then the OutOfMemoryError message or the background thread just going like in 'stand-by' without any advice after running for long time (waiting for garbage collection?).
Anyway here's the code that sum up how I solved: the threads are correctly closed. By the way, sometimes, there's a little 'GUI delay' due to cleaning the GUI on the isCancelled() method check: the GUI swing between clear/not clear, because in my opinion it keeps get filled by the results of the previous tasks in the recursion.
private static BackgroundTask backgroundTask;
private static Thread thread;
tree.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<Object>() {
#Override
public void changed(final ObservableValue observable, final Object oldValue, final Object newValue) {
//I close previous running background tasks if there's any
if (backgroundTask != null) {
while (backgroundTask.isRunning()) {
backgroundTask.cancel(true);
// reset GUI nodes here used to show results of the previous thread
}
}
backgroundTask = new BackGoundTask();
thread= new Thread(backgroundTask);
thread.setDaemon(true);
thread.start();
//This will be called only when latest recursion is finished, not at every run
backgroundTask.setOnSucceeded(e -> {});
}
});
BackgroundTask class:
public static class BackgroundTask extends Task<Object> {
// .. variables used by the task here
//constructor: initialize variables at every run of the Task
public BackgroundTask() {
}
#Override
protected Object call() throws Exception {
if (!isCancelled()) {
// ... Do all background work here
Platform.runLater(new Runnable() {
#Override
public void run() {
// GUI progress can goes here
}
});
//recursion here
if(something) {
//...
} else {
call();
}
} else {
//user want to cancel task: clean GUI nodes
}
return null;
}
}
I had an issue where Text to Speech would not speak anything. I realised this was due to the fact that I was attempting to call 'Speak()' before TTS had initialised.
I need to wait until TTS has initialised, so that I can call 'Speak()' successfully. I thought doing something along the lines of this would work:
#Override
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
mTTSInitialised = true;
} else {
Log.e("TTS", "Initialisation Failed!");
}
}
...
while(!mTTSInitialised){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
But this fails to initialise at all. Is there a way to do this effectively?
The initialisation of the Text to Speech engine is asynchronous, which is why you realised you have to 'wait' for it to complete, before requesting that it processes an utterance.
Even when it eventually initialises successfully, it can be subsequently killed by the system, or it can of course fail to initialise, so you always need to be ready to handle a request to speak, where the engine isn't prepared.
Add the following helper class
public class PendingTTS {
private String pendingUtterance;
private int pendingQueueType;
public String getPendingUtterance() {
return this.pendingUtterance;
}
public void setPendingUtterance(#NonNull final String pendingUtterance) {
this.pendingUtterance = pendingUtterance;
}
public int getPendingQueueType() {
return this.pendingQueueType;
}
public void setPendingQueueType(final int pendingQueueType) {
this.pendingQueueType = pendingQueueType;
}
}
Assuming you're using an Activity, you need to declare the following variables:
private volatile PendingTTS pendingTTS;
private static final int MAX_INIT_ATTEMPTS = 4;
private volatile int initCount;
and initialise the Text to Speech object in onCreate()
tts = new TextToSpeech(YOURActivity.this, YOURonInitListener);
In your onInitListener you would check if there is any pending speech:
#Override
public void onInit(final int status) {
switch (status) {
case TextToSpeech.SUCCESS:
initCount = 0;
// Set up tts stuff
tts.setOnUtteranceProgressListener(YOURprogressListener);
if (pendingTTS != null) {
// We have pending speech, process it and check the result
int speechResult = tts.speak(pendingTTS.getPendingUtterance(),pendingTTS.getPendingQueueType(),
// remaining tts variables here)
switch (speechResult){
case TextToSpeech.SUCCESS:
// Result was successful
pendingTTS = null;
break;
case TextToSpeech.ERROR:
// Speech failed
// Check if it has repeatedly failed up to the max attempts
if(initCount < MAX_INIT_ATTEMPTS){
initCount ++;
tts = new TextToSpeech(YOURActivity.this, YOURonInitListener);
} else {
// Totally broken - let the user know it's not working
}
break;
}
} else {
// there was nothing to process
}
break;
case TextToSpeech.ERROR:
// Check if it has repeatedly failed up to the max attempts
if(initCount < MAX_INIT_ATTEMPTS){
initCount ++;
tts = new TextToSpeech(YOURActivity.this, YOURonInitListener);
} else {
// Totally broken - let the user know it's not working
}
break;
}
I've glued the above together from my code - where the speech and initialisation methods are all separated, but I tried to give you an overview above of everything you need to handle.
Elsewhere in your code, when you make a tts.speak(//stuff here) request, you need to check the result as demonstrated above, to make sure it was successful. Again, in my code, this is separated into one single method. If it does fail, you need to set the PendingTTS parameters prior to attempting to initialise again:
pendingTTS = new PendingTTS();
pendingTTS.setPendingQueueType(// your queue type);
pendingTTS.setPendingUtterance(// your utterance);
It is is successful, make sure pendingTTS is set to null.
The overall design is that if the initialisation failed, it will attempt to initialise again, up to the maximum allowed attempts. If the speech fails, it will attempt to initialise the engine again, firstly setting the PendingTTS parameters.
Hope you managed to follow that.
Hmm..
Not a very good idea.
You can try to add the text to the TTS queue and let it do it's work. This snippet can be inside button click, etc as:
tts.speak(toSpeak, TextToSpeech.QUEUE_ADD, null);
Small tutorial that would help.
I am having an issue where calling JComboBox.setSelectedIndex(0) causes
my program to crash. Any help would be greatly appreciated!!
On itemStateChanged() starts a new Thread to handle UpdateAllForms.
UpdateAllForms calls updateComboModel() which Queries an SQL Database to update the ComboBoxModel and adds an additional option 'Select...'
This all works fine, however if i add JComboBox.setSelectedIndex(0) the
program crashes with no exception etc. I assume the issue is with threading?
itemStateChanged() Method:
public void itemStateChanged(ItemEvent e) {
if (e.getStateChange() == ItemEvent.DESELECTED) {
Runnable updateRunnable = new UpdateAllForms(e.getSource());
new Thread(updateRunnable).start();
}
}
UpdateAllForms Class:
// <<=== UpdateAllForms Class ===>>
// Only Updates Forms below the Current Form
// Must be ran as a Separate Thread due to swing concurrency
// ==============================================================================
public class UpdateAllForms implements Runnable {
Object source = null;
public UpdateAllForms(Object source) {
this.source = source;
}
#Override
public void run() {
// TODO Auto-generated method stub
boolean shouldUpdate = false;
Logger.write("PropConfDialog.updateAllForms");
// Loop through Forms
for (int formCount = 0; formCount < dataInputForms.get(1).size(); formCount++) {
Component curForm = dataInputForms.get(1).get(formCount);
// Update Forms after current form
if (shouldUpdate) {
if (curForm instanceof JSQLComboPanel) {
JSQLComboPanel panel = (JSQLComboPanel) curForm;
// Resets the where String
panel.setWhereString(getInputString(panel.getInputID()));
panel.updateComboModel();
shouldUpdate = true;
continue;
} else if (curForm instanceof JSQLLabelPanel) {
JSQLLabelPanel panel = (JSQLLabelPanel) curForm;
panel.setWhereString(getInputString(panel.getInputID()));
panel.updateLabel();
shouldUpdate = true;
Logger.write("LABEL CAN CARRY OUT");
continue;
}// End if/else
} // End should update
if (source == ((JSQLComboPanel) dataInputForms.get(1).get(formCount)).getComboBox()) {
shouldUpdate = true;
}// End if
}// End Loop
}// End updateAllCombos()
}// End UpdateAllForms Class
JSQLComboPanel Class - updateComboModel Method !!THIS IS THE ISSUE!!! if I call
combo.setSelectedIndex(0) in this method the program crashes.
public void updateComboModel(){
if(comboType == TYPE_DRIVEN_COMBO){
ArrayList values = SQLTools.getColValues(lkTable, lkNameCol);
combo.setModel(new DefaultComboBoxModel(values.toArray(new String[values.size()])));
}else if(comboType == TYPE_WHERE_COMBO){
ArrayList values = SQLTools.executeJoin(fkTable, fkIDCol, fkNameCol, lkTable, lkIDCol, lkNameCol, whereString);
combo.setModel(new DefaultComboBoxModel(values.toArray(new String[values.size()])));
}else if(comboType == TYPE_WHERE_LINKED_COMBO){
ArrayList values = SQLTools.executeLinkTableJoin(fkTable, fkIDCol, fkNameCol, linkTable, fkIDCol, lkIDCol, lkTable, lkIDCol, lkNameCol,whereString);
combo.setModel(new DefaultComboBoxModel(values.toArray(new String[values.size()])));
}//End if/else
combo.insertItemAt("Select...", 0);
//combo.setSelectedIndex(0);
combo.repaint();
}//End updateComboModel()
If anybody can shed any light, that would be fantastic! I am fairly new to Java especially Threading!
Thanks again
Tim
The problem is (almost certainly) related to the fact that you are modifying the state of Swing Components on the wrong thread.
The general rule is:
Code that depends on or modifies the state of a Swing Component should be executed on the Event Dispatch Thread.
A violation of this rule may sometimes be hard to detect - particularly, when only a model is modified, which does not necessarily have a connection to a GUI component!
However, in your case, the main problem is more obvious, because there is (at least) the problematic call
combo.setModel(new DefaultComboBoxModel(values.toArray(new String[values.size()])));
which happens on an own thread, and modifies the Swing component directly.
As suggested in the comments, you should definitiely consider using a SwingWorker. More details about the SwingWorker (and threading in Swing in general) can be found in the article about Concurrency In Swing
A quick workaround for your problem could be the following:
...
// Remove this line
//combo.setModel(new DefaultComboBoxModel(values.toArray(new String[values.size()])));
// Replace it with this line
setModelOnEDT(combo, new DefaultComboBoxModel(values.toArray(new String[values.size()]));
and create a method like this:
private static void setModelOnEDT(
final JComboBox comboBox, final ComboBoxModel model)
{
SwingUtilities.invokeLater(new Runnable()
{
#Override
public void run()
{
comboBox.setModel(model);
}
});
}
This is certainly not the prettiest solution, but the simplest, until you modify the code to use a SwingWorker.
I'am doing simple libgdx game. I have lag (game stop for 0. 5 sec) when i use sound.play()
edit this bug apear on android 4.0 on 2.3 everything is running fine.
method. I play sound by this code:
if(CollisionDetector.detect(touchArea, hoodie.getTouchArea())){
GameScreen.totalScore++;
setPosition();
System.out.println("played");
Assets.eatSound.play();
}
And i use this method to load sound:
static long waitForLoadCompleted(Sound sound,float volume) {
long id;
while ((id = sound.play(volume)) == -1) {
long t = TimeUtils.nanoTime();
while (TimeUtils.nanoTime() - t < 100000000);
}
return id;
}
What am i doing wrong? Or what can i do to fix this lag ?
edit:
I have just tried to do thread with sound.play() but it also doesn't work:
new Thread(new Runnable() {
#Override
public void run() {
// do something important here, asynchronously to the rendering thread
// post a Runnable to the rendering thread that processes the result
Gdx.app.postRunnable(new Runnable() {
#Override
public void run() {
// process the result, e.g. add it to an Array<Result> field of the ApplicationListener.
eatSound2.play();
}
});
}
}).start();
My Sound asset class looks like this but i still have lag with sound.
package com.redHoodie;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.utils.Disposable;
public class SoundEffect implements Disposable {
private static final int WaitLimit = 1000;
private static final int ThrottleMs = 100;
Sound eatSound;
Sound endSound;
public SoundEffect(){
eatSound = Gdx.audio.newSound(Gdx.files.internal("eatSound.ogg"));
endSound = Gdx.audio.newSound(Gdx.files.internal("sadend.wav"));
checkedPlay(eatSound);
}
protected long checkedPlay (Sound sound) {
return checkedPlay(sound, 1);
}
protected long checkedLoop (Sound sound) {
return checkedLoop(sound, 1);
}
protected long checkedPlay (Sound sound, float volume) {
int waitCounter = 0;
long soundId = 0;
boolean ready = false;
while (!ready && waitCounter < WaitLimit) {
soundId = sound.play(volume);
ready = (soundId != 0);
waitCounter++;
try {
Thread.sleep(ThrottleMs);
} catch (InterruptedException e) {
}
}
return soundId;
}
protected long checkedLoop (Sound sound, float volume) {
int waitCounter = 0;
long soundId = 0;
boolean ready = false;
while (!ready && waitCounter < WaitLimit) {
soundId = sound.loop(volume);
ready = (soundId != 0);
waitCounter++;
try {
Thread.sleep(ThrottleMs);
} catch (InterruptedException e) {
}
}
return soundId;
}
#Override
public void dispose() {
// TODO Auto-generated method stub
}
}
I had the same problem. It was because my .mp3 file was too short. Mine was 0.167 seconds long. I added 1.2 seconds of silence with Audacity, and it fixed the problem.
Lately I run into the same issue (except I'm using wav instead mp3 files). My app was lagging when I play many (like 10 or 20) sounds at the same time (same render method). "Solved" this by playing only 1 sound at the time. Generally it's hard to distinct many sounds at the same time. Also on desktop it works fine, but problem appears on android (9 or 8).
If someone still facing this issue as me there is the alternative solution with one limitation: no option to use sound id.
You can change default LibGDX behavior and use AsynchronousAndroidAudio by overriding this method in your AndroidLauncher class:
#Override
public AndroidAudio createAudio(Context context, AndroidApplicationConfiguration config) {
return new AsynchronousAndroidAudio(context, config);
}
See the official documentation for more info and also the pull request
Also, if for any reasons you need sound id you can take this implementation as an example and find a workaround for your project.
Fix is available starting from LibGDX 1.9.12