Audio on Android - Looping

Hello,



Has anyone tried audio on Android? I’m not having much luck.



I initialize my audio with the following



[java]AudioNode audio_fire = new AudioNode(assetManager, “Sound/sound001.ogg”, false);

audio_fire.setLooping(false);

audio_fire.setVolume(2);

rootNode.attachChild(audio_fire);[/java]



I play the sound on an event taking place with the following

[java]audio_fire.playInstance();[/java]



The sound plays but it loops!



Am I forgetting something?



Thanks

Perhaps the event happens so often that its like the sound plays in a loop?

haha i wish that was the problem!



playInstance() is only called when event is triggered. In fact, it is only called once!



BTW, I’ve tried setting it up in a clean project (see below). Works on the desktop but not on the Android!!



[java]package mygame;



import com.jme3.app.SimpleApplication;

import com.jme3.audio.AudioNode;

import com.jme3.material.Material;

import com.jme3.math.ColorRGBA;

import com.jme3.math.Vector3f;

import com.jme3.renderer.RenderManager;

import com.jme3.scene.Geometry;

import com.jme3.scene.shape.Box;



/**

  • test
  • @author normenhansen

    */

    public class Main extends SimpleApplication {



    private AudioNode audio_gun;

    private int counter = 0;



    public static void main(String[] args) {

    Main app = new Main();

    app.start();

    }



    @Override

    public void simpleInitApp() {

    Box b = new Box(Vector3f.ZERO, 1, 1, 1);

    Geometry geom = new Geometry(“Box”, b);



    Material mat = new Material(assetManager, “Common/MatDefs/Misc/Unshaded.j3md”);

    mat.setColor(“Color”, ColorRGBA.Blue);

    geom.setMaterial(mat);



    rootNode.attachChild(geom);

    try

    {

    audio_gun = new AudioNode(assetManager, “Sounds/sound003.ogg”);

    }

    catch (Exception e)

    {

    audio_gun = new AudioNode(assetManager, “assets/Sounds/sound003.ogg”);

    }





    audio_gun.setLooping(false);

    audio_gun.setVolume(2);

    rootNode.attachChild(audio_gun);



    //audio_gun.playInstance();



    }



    @Override

    public void simpleUpdate(float tpf) {

    //TODO: add update code

    if (counter < 1) {

    audio_gun.playInstance();

    counter++;

    }

    }



    @Override

    public void simpleRender(RenderManager rm) {

    //TODO: add render code

    }

    }[/java]



    The above code gives me an error on Android but not on the desktop. It says “Failed to load: assets/Sounds/sound003.ogg: java.lang.IllegalArgumentException”



    BTW I have the file sound003.ogg in the assets/Sounds/ folder and mobile/assets/Sounds/ folder

Yes you aren’t supposed to prefix the asset name with assets/

The asset/ prefix was only affixed to the catch statement. It was only put in there in anticipation of people posting, ‘try affixing assets/ to the path.’



The try statement isn’t prefixed with assets/



Not sure what you are suggesting… scratch head

The person who is maintaining the android build isn’t here … I created an issue for this in the issue tracker

Thanks :slight_smile:

did someone change the audioNode? because setting my streaming music to loop only plays the first 0.1 secs of it and keeps looping it.

@kentsusai said:
The asset/ prefix was only affixed to the catch statement. It was only put in there in anticipation of people posting, 'try affixing assets/ to the path.'
Not sure what you are suggesting... *scratch head*

You are using eclipse right? xD Include the assets folder in your classpath and don't use the prefix as momoko suggested.

@wezrule said:
did someone change the audioNode? because setting my streaming music to loop only plays the first 0.1 secs of it and keeps looping it.

Not that I know of, do you load the audio from elsewhere? e.g. http looping isn't supported afaik.

from the assets folder, it did work a few days ago

@normen



Actually, I tried the above by creating a project with JMP, creating the Android project within JMP and then building the Android project using ant debug and ant installd



I’ve commented out the catch statement (see below). Now I have the same problem I had in the beginning! The sound is only meant to play once but it plays in a loop!



[java]package mygame;



import com.jme3.app.SimpleApplication;

import com.jme3.audio.AudioNode;

import com.jme3.material.Material;

import com.jme3.math.ColorRGBA;

import com.jme3.math.Vector3f;

import com.jme3.renderer.RenderManager;

import com.jme3.scene.Geometry;

import com.jme3.scene.shape.Box;



/**

  • test
  • @author

    */

    public class Main extends SimpleApplication {



    private AudioNode audio_gun;

    private int counter = 0;



    public static void main(String[] args) {

    Main app = new Main();

    app.start();

    }



    @Override

    public void simpleInitApp() {

    Box b = new Box(Vector3f.ZERO, 1, 1, 1);

    Geometry geom = new Geometry("Box", b);



    Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");

    mat.setColor("Color", ColorRGBA.Blue);

    geom.setMaterial(mat);



    rootNode.attachChild(geom);

    //try

    //{

    audio_gun = new AudioNode(assetManager, "Sounds/sound003.ogg");

    //}

    //catch (Exception e)

    //{

    // audio_gun = new AudioNode(assetManager, "assets/Sounds/sound003.ogg");

    //}





    audio_gun.setLooping(false);

    audio_gun.setVolume(2);

    rootNode.attachChild(audio_gun);



    //audio_gun.playInstance();



    }



    @Override

    public void simpleUpdate(float tpf) {

    //TODO: add update code

    if (counter < 1) {

    audio_gun.playInstance();

    counter++;

    }

    }



    @Override

    public void simpleRender(RenderManager rm) {

    //TODO: add render code

    }

    }

    [/java]

Same with Eclipse too. The audio keeps looping





BTW, if I change line 56 to read



[java]if counter(counter < 10) {[/java]



it crashes as per my original problem (note, I didn’t mention this in my first post because it would play the sound once [with unwanted looping] and then crash upon playing the second)

Just letting you all know that for the interim I have been using SoundPool and MediaPlayer objects to handle my sound. Not my preferred option given that it is Android specific code!!! :frowning:



MediaPlayer is great for streaming big sound files like background music



SoundPool is great for small sound effects



And BTW, if you need access to the Activity and Context objects to initialise either of them, you can access them via the JmeSystem object :slight_smile:

@normen said:
You are using eclipse right? xD Include the assets folder in your classpath and don't use the prefix as momoko suggested.


Not that I know of, do you load the audio from elsewhere? e.g. http looping isn't supported afaik.


And What if I use JMonkeyEngine IDE and also receive “Failed to load: Sounds/sound.ogg: java.lang.IllegalArgumentException” ? I have no problem with loadding textures and models from my default assets folder, but can't load audio files. Why is that happening?
@wezrule said:
did someone change the audioNode? because setting my streaming music to loop only plays the first 0.1 secs of it and keeps looping it.


Streamed audio has never been loopable. It's very frustrating but I found that a long time ago. In Mythruna, I watch the node for Status.Stopped or whatever and then recreate the node.

That java.lang.IllegalArgumentException shows in android version of my app (writing about my previous post). How should I set the audio file path for android version of my game to run?

Is it not the same as for desktop version too? Like “Interface/Sounds/mysound.ogg”.

What should I use “Interface” on the begining of the path? I keep all my resources in the assets folder of JME project and i use: Textures/image.jpg for textures or Models/model.j3o for objects and that works on android. Why doesn’t it work for audio?

It does work, theres some other issue here, like the ogg cannot be decoded or something. In what class does the error actually occur? Did you try using a WAV file?

yes. I tried to use wav and it gives me the same java.lang.IllegalArgumentException. I’ll watch what DDMS say and post it here in a sec