I write some core java code to a play one mp3 file in java .I was successful. But i want to play more than one mp3 music file sequentially one by one in java.
我在java中寫了一些核心java代碼到一個播放一個mp3文件。我成功了。但我想在java中逐個播放多個mp3音樂文件。
can it be possible ?? If possible plz help me.
有可能嗎?如果可能,請幫助我。
Here is the demo code ,
這是演示代碼,
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
{
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();
//}
}
}
}
Pass mp3 files from comand prompt by separating commas. ex :java MP3 test.mp3,a.mp3,b.mp3
通過分隔逗號從命令提示符傳遞mp3文件。例如:java MP3 test.mp3,a.mp3,b.mp3
Thanks Bipin
2 个解决方案
#1
0
Similar to what birryree said in his comment. You could change the MP3 class to store a list of filenames. Play can then loop over these playing each in turn.
與birryree在評論中所說的相似。您可以更改MP3類以存儲文件名列表。然后播放可以依次循環播放這些播放。
#2
0
It is definitely possible.
這絕對是可能的。
If reading from a directory say your C:\Music, then simple have your program read in all of the locations (I am assuming you are reading in only one at the moment).
如果從目錄中讀取說你的C:\ Music,那么簡單就可以在所有位置讀取你的程序(我假設你現在只讀一個)。
Then execute on each of those files by calling your play method(Im assuming you have something similar))
然后通過調用play方法對每個文件執行(我假設你有類似的東西))
You can loop that method or have it play down the list etc etc.
您可以循環該方法或讓它播放列表等等。
I can elaborate if you post a snippet of your code.
如果您發布代碼片段,我可以詳細說明。
EDIT: I didnt know you were using command line arguements, so I will try and help the best I can.
編輯:我不知道你使用命令行爭論,所以我會盡力幫助我。
You should be able to read in each arguement and have it execute on each one until there are not left using a for loop.
你應該能夠讀入每個爭論並讓它在每個爭論中執行,直到沒有使用for循環。
public static void main(String[] args) {
//declare the length of your args
int length = args.length;
Once thats done you can run the play method on each of the songs.
完成后,您可以在每首歌曲上運行播放方法。
//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");
}