Audio on Android – Looping (47 posts)

Topic tags: android, audio
  • Profile picture of kentsusai kentsusai said 6 months ago:

    Hello,

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

    I initialize my audio with the following

    AudioNode audio_fire = new AudioNode(assetManager, "Sound/sound001.ogg", false);
    audio_fire.setLooping(false);
    audio_fire.setVolume(2);
    rootNode.attachChild(audio_fire);

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

    audio_fire.playInstance();

    The sound plays but it loops!

    Am I forgetting something?

    Thanks

  • Profile picture of Momoko_Fan Momoko_Fan366p said 6 months ago:

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

  • Profile picture of kentsusai kentsusai said 6 months ago:

    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!!

    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
        }
    }

    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

  • Profile picture of Momoko_Fan Momoko_Fan366p said 6 months ago:

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

  • Profile picture of kentsusai kentsusai said 6 months ago:

    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*

  • Profile picture of Momoko_Fan Momoko_Fan366p said 6 months ago:

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

  • Profile picture of kentsusai kentsusai said 5 months, 4 weeks ago:

    Thanks :-)

  • Profile picture of wezrule wezrule190p said 5 months, 4 weeks ago:

    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.

  • Profile picture of normen normen1271p said 5 months, 4 weeks ago:

    @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.

  • Profile picture of wezrule wezrule190p said 5 months, 4 weeks ago:

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

  • Profile picture of kentsusai kentsusai said 5 months, 4 weeks 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!

    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
        }
    }
    
  • Profile picture of kentsusai kentsusai said 5 months, 4 weeks ago:

    Same with Eclipse too. The audio keeps looping

    BTW, if I change line 56 to read

    if counter(counter < 10) {

    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)

  • Profile picture of kentsusai kentsusai said 5 months, 3 weeks ago:

    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!!!! :-(

    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 :-)

  • Profile picture of blackmasoon blackmasoon said 4 months, 4 weeks ago:

    @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?

  • Profile picture of pspeed pspeed805p said 4 months, 4 weeks ago:

    @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.