在Android上播放阵列中的声音

时间:2015-01-19 21:40:46

标签: android audio

解决:我忘记了track.play();最后...

我想在Android智能手机上播放声音(4.0.4 Api等级15)。 我试着听到一些随机噪音,但它不起作用:

public class Sound {

  private static int length = 22050 * 10; //10 seconds long
  private static byte[] data = new byte[length];

  static void fillRandom() {
    new Random().nextBytes(data); //Create some random noise to listen to.
  }

  static void play() {

    fillRandom();

    final int TEST_SR = 22050; //This is from an example I found online.
    final int TEST_CONF = AudioFormat.CHANNEL_OUT_MONO;
    final int TEST_FORMAT = AudioFormat.ENCODING_PCM_16BIT;
    final int TEST_MODE = AudioTrack.MODE_STATIC; //I need static mode.
    final int TEST_STREAM_TYPE = AudioManager.STREAM_ALARM;
    AudioTrack track = new AudioTrack(TEST_STREAM_TYPE, TEST_SR, TEST_CONF, TEST_FORMAT, length, TEST_MODE);
    track.write(data, 0, length);
  }
}

我已经玩了一些变量,但无法让它起作用。

1 个答案:

答案 0 :(得分:0)

你剩下要做的就是玩它。将此行添加到play()函数的末尾:

track.play();