2015-05-31 145 views
0

你好,我試圖做一個java鼓套件,但我的代碼不播放聲音,當我點擊按鈕,我不知道爲什麼。我有代碼的文件中的聲音文件,所以我知道這不是問題。這裏是我的代碼...爪哇鼓組項目

import java.applet.AudioClip; 
import java.awt.FlowLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.JApplet; 
import javax.swing.JButton; 

public class FinalProjectst extends JApplet implements ActionListener { 
    private JButton snareButton; 
    private JButton hiHatButton; 
    private JButton bassButton; 
    private JButton cymbalsButton; 
    private AudioClip snare; 
    private AudioClip hiHat; 
    private AudioClip bass; 
    private AudioClip cymbals; 

    public void init() { 
     setLayout(new FlowLayout()); 
     sampleButtons(); 
     snare = getAudioClip(getCodeBase(), "Snare.wav"); 
     hiHat = getAudioClip(getCodeBase(), "HiHat.wav"); 
     bass = getAudioClip(getCodeBase(), "Kick.wav"); 
     cymbals = getAudioClip(getCodeBase(), "Crash.wav"); 
    } 

    private void sampleButtons() { 
     snareButton = new JButton("Snare"); 
     hiHatButton = new JButton("Hi Hat"); 
     bassButton = new JButton("Kick"); 
     cymbalsButton = new JButton("Cymbals"); 

     snareButton.addActionListener(new ButtonListener()); 
     hiHatButton.addActionListener(new ButtonListener()); 
     bassButton.addActionListener(new ButtonListener()); 
     cymbalsButton.addActionListener(new ButtonListener()); 

     add(snareButton); 
     add(hiHatButton); 
     add(bassButton); 
     add(cymbalsButton); 
    } 

    public void actionPerformed(ActionEvent e) { 
     if (e.getSource() == snareButton) 
      snare.play(); 
     if (e.getSource() == hiHatButton) 
      hiHat.play(); 
     if (e.getSource() == bassButton) 
      bass.play(); 
     if (e.getSource() == cymbalsButton) 
      cymbals.play(); 
    } 
} 
+2

「ButtonListener」的定義在哪裏? – copeg

+2

嘗試使用'snareButton.addActionListener(this);'和其他按鈕相同。 –

回答

1

有很多不好的事情與此代碼,但總之我得到「文件:/ tmp目錄/」從getCodeBase()回報。所以你指向* .wav文件什麼都不是。

使用此代碼,第一個按鈕將播放一段距離網頁有趣的WAV聲音。

import java.awt.*; 
import java.applet.*; 
import java.awt.event.*; 
import javax.swing.*; 
import java.net.MalformedURLException; 
import java.net.URL; 


public class FinalProjectst extends JApplet implements ActionListener 
{ 

    private JButton snareButton; 
    private JButton hiHatButton; 
    private JButton bassButton; 
    private JButton cymbalsButton; 
    private AudioClip snare; 
    private AudioClip hiHat; 
    private AudioClip bass; 
    private AudioClip cymbals; 

    public void init() 
    { 
     setLayout (new FlowLayout()); 

     sampleButtons(); 

     try { 
      snare = getAudioClip(new URL("http://download.wavetlan.com/SVV/Media/HTTP/WAV/Media-Convert/Media-Convert_test1_Alaw_Mono_VBR_8SS_16000Hz.wav")); //getCodeBase(), "Snare.wav"); 
     } catch (MalformedURLException e) { 
      e.printStackTrace(); 
     } 
     hiHat = getAudioClip(getCodeBase(), "HiHat.wav"); 
     bass = getAudioClip(getCodeBase(), "Kick.wav"); 
     cymbals = getAudioClip(getCodeBase(), "Crash.wav"); 

    } 

    private void sampleButtons() 
    { 
     snareButton = new JButton("Snare"); 
     hiHatButton = new JButton("Hi Hat"); 
     bassButton = new JButton("Kick"); 
     cymbalsButton = new JButton("Cymbals"); 

     snareButton.addActionListener(this); //new ButtonListener() 
     hiHatButton.addActionListener(this); 
     bassButton.addActionListener(this); 
     cymbalsButton.addActionListener(this); 

     add(snareButton); 
     add(hiHatButton); 
     add(bassButton); 
     add(cymbalsButton); 
    } 

    public void actionPerformed(ActionEvent e) 
    { 
     if (e.getSource() == snareButton) { 
      snare.play(); 
      System.out.println(getCodeBase()); 
     } 
     if (e.getSource() == hiHatButton) 
      hiHat.play(); 
     if (e.getSource() == bassButton) 
      bass.play(); 
     if (e.getSource() == cymbalsButton) 
      cymbals.play(); 



    } 
} 

這個作品的唯一原因是因爲我改變了失蹤ButtonListener(),以「本」(你並不需要,如果你有類)。現在你有一個到第一個按鈕的現有URL的工作路徑。它播放聲音很好。

我建議你尋找一種方法來獲得你需要的準確的URL,用getCodeBase()或其他方法。