• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*******************************************************************************
2  * Copyright 2011 See AUTHORS file.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  ******************************************************************************/
16 
17 package com.badlogic.gdx.tests;
18 
19 import com.badlogic.gdx.Files.FileType;
20 import com.badlogic.gdx.Gdx;
21 import com.badlogic.gdx.audio.Sound;
22 import com.badlogic.gdx.graphics.GL20;
23 import com.badlogic.gdx.scenes.scene2d.Actor;
24 import com.badlogic.gdx.scenes.scene2d.InputEvent;
25 import com.badlogic.gdx.scenes.scene2d.Stage;
26 import com.badlogic.gdx.scenes.scene2d.ui.Label;
27 import com.badlogic.gdx.scenes.scene2d.ui.Skin;
28 import com.badlogic.gdx.scenes.scene2d.ui.Slider;
29 import com.badlogic.gdx.scenes.scene2d.ui.Table;
30 import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
31 import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener;
32 import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
33 import com.badlogic.gdx.tests.utils.GdxTest;
34 import com.badlogic.gdx.utils.Align;
35 
36 public class SoundTest extends GdxTest {
37 	Sound sound;
38 	float volume = 0.5f;
39 	long soundId = 0;
40 	Stage ui;
41 	Skin skin;
42 
43 	@Override
create()44 	public void create () {
45 		sound = Gdx.audio.newSound(Gdx.files.getFileHandle("data/shotgun.ogg", FileType.Internal));
46 
47 		skin = new Skin(Gdx.files.internal("data/uiskin.json"));
48 		ui = new Stage();
49 		TextButton play = new TextButton("Play", skin);
50 		TextButton stop = new TextButton("Stop", skin);
51 		final Slider pitch = new Slider(0.1f, 4, 0.1f, false, skin);
52 		pitch.setValue(1);
53 		final Label pitchValue = new Label("1.0", skin);
54 		final Slider volume = new Slider(0.1f, 1, 0.1f, false, skin);
55 		volume.setValue(1);
56 		final Label volumeValue = new Label("1.0", skin);
57 		Table table = new Table();
58 		final Slider pan = new Slider(-1f, 1f, 0.1f, false, skin);
59 		pan.setValue(0);
60 		final Label panValue = new Label("0.0", skin);
61 		table.setFillParent(true);
62 
63 		table.align(Align.center | Align.top);
64 		table.columnDefaults(0).expandX().right().uniformX();
65 		table.columnDefaults(2).expandX().left().uniformX();
66 		table.add(play);
67 		table.add(stop).left();
68 		table.row();
69 		table.add(new Label("Pitch", skin));
70 		table.add(pitch);
71 		table.add(pitchValue);
72 		table.row();
73 		table.add(new Label("Volume", skin));
74 		table.add(volume);
75 		table.add(volumeValue);
76 		table.row();
77 		table.add(new Label("Pan", skin));
78 		table.add(pan);
79 		table.add(panValue);
80 		ui.addActor(table);
81 
82 		play.addListener(new ClickListener() {
83 			public void clicked (InputEvent event, float x, float y) {
84 				soundId = sound.play(volume.getValue());
85 				sound.setPitch(soundId, pitch.getValue());
86 				sound.setPan(soundId, pan.getValue(), volume.getValue());
87 			}
88 		});
89 
90 		stop.addListener(new ClickListener() {
91 			public void clicked (InputEvent event, float x, float y) {
92 				sound.stop(soundId);
93 			}
94 		});
95 		pitch.addListener(new ChangeListener() {
96 			public void changed (ChangeEvent event, Actor actor) {
97 				sound.setPitch(soundId, pitch.getValue());
98 				pitchValue.setText("" + pitch.getValue());
99 			}
100 		});
101 		volume.addListener(new ChangeListener() {
102 			public void changed (ChangeEvent event, Actor actor) {
103 				sound.setVolume(soundId, volume.getValue());
104 				volumeValue.setText("" + volume.getValue());
105 			}
106 		});
107 		pan.addListener(new ChangeListener() {
108 			public void changed (ChangeEvent event, Actor actor) {
109 				sound.setPan(soundId, pan.getValue(), volume.getValue());
110 				panValue.setText("" + pan.getValue());
111 			}
112 		});
113 		Gdx.input.setInputProcessor(ui);
114 	}
115 
116 	@Override
render()117 	public void render () {
118 		Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
119 		ui.act(Gdx.graphics.getDeltaTime());
120 		ui.draw();
121 	}
122 
123 	@Override
dispose()124 	public void dispose () {
125 		ui.dispose();
126 		skin.dispose();
127 		sound.dispose();
128 	}
129 }
130