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.Gdx; 20 import com.badlogic.gdx.audio.Music; 21 import com.badlogic.gdx.graphics.GL20; 22 import com.badlogic.gdx.graphics.Texture; 23 import com.badlogic.gdx.graphics.g2d.BitmapFont; 24 import com.badlogic.gdx.graphics.g2d.SpriteBatch; 25 import com.badlogic.gdx.graphics.g2d.TextureRegion; 26 import com.badlogic.gdx.scenes.scene2d.Actor; 27 import com.badlogic.gdx.scenes.scene2d.Stage; 28 import com.badlogic.gdx.scenes.scene2d.ui.Skin; 29 import com.badlogic.gdx.scenes.scene2d.ui.Slider; 30 import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener; 31 import com.badlogic.gdx.scenes.scene2d.utils.ClickListener; 32 import com.badlogic.gdx.tests.utils.GdxTest; 33 34 public class MusicTest extends GdxTest { 35 36 Music music; 37 float songDuration = 183; 38 float currentPosition; 39 40 TextureRegion buttons; 41 SpriteBatch batch; 42 BitmapFont font; 43 44 Stage stage; 45 Slider slider; 46 boolean sliderUpdating = false; 47 48 @Override create()49 public void create () { 50 music = Gdx.audio.newMusic(Gdx.files.internal("data/8.12.mp3")); 51 music.play(); 52 53 buttons = new TextureRegion(new Texture(Gdx.files.internal("data/playback.png"))); 54 batch = new SpriteBatch(); 55 font = new BitmapFont(Gdx.files.internal("data/arial-15.fnt"), false); 56 57 stage = new Stage(); 58 Skin skin = new Skin(Gdx.files.internal("data/uiskin.json")); 59 slider = new Slider(0, 100, 0.1f, false, skin); 60 slider.setPosition(200, 20); 61 slider.addListener(new ChangeListener() { 62 @Override 63 public void changed (ChangeEvent event, Actor actor) { 64 if (!sliderUpdating && slider.isDragging()) music.setPosition((slider.getValue() / 100f) * songDuration); 65 } 66 }); 67 stage.addActor(slider); 68 69 Gdx.input.setInputProcessor(stage); 70 } 71 72 @Override resize(int width, int height)73 public void resize (int width, int height) { 74 batch.getProjectionMatrix().setToOrtho2D(0, 0, width, height); 75 } 76 77 @Override resume()78 public void resume () { 79 System.out.println(Gdx.graphics.getDeltaTime()); 80 } 81 82 @Override render()83 public void render () { 84 currentPosition = music.getPosition(); 85 Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); 86 batch.begin(); 87 batch.draw(buttons, 0, 0); 88 font.draw(batch, (int)currentPosition / 60 + ":" + (int)currentPosition % 60, 365, 35); 89 batch.end(); 90 91 sliderUpdating = true; 92 slider.setValue((currentPosition / songDuration) * 100f); 93 sliderUpdating = false; 94 stage.act(); 95 stage.draw(); 96 97 if (Gdx.input.justTouched()) { 98 if (Gdx.input.getY() > Gdx.graphics.getHeight() - 64) { 99 if (Gdx.input.getX() < 64) { 100 music.play(); 101 } 102 if (Gdx.input.getX() > 64 && Gdx.input.getX() < 128) { 103 music.stop(); 104 } 105 if (Gdx.input.getX() > 128 && Gdx.input.getX() < 192) { 106 music.pause(); 107 } 108 } 109 } 110 } 111 112 @Override dispose()113 public void dispose () { 114 batch.dispose(); 115 buttons.getTexture().dispose(); 116 music.dispose(); 117 } 118 } 119