2016-03-07 112 views
1

所以我被要求幫助一個Java遊戲,並且我被賦予了音頻角色。我得到的音頻播放,但我需要它到我可以停止音頻和啓動一個新的音頻字符串在另一個文件調用它。這就是我所擁有的;Java遊戲的背景音樂

import java.io.*; 
import java.net.URL; 
import javax.sound.sampled.*; 
import javax.swing.*; 

public class SoundTest{ 

// Constructor 
public static void Run(String pen)throws InterruptedException { 
JFrame f = new JFrame(); 
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
f.setTitle("Test Sound"); 
f.setSize(300, 200); 
f.setVisible(false);  

try { 
    // Open an audio input stream.   
     File soundFile = new File(pen); 
     AudioInputStream audioIn = AudioSystem.getAudioInputStream(soundFile);    
     Clip clip = AudioSystem.getClip(); 

    clip.open(audioIn); 
    clip.start(); 
    } catch (UnsupportedAudioFileException e) { 
    e.printStackTrace(); 
    } catch (IOException e) { 
    e.printStackTrace(); 
    } catch (LineUnavailableException e) { 
    e.printStackTrace(); 
    } 
} 
} 

,我有這個調用它

import java.io.*; 
import java.net.URL; 
import javax.sound.sampled.*; 
import javax.swing.*; 

public class SoundCall{ 


public static void main(String[] args)throws InterruptedException { 
JFrame f = new JFrame(); 

SoundTest sound = new SoundTest(); 
sound.Run("Jskee_-_I_Am_Pharaoh_.wav"); 

JOptionPane.showMessageDialog(f,"Window","Cracker",JOptionPane.PLAIN_MESSAGE); 
sound.stop; 
sound.Run("Rejekt_-_Crank.wav"); 
} 
} 

回答

0

首先,聲明你剪輯的運行功能之外,像這樣:Clip clip;然後就去做clip = AudioSystem.getClip();,最後打個不停功能您SoundTest類,看起來像這樣:

public boolean stop(int id) { 
    if (clips.get(id-1) != null) { 
     clips.get(id-1).stop(); 
     return true; 
    } 
    return false; 
} 

返回一個布爾值是檢查,讓您的應用程序是肯定是否已經停止音樂 或不。這是你的全SoundTest文件將是什麼樣子算賬:

import java.io.*; 
import java.net.URL; 
import javax.sound.sampled.*; 
import javax.swing.*; 

public class SoundTest{ 

private ArrayList<Clip> clips = new ArrayList<Clip>(); 

// Constructor 
public void Run(String pen)throws InterruptedException { 
JFrame f = new JFrame(); 
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
f.setTitle("Test Sound"); 
f.setSize(300, 200); 
f.setVisible(false);  

try { 
    // Open an audio input stream.   
     File soundFile = new File(pen); 
     AudioInputStream audioIn = AudioSystem.getAudioInputStream(soundFile);    
     Clip clip = AudioSystem.getClip(); 

    clip.open(audioIn); 
    clip.start(); 
    clips.add(clip); 
    } catch (UnsupportedAudioFileException e) { 
    e.printStackTrace(); 
    } catch (IOException e) { 
    e.printStackTrace(); 
    } catch (LineUnavailableException e) { 
    e.printStackTrace(); 
    } 
} 
public boolean stop(int id) { 
    if (clips.get(id-1) != null) { 
     clips.get(id-1).stop(); 
     return true; 
    } 
    return false; 
} 
} 

而在你SoundCall類,根本就

if(sound.stop(1)) { //1 is an id, I made it so that it starts at 1, not 0. 
     System.out.println("Audio stopped."); 
} else { 
     System.out.println("Audio has not stopped."); 
} 

id是剪輯調用時,還挺。如果它是要執行的第一個剪輯,則ID爲1,如果它是第二個ID,則爲2等。

+0

在哪裏可以實現剪輯的聲明? – Jotohooh

+0

@Jotohooh在您的SoundTest文件的頂部 – ItzBenteThePig

+0

@Jotohooh編輯答案,讓您更容易理解 – ItzBenteThePig