2017-03-05 152 views
0

沒關係,我有這樣的問題:我的聲音開始正常播放,但即使它不會停止,「clip.stop()」或「clip.close()」 ......你有什麼想法該怎麼做才能阻止它? (我甚至可以接受甚至靜音,我真的很絕望)java的音頻播放不會停止

public class Main { 
    //audio playing 
    public static void audio(boolean a) { 
     try { 

      File file = new File("textures/Main_theme.wav"); 
      Clip clip = AudioSystem.getClip(); 
      clip.open(AudioSystem.getAudioInputStream(file)); 

      if(a == true){ 
      // this loads correctly, but wont stop music 
      clip.stop(); 
      System.out.println(a); 
      } 
      else{ 
      clip.start(); 

      } 
     } catch (Exception e) { 
      System.err.println("Put the music.wav file in the sound folder if you want to play background music, only optional!"); 
     } 
    } 


    private static String arg; 

    public static void main(String[] args){ 

    //picture loading ... ignorable now 

    arg = "textures/ccc.gif"; 
    JFrame f = new JFrame(); 
    JPanel p = new JPanel(); 
    JLabel l = new JLabel(); 
    ImageIcon icon = new ImageIcon(arg);  
    f.setSize(480, 360); 
    f.setVisible(true); 
    l.setIcon(icon); 
    p.add(l); 
    f.getContentPane().add(p); 
    f.setLocationRelativeTo(null); 
    f.setResizable(false); 
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    //calling audio method to play sound (works) 
    audio(false); 

    //should stop music and run another class 
    KeyListener action = new KeyListener() 
    { 
     @Override 
     public void keyPressed(KeyEvent e) { 
     //trying to stop music 
      f.dispose(); 
      try { 

       Menu.menu(args); 
       Main.audio(true); 

      } catch (IOException e1) { 
      //rest of code ... ignorable  
       e1.printStackTrace(); 
      } 

     } 

     @Override 
     public void keyReleased(KeyEvent e) { 
      // TODO Auto-generated method stub 

     } 

     @Override 
     public void keyTyped(KeyEvent e) { 
      // TODO Auto-generated method stub 

     } 

    }; 
    f.addKeyListener(action); 

     } 
    } 
+0

當你嘗試調用'stop',你調用'在'Clip'這是不是在玩stop',您需要使用相同的參考在'Clip'你叫'上 – MadProgrammer

回答

1

你需要退後一秒,思考你在做什麼。

你正在創建一個Clip和你玩它。在將來某個時候,您將創建一個新的Clip並嘗試停止。這兩個Clip有什麼共同點?它們如何連接?答案是,他們不是。將相同的文件加載到Clip以單獨播放是非常合理的。

相反,你需要停止你起步較早的Clip的實例。

因爲我懶,我想通過封裝音頻功能到一個簡單的類開始。

public class Audio { 

    private Clip clip; 

    protected Audio() { 
    } 

    public Audio(File source) throws LineUnavailableException, MalformedURLException, IOException, UnsupportedAudioFileException { 
     this(source.toURI().toURL()); 
    } 

    public Audio(URL source) throws LineUnavailableException, IOException, UnsupportedAudioFileException { 
     this(source.openStream()); 
    } 

    public Audio(InputStream source) throws LineUnavailableException, IOException, UnsupportedAudioFileException { 
     init(source); 
    } 

    protected void init(File source) throws LineUnavailableException, MalformedURLException, IOException, UnsupportedAudioFileException { 
     init(source.toURI().toURL()); 
    } 

    protected void init(URL source) throws IOException, LineUnavailableException, UnsupportedAudioFileException { 
     init(source.openStream()); 
    } 

    protected void init(InputStream source) throws LineUnavailableException, IOException, UnsupportedAudioFileException { 
     clip = AudioSystem.getClip(); 
     clip.open(AudioSystem.getAudioInputStream(source)); 
    } 

    public void setRepeats(boolean repeats) { 
     clip.loop(repeats ? Clip.LOOP_CONTINUOUSLY : 1); 
    } 

    public void reset() { 
     clip.stop(); 
     clip.setFramePosition(0); 
    } 

    public void play() { 
     clip.start(); 
    } 

    public void stop() { 
     clip.stop(); 
    } 

    public boolean isPlaying() { 
     return clip.isActive(); 
    } 
} 

「爲什麼?」你問,因爲我現在可以創建一個代表特定的聲音並加載它們的子類,而無需關心或記住什麼聲音的來源,例如...

public class MainTheme extends Audio { 

    public MainTheme() throws LineUnavailableException, MalformedURLException, IOException, UnsupportedAudioFileException { 
     init(getClass().getResource("textures/Main_theme.wav")); 
    } 

} 

現在,我可以很容易地創建「主要主題」音頻,而不必關心它的來源是什麼

這也意味着我可以將MainTheme傳遞給程序的其他部分,這些部分需要一個Audio的實例並簡單地卸載管理

然後你只需要創建一個類的實例和啓動/ STO p將其按要求,例如...

package test; 

import java.awt.EventQueue; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.io.File; 
import java.io.IOException; 
import java.io.InputStream; 
import java.net.MalformedURLException; 
import java.net.URL; 
import java.util.logging.Level; 
import java.util.logging.Logger; 
import javax.sound.sampled.AudioSystem; 
import javax.sound.sampled.Clip; 
import javax.sound.sampled.LineUnavailableException; 
import javax.sound.sampled.UnsupportedAudioFileException; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.UIManager; 
import javax.swing.UnsupportedLookAndFeelException; 

public class Test { 

    public static void main(String[] args) { 
     new Test(); 
    } 

    public Test() { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
       } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { 
        ex.printStackTrace(); 
       } 

       JFrame frame = new JFrame("Testing"); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.add(new TestPane()); 
       frame.pack(); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true); 
      } 
     }); 
    } 

    public class TestPane extends JPanel { 

     public TestPane() { 
      JButton btn = new JButton("Click"); 
      btn.addActionListener(new ActionListener() { 

       private Audio audio; 

       @Override 
       public void actionPerformed(ActionEvent e) { 
        try { 
         if (audio == null) { 
          audio = new MainTheme(); 
         } 

         if (audio.isPlaying()) { 
          audio.stop(); 
         } else { 
          audio.play(); 
         } 
        } catch (LineUnavailableException | IOException | UnsupportedAudioFileException ex) { 
         ex.printStackTrace(); 
        } 
       } 
      }); 

      add(btn); 
     } 
    } 
} 
+0

感謝stop',這對我幫助很大,我只是有一些麻煩與子說:「公衆型MainTheme必須在它自己的文件中定義 \t不能引用實例方法,而明確地調用構造函數「...你知道如何解決這個問題嗎? –

+0

你複製這是MainTheme類包括 – MadProgrammer

+0

肯定,並把它的權利低於去年音頻類「}」(我真的很傻程序員順便說一句,很抱歉:')) –