如何在java中播放mp3音乐文件(多于一首音乐)?

问题描述:

我写了一些核心的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

感谢 比拼

+0

它运行您的下一个mp3文件等代码?根据你的结构,你可能需要一些代码来获得一些答案。 – sealz

+0

添加一个包含要播放的歌曲列表的容器,并在使用代码播放每首歌曲时遍历列表? – birryree

+0

@ Bipin我编辑我的答案,我不知道你在使用CMD的论点,但我的想法应该做的伎俩,也许有一个小调整。 – sealz

这是绝对有可能的。

如果从一个目录读取说你的C:\音乐,那么简单地让你的程序在所有位置读取(我假设你现在只读一个)。

然后通过调用您的播放方法(IM假设你有类似的东西))

你也可以遍历该方法还是有它淡化列表等等等等

我所能地对这些文件的执行详细说明你是否发布了代码片段。

编辑:我不知道你使用的是命令行参数,所以我会尽力帮助尽我所能。

你应该能够阅读每个参数并在每个参数上执行,直到没有使用for循环为止。

public static void main(String[] args) { 
      //declare the length of your args 
      int length = args.length; 

一旦完成,您可以在每首歌曲上运行播放方法。

 //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"); 
     } 
+0

嗨哈珀,我很抱歉编辑你的帖子。我试图编辑我的帖子,但不幸的是我编辑你的帖子。PLZ回复上面的演示编码。你可以查看演示代码。谢谢,Bipin – Bipin

+0

嗨哈珀,上述代码运行成功。但直到最后才能播放mp3文件。它只是加载然后去第二个mpe文件等。可以播放mp3直到结束,然后在第一个mp3结束后加载第二个。感谢哈珀。 – Bipin

+0

是的,它应该是,我不知道我的头顶,但它似乎你只需要添加更多的线条或移动的东西。如果我有一段时间,我会尝试运行类似的东西,并让你知道我还能找到什么。 – sealz

与birryree在他的评论中所说的类似。您可以更改MP3类来存储文件名列表。然后,玩家可以依次循环播放这些内容。