问题描述
我现在写个应用程序来播放声音文件 ,播放单个声音文件没有问题 ,但我连续播放多个声音文件 却有问题 ,我不想用 Thread.sleep(300) 这样实现文件与文件的间隔我将 多个文件的字节流合并成一个 在播放去只播放第一个文件 不知道哪里有问题 希望大家指导,帮助代码:public class Sound {private AudioStream as = null;private byte[] bs = null;public Sound() {}/** * 播放单个文件 */public synchronized void play(InputStream ins) {try {as = new AudioStream(ins);} catch (IOException e) {e.printStackTrace();}play(as);}/** * 播放 * * @param as */public synchronized void play(AudioStream as) {AudioPlayer.player.start(as);try {AudioPlayer.player.join(5000L);} catch (InterruptedException e) {e.printStackTrace();}AudioPlayer.player.stop(as);}/** * 播放单个文件 */public synchronized void play(InputStream[] ins) {as = getAllAudioStream(ins);play(as);}/** * 将多个流合并 * * @param ins * @return */private AudioStream getAllAudioStream(InputStream[] ins) {try {if (ins == null || ins.length == 0) {return null;}byte[] bytes = new byte[1024100];bs = new byte[0];for (int i = 0, count = ins.length; i < count; i++) {InputStream in = ins[i];int length = 0;length = in.read(bytes);byte[] tmp = new byte[bs.length + length];int j = 0;for (; j < bs.length; j++) {tmp[j] = bs[j];}for (int k = 0; k < length; k++) {tmp[j++] = bytes[k];}bs = new byte[tmp.length];for (int n = 0; n < tmp.length; n++) {bs[n] = tmp[n];}}as = new AudioStream(new ByteArrayInputStream(bs));} catch (IOException e) {e.printStackTrace();}return as;}}
解决方案
不建议合并流推荐楼主递归调用单个播放的方法