简体   繁体   English

LibGdx录制和播放声音

[英]LibGdx recording and playback sound

I have an application which records for 10 seconds and playback the sound.but for some reason when I run it the application doesn't playback, only record and flip the screen when it hears sound at a certain volume (real phone). 我有一个可以录制10秒钟并播放声音的应用程序。但是由于某种原因,当我运行它时,该应用程序无法播放,只能在听到一定音量的声音时才录制并翻转屏幕(真实电话)。 I'm really out of ideas the code seems to be good, help will be appreciated. 我真的没有想法,代码似乎很好,将不胜感激。

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 () {
}
}

It's the only class in the program. 它是程序中唯一的类。

Don't use game thread for recording, create another thread and start recording in that new thread. 不要使用游戏线程进行录制,而要创建另一个线程并在该新线程中开始录制。 I've added small example below that working fine in Android. 我在下面添加了一个小示例,在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();
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM