LibGdx录制和播放声音

时间:2017-06-01 09:38:42

标签: android android-studio audio libgdx

我有一个应用程序记录10秒钟并播放声音。但由于某些原因,当我运行它时,应用程序不播放,只有在听到特定音量的声音时才会录制并翻转屏幕(真实手机) 。 我真的没有想法代码似乎很好,帮助将不胜感激。

package com.mygdx.game;

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.audio.AudioDevice;
import com.badlogic.gdx.audio.AudioRecorder;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;

 public class MyGdxGame extends ApplicationAdapter{

@Override
public void create () {
    AudioDevice playbackDevice = Gdx.audio.newAudioDevice(44100, true);
    AudioRecorder recordingDevice = Gdx.audio.newAudioRecorder(44100, true);
    short[] samples = new short[44100 * 10]; // 10 seconds mono audio
    recordingDevice.read(samples, 0, samples.length);
    playbackDevice.writeSamples(samples, 0, samples.length);
    recordingDevice.dispose();
    playbackDevice.dispose();

}

@Override
public void render () {

}

@Override
public void dispose () {
}
}

这是该计划中唯一的课程。

1 个答案:

答案 0 :(得分:0)

不要使用游戏线程进行录制,创建另一个线程并开始在该新线程中录制。我在下面添加了一个小例子,在Android中运行良好。

public class RecordingTest extends ApplicationAdapter{

   Stage stage;

   @Override
   public void create() {

      final AudioDevice playbackDevice = Gdx.audio.newAudioDevice(44100, true);
      final AudioRecorder recordingDevice = Gdx.audio.newAudioRecorder(44100, true);

      stage=new Stage();
      final Label labelStatus=new Label("NOTHING",new Label.LabelStyle(new BitmapFont(), Color.BLUE));

      final short[] samples = new short[44100 * 10]; // 10 seconds mono audio

      new Thread(new Runnable() {
          @Override
          public void run() {

            labelStatus.setText("Recording Started");
            recordingDevice.read(samples, 0, samples.length);
            recordingDevice.dispose();
            labelStatus.setText("Recording Ended");
            labelStatus.setText("Recorded sample start Playing");
            playbackDevice.writeSamples(samples, 0, samples.length);
            labelStatus.setText("Playing Ended");
            playbackDevice.dispose();

        }
    }).start();

    Table table=new Table();
    table.setFillParent(true);
    table.add(labelStatus);
    stage.addActor(table);
  }

  @Override
  public void render() {

    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    Gdx.gl.glClearColor(1,1,1,1);

    stage.draw();
    stage.act();
  }

  @Override
  public void dispose() {
      stage.dispose();
  }
}