在Android上播放阵列中的声音 [英] Play sound from array on Android
本文介绍了在Android上播放阵列中的声音的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
已解决:我忘记了track.play();最后...
Solved: I forgot the track.play(); at the end...
我想在我的Android智能手机(4.0.4 Api 15级)上播放声音. 我试图听到一些随机的声音,但是它不起作用:
I want to play a sound on my Android Smartphone (4.0.4 Api level 15). I tried to hear some random noise, but its not working:
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);
}
}
我在玩水粉玩具时有点用,但是不能使它起作用.
I have played a little bit with the variabels, but could not get it to work.
推荐答案
剩下要做的就是播放它.将此行添加到play()函数的末尾:
All you have left to do is play it. Add this line to the end of your play() function:
track.play();
这篇关于在Android上播放阵列中的声音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文