简体   繁体   中英

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). 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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