//不能播放中提示了使用了关闭的流
//解决方案把需要用到的声音文件存储到内存中
//不能播放中提示了使用了关闭的流
//解决方案把需要用到的声音文件存储到内存中
//1.把文件加载到内存
//取到文件的URL
//2.把文件的url通过openStream()转换成输入流InputStream
//3.把InputStream数据取出写入到字节数组输出流 ByteArrayOutputStream
//4.通过字节数组接收字节数组输出流的数据添加到定义的Map集合中
class ResManager{
static Map<String ,byte[]> files = new HashMap<String,byte[]>();
//获取文件URL
public satic URL getResUrl(String xx){
URL u = ResManger.class.getClassLoader().getResource("xx");
return u;
}
//通过穿文件路径的方式把文件加载到内存
private static void loadfile(String xx){
//返回文件的URL
URL u = ResManager.class.getClassLoader().getResource(xx);
//把输入的文件转换成字节输入流
InputStream temp = u.openStream();
//加载字节流
int len = -1;
//缓冲数组
byte data[] = new byte[512];
//字节输出流
ByteArrayOutputStream baos = new ByteArrayOutputStream();
while((len = temp.read(data)) != -1){
baos.write(data,0,len);
//取出字节数组
byte tep[] = baos.toByteArray();
//添加到集合类
files.put(xx.toUpperCase(),tep)
//关闭流
baos.close();
temp.close();
}
}
//返回字节输入流
public static InputStream getResStream(String xx){
//取出内存中map的数据
byte data[] = files.get(xx.UpperCase());
//字节输入流 把字节转换为字节流
ByteArrayInputStream bais = new ByteArrayInputStream(b);
return bais;
}
//必须初始化把数据加载到内存才有办法取出输入流
public void init(){
loadfile("xxx.wav");
}
}