[英]How to play music using libgdx
I have had trouble playing music using libgdx. 使用libgdx播放音乐时遇到麻烦。 My code is as follows:
我的代码如下:
package com.me.fixGame;
import java.util.Random;
import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;
//import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.utils.Scaling;
import com.sun.jmx.snmp.tasks.Task;
public class fixGame implements ApplicationListener {
SpriteBatch batch;
SpriteBatch spriteBatch;
Texture trash;
Texture paper;
SpriteBatch spritebatch;
Vector2 position;
Vector2 pas;
boolean collide;
boolean countMe=false;
Vector2 size;
Vector2 size2;
Vector2 pos;
Rectangle bounds;
float posSpeed=30;
Rectangle bounds2;
float delay = 1; // seconds
boolean counted= false;
int score = 3;
//Texture Gogreen;
String myScore;
Texture background;
CharSequence str = "Lives left: 3"; // = myScore;
CharSequence line = "Score: 0"; // = myScore;
String myLife;
int life=0;
BitmapFont font;
float x;
float y;
Sound sound;
boolean collision = false;
@Override
public void create() {
//Gogreen = new Texture(Gdx.files.internal("data/gogreenNow.jpg"));
background = new Texture(Gdx.files.internal("data/trash.png"));
x= background.getWidth();
y=background.getHeight();
//float delaySeconds = 1;
spriteBatch = new SpriteBatch();
trash = new Texture(Gdx.files.internal("data/trash.png"));
paper = new Texture(Gdx.files.internal("data/paper1.jpg"));
sound = Gdx.audio.newSound(Gdx.files.internal("data/testjava.mp3"));
position = new Vector2(100, 50);
pos = new Vector2(54, 14);
batch = new SpriteBatch();
BitmapFont font = new BitmapFont();
size2 = new Vector2(trash.getWidth() ,trash.getHeight() );
//size2.y = trash.getHeight();
//size2.x = trash.getWidth();
size = new Vector2(paper.getWidth() ,paper.getHeight());
bounds= new Rectangle(pos.x, pos.y, size.x, size.y);
bounds2= new Rectangle(position.x, position.y, size2.x, size2.y);
}
@Override
public void dispose() {
}
public void update(){
bounds.set(pos.x, pos.y, size.x, size.y);
bounds2.set(position.x, position.y, size2.x, size2.y);
float pos1=Gdx.input.getAccelerometerX();
//if(pos1<0)
// pos1=(-1)*pos1;
position.x = position.x - 5*pos1;
}
@Override
public void render() {
sound.play(0.5f);
sound.dispose();
if(bounds.overlaps(bounds2)){
collision=true;
counted=true;
}else{
collision=false;
}
if(collision==true){
}
if(pos.y<640){
counted=false;
} else if(pos.y > 640 && collision==false && counted==false){
counted=true;
score= score-1;
myScore = "Lives left: " + score;
str = myScore;
}
if(bounds.overlaps(bounds2)){
countMe=true;
life= life+50;
myLife = "Score: " + life;
line = myLife;
}
if(position.x<0){
position.x= position.x+11;
}
if(position.x>425){
position.x= position.x-11;
}
update();
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
pos.y=pos.y-posSpeed;
//posSpeed = posSpeed+(2/3);
if(pos.y<0){
pos.y = 700;
Random randomGenerator = new Random();
pos.x = randomGenerator.nextInt(500);
}
BitmapFont font = new BitmapFont();
batch.begin();
batch.draw(background, 0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
if (!collision) {
batch.draw(paper, pos.x, pos.y);
}
//batch.draw(paper, pos.x, pos.y);
batch.draw(trash, position.x, position.y);
font.setScale(3);
font.setColor(0.0f, 0.0f, 1.0f,1.0f);
font.draw(batch, str, 300,900);
font.draw(batch, line, 300, 950);
batch.end();
font.dispose();
}
@Override
public void resize(int width, int height) {
}
@Override
public void pause() {
}
@Override
public void resume() {
}
}
My sound file I call over here: 我在这里呼叫我的声音文件:
sound = Gdx.audio.newSound(Gdx.files.internal("data/testjava.mp3"));
And the code I use to play it is: 我用来播放的代码是:
sound.play();
sound.dispose();
For some reason it does not work on my desktop and my phone. 由于某种原因,它无法在我的台式机和手机上运行。 I tested the the sound file actually had some sound in it.
我测试了声音文件中实际上有声音。
You need to get rid of the sound.dispose();
您需要摆脱
sound.dispose();
call right after playing the sound, since it will dispose the sound file ;) 播放声音后立即调用,因为它将处理声音文件;)
Just put it into your game's dispose();
只需将其放入游戏的
dispose();
method, so it will get removed after the game is done. 方法,因此将在游戏完成后将其删除。
Also, you shouldn't call sound.play(0.5f);
另外,您不应调用
sound.play(0.5f);
within your render-loop. 在您的渲染循环中。 Remember this function will get called about 60 times a second and you don't want to have the sound start 60 times a second.
请记住,此功能每秒会调用约60次,而您不想让声音每秒启动60次。
So if it's some kind of sound effect that comes with a special event, like firing a bullet or hitting a target etc, just call sound.play();
因此,如果是特殊事件带来的某种声音效果,例如发射子弹或击中目标等,则只需调用
sound.play();
one time, when you are actually firing the event. 一次,实际上是在触发事件时。
https://github.com/libgdx/libgdx/wiki/Sound-effects https://github.com/libgdx/libgdx/wiki/Sound-effects
If you want to play background music, you should try streaming the music file. 如果要播放背景音乐,则应尝试流式播放音乐文件。 The wiki is awesome for that: https://github.com/libgdx/libgdx/wiki/Streaming-music
维基对此很棒: https : //github.com/libgdx/libgdx/wiki/Streaming-music
Hope it helps :) 希望能帮助到你 :)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.