2016-09-03 7 views
0

コードに問題があります。ボタンをクリックすると、.mp3オーディオファイルを含むファイル配列をシャッフルして1つのファイルを再生します。私は絶対にこれを行う方法のアイデアはありません、私はいくつかの助け、本当の助けが必要です!ここに私のコードです!.....それは実行されますが、すべてのファイルを一度に再生します....ランダム化し、ファイルから1つだけ再生します。配列からオーディオファイルをシャッフルして再生する方法

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] 
... 

編集:

あなたはあなたのコードが配列内のすべてのファイルを正しい順序で再生することを知っているので、配列からランダムなサウンドを再生したいとしました。

1つだけ再生したい場合は、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

投稿を編集しました。 –

関連する問題