2016-09-03 93 views
0

我的代碼存在一些問題,我想隨機播放包含.mp3音頻文件的文件數組,並在單擊按鈕時播放其中的一個文件!我絕對不知道如何做到這一點,我需要一些幫助,真正的幫助!這裏是我的代碼!.....它運行,但它一次播放所有文件....我希望它隨機化,並只播放一個文件。如何從數組中隨機播放音頻文件

import javax.swing.*; 

import sun.audio.AudioPlayer; 
import sun.audio.AudioStream; 

import java.awt.*; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileNotFoundException; 
import java.io.IOException; 
import java.io.InputStream; 
import java.lang.reflect.Array; 
import java.util.Random; 

public class Quest1 extends JFrame { 

    String word [] = { "C:/Users/HP/Desktop/eclipse/audio.wav", 
      "C:/Users/HP/Desktop/eclipse/baby.wav", 
      "C:/Users/HP/Desktop/eclipse/board.wav", 
      "C:/Users/HP/Desktop/eclipse/bomb.wav", 
      "C:/Users/HP/Desktop/eclipse/gym.wav", 
      "C:/Users/HP/Desktop/eclipse/football.wav", 
      "C:/Users/HP/Desktop/eclipse/school.wav", 
      "C:/Users/HP/Desktop/eclipse/keyboard.wav", 
      "C:/Users/HP/Desktop/eclipse/computer.wav", 
      "C:/Users/HP/Desktop/eclipse/name.wav" }; 


    JButton click;  

    public Quest1() { 

     getContentPane().setBackground(Color.DARK_GRAY); 

     setLayout(new GridBagLayout()); 

     GridBagConstraints g = new GridBagConstraints(); 


     g.anchor = GridBagConstraints.CENTER; 
     g.gridx = 4; 
     g.gridy = 5; 
     g.gridwidth = 2; 
     g.insets = new Insets (50, 2, 2, 2); 
     g.fill = GridBagConstraints.CENTER; 
     fill = new JTextField(15); 
     add (fill, g); 

     g.anchor = GridBagConstraints.CENTER; 
     g.gridx = 4; 
     g.gridy = 8; 
     g.gridwidth = 2; 
     g.insets = new Insets (30, 2,2,2); 
     click = new JButton("Play"); 
     add(click, g); 

     click.addActionListener (new ActionListener(){ 
      public void actionPerformed (ActionEvent x) { 

       Random rand = new Random(); 
        for (int i = 0; i < word.length;) { 
         int random = rand.nextInt(word.length); 
         String temp = word[i]; 
         word [i] = word[random]; 
         word[random] = temp; 

         InputStream in =null; 
         AudioStream out = null; 
          try { 
           in = new FileInputStream(temp); 
          } catch (FileNotFoundException e) { 
           // TODO Auto-generated catch block 
           e.printStackTrace(); 
          } 
          try { 
           out = new AudioStream(in); 
          } catch (IOException e) { 
           // TODO Auto-generated catch block 
           e.printStackTrace(); 
          }  
          AudioPlayer.player.start(out); 
        }  
        return; 
      } 
     });  
    } 
} 
+0

是您的問題,播放聲音文件在所有,或起草/從陣列中選擇? – jagdpanzer

+0

從數組中選擇,是的,這是我的問題.....我想隨機化,然後播放。 –

回答

0

如果這是一個正常的Java數組抱着你要播放的文件,最簡單的方法是簡單地生成一個隨機指數和從數組索引得到對象:

//Index of the random song 
int r = (int) (Math.random() * (songs.length - 1); 
//Get the song from the array 
songs[r] 
... 

編輯:

你說你想從數組中播放一個隨機聲音,通過知道你的代碼只是以正確的順序播放數組中的所有文件。

如果您只想播放ONE,則必須刪除for循環。

正確的代碼:

click.addActionListener (new ActionListener(){ 
     public void actionPerformed (ActionEvent x) { 
      //Get random filepath from the array 
      Random rand = new Random(); 
      int random = rand.nextInt(word.length); 
      String temp = word[random]; 

      InputStream in =null; 
      AudioStream out = null; 
      //Get the actual file from the path 
      try { 
       in = new FileInputStream(temp); 
      } catch (FileNotFoundException e) { 
       e.printStackTrace(); 
      } 
      try { 
       out = new AudioStream(in); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      }  
      //Play the file 
      AudioPlayer.player.start(out); 
      return; 
     } 
    });  
+0

好吧,我不知道你是否可以幫我編寫播放聲音文件的語法,我似乎沒有把它弄直.......謝謝! –

+0

我需要更多信息才能幫助您播放文件本身。 – Sandrogo

+0

我已編輯我的帖子。 –