我寫了一些核心的java代碼,在java中播放一個mp3文件。我成功了。但我想在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();
//}
}
}
}
通行證MP3文件通過分離逗號。例如:java的MP3請將test.mp3,a.mp3,b.mp3
感謝 比拼
它運行您的下一個mp3文件等代碼?根據你的結構,你可能需要一些代碼來獲得一些答案。 – sealz
添加一個包含要播放的歌曲列表的容器,並在使用代碼播放每首歌曲時遍歷列表? – birryree
@ Bipin我編輯我的答案,我不知道你在使用CMD的論點,但我的想法應該做的伎倆,也許有一個小調整。 – sealz