[英]how to play mp3 music files (more than one music) in java?
我编写了一些核心Java代码来在Java中播放一个mp3文件。我成功了。 但是我想在Java中依次一个接一个地播放多个mp3音乐文件。
有可能吗? 如果可能的话请帮助我。
这是演示代码,
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import javazoom.jl.player.Player;
public class MP3 extends Thread{
static int k=0;
private String filename;
private Player player;
// constructor that takes the name of an MP3 file
public MP3(String filename) {
this.filename = filename;
}
public void close() { if (player != null) player.close(); }
// play the MP3 file to the sound card
public synchronized void play() {
try {
FileInputStream fis = new FileInputStream(filename);
BufferedInputStream bis = new BufferedInputStream(fis);
player = new Player(bis);
}
catch (Exception e) {
System.out.println("Problem playing file " + filename);
System.out.println(e);
}
// run in new thread to play in background
new Thread() {
public void run() {
try { player.play();}
catch (Exception e) { System.out.println(e); }
}
}.start();
}
// test client
public static void main(String[] args) {
String filename = args[0];
//StringBuffer br= new StringBuffer(filename);
StringBuffer mp3_name = new StringBuffer();
String arr[]=new String[3];
String mp3temp=filename;
for(int i=0;i<mp3temp.length();i++)
{
int count = mp3temp.indexOf(",");
System.out.println(count);
if(count != -1)
{
String temp = mp3temp.substring(0,count);
System.out.println(temp);
mp3_name.append(temp);
arr[k++] = temp;
mp3temp = mp3temp.substring(count+1,mp3temp.length());
}
if(count == -1)
{
if(mp3temp.endsWith("mp3"))
{
mp3_name.append(mp3temp);
arr[k++] = mp3temp;
System.out.println(mp3temp);
break;
}
}
}
StringBuffer bb=mp3_name;
String test ="";
String subtest ="";
for(int s= 0;s < arr.length;s++)
{
System.out.println(arr[s]);
MP3 mp3 = new MP3(arr[s]);
mp3.play();
System.out.println(arr[s]);
// do whatever computation you like, while music plays
int N = 4000;
double sum = 0.0;
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
sum += Math.sin(i + j);
}
}
System.out.println(sum);
// when the computation is done, stop playing it
mp3.close();
// play from the beginning
mp3 = new MP3(arr[s]);
mp3.play();
//}
}
}
}
通过分隔逗号从comand提示符处传递mp3文件。 例如:java MP3 test.mp3,a.mp3,b.mp3
感谢Bipin
绝对有可能。
如果从目录中读取您的C:\\ Music,则只需在所有位置读取您的程序即可(我假设您目前只读取其中一个)。
然后通过调用play方法在每个文件上执行(假设您有类似的东西)
您可以循环该方法,或使其在列表中显示等等。
如果您发布代码段,我可以详细说明。
编辑:我不知道您使用命令行争论,所以我会尽力而为。
您应该能够阅读每项论据,并使其在每项论据上执行,直到不再使用for循环为止。
public static void main(String[] args) {
//declare the length of your args
int length = args.length;
完成后,您可以在每首歌曲上运行play方法。
//Perform your loop
for (int i = 0; i < length; i++) {
//Call your play method here
} // End Loop
//When done you can print something
System.out.println("All Songs have been played");
}
类似于birryree在评论中所说。 您可以更改MP3类以存储文件名列表。 然后,播放可以依次循环这些播放。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.