|
| 1 | +package jme3test.audio; |
| 2 | + |
| 3 | +import com.jme3.app.SimpleApplication; |
| 4 | +import com.jme3.audio.AudioData; |
| 5 | +import com.jme3.audio.AudioNode; |
| 6 | +import com.jme3.input.KeyInput; |
| 7 | +import com.jme3.input.controls.ActionListener; |
| 8 | +import com.jme3.input.controls.KeyTrigger; |
| 9 | +import com.jme3.input.controls.Trigger; |
| 10 | + |
| 11 | +/** |
| 12 | + * This test demonstrates that destroying and recreating the OpenAL Context |
| 13 | + * upon device disconnection is not an optimal solution. |
| 14 | + * |
| 15 | + * As shown, AudioNode instances playing in a loop cease to play after a device disconnection |
| 16 | + * and would require explicit restarting. |
| 17 | + * |
| 18 | + * This test serves solely to highlight this issue, |
| 19 | + * which should be addressed with a more robust solution within |
| 20 | + * the ALAudioRenderer class in a dedicated future pull request on Git. |
| 21 | + */ |
| 22 | +public class TestAudioDeviceDisconnect extends SimpleApplication implements ActionListener { |
| 23 | + |
| 24 | + public static void main(String[] args) { |
| 25 | + TestAudioDeviceDisconnect test = new TestAudioDeviceDisconnect(); |
| 26 | + test.start(); |
| 27 | + } |
| 28 | + |
| 29 | + private AudioNode audioSource; |
| 30 | + |
| 31 | + @Override |
| 32 | + public void simpleInitApp() { |
| 33 | + audioSource = new AudioNode(assetManager, |
| 34 | + "Sound/Environment/Ocean Waves.ogg", AudioData.DataType.Buffer); |
| 35 | + audioSource.setName("Waves"); |
| 36 | + audioSource.setLooping(true); |
| 37 | + rootNode.attachChild(audioSource); |
| 38 | + |
| 39 | + audioSource.play(); |
| 40 | + |
| 41 | + registerInputMappings(); |
| 42 | + } |
| 43 | + |
| 44 | + @Override |
| 45 | + public void onAction(String name, boolean isPressed, float tpf) { |
| 46 | + if (!isPressed) return; |
| 47 | + |
| 48 | + if (name.equals("play")) { |
| 49 | + // re-play active sounds |
| 50 | + audioSource.play(); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + private void registerInputMappings() { |
| 55 | + addMapping("play", new KeyTrigger(KeyInput.KEY_SPACE)); |
| 56 | + } |
| 57 | + |
| 58 | + private void addMapping(String mappingName, Trigger... triggers) { |
| 59 | + inputManager.addMapping(mappingName, triggers); |
| 60 | + inputManager.addListener(this, mappingName); |
| 61 | + } |
| 62 | + |
| 63 | +} |
0 commit comments