贴出一段代码,随机播放音乐播放库里的mp3音频文件,不是很好,欢迎大家指正。
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Random;
import javax.imageio.stream.FileImageInputStream;
import javazoom.jl.decoder.JavaLayerException;
import javazoom.jl.player.Player;
public class Mp3Test {
/*
* 将一个文件夹下的全部mp3文件读取病播放其一个帧,组合成一个音乐库。
*
* */
public static void main(String[] args) {
String path="G:\\酷狗\\酷狗文件夹\\"; //这里存放音频文件
new SearchFile(path).search();
}
}
class SearchFile{
private String fString;
private Player player;
private BufferedInputStream bis;
public SearchFile(String fString) {
this.fString=fString;
}
public void search() {
File file=new File(fString);
File[] files=file.listFiles();
for (int i = 0; i < files.length; i++) {
if (files[i].isFile() && files[i].getName().endsWith(".mp3")) {
try {
Random random=new Random(i);
int a=random.nextInt(788);
System.out.println(files[a].getName());
bis=new BufferedInputStream(new FileInputStream(files[a]));
player=new Player(bis);
if (!player.isComplete()) {
player.play();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (JavaLayerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
if (bis!=null) {
bis.close();
}
} catch (IOException e) {
e.printStackTrace();
}
player.close();
}
}
}
}
}
阅读(1025) | 评论(1) | 转发(0) |