audio - Loop sound in java directory -
I need some explanation with sound class, so I made a I hope this code will help you. I created a package under the resource under resource . Under the Resources package, I put all my sound files. This is to check the sound effect in the class swing app voice object class So that I can get permission to call any sound later on. My file is in the same directory as a Java file in a
voice folder, so it
~ \ game \ sounds \ music.wav now every time I get this code Executes the main square, then invokes the object and calls it, it does not file me, I am pointing incorrectly to the file? Currently my
filename only
"music.wav" How can I just point it in the
sounds directory without code coding so that it does work On any CPU possible
public sound (string filename) {try {file file = new file (fileName); If (file.exists ()) {myClip = (AudioClip) applet.nuoudo clip (file.toURI () .toURL ()); } And {new runtime expiry ("sound: file not found:" + fileName); }} Hold (Malformedlexception E) {New Runtime Expansion ("Sound: Distorted URL:" + E); }} Public Zero Play () {myClip.play (); }
Import java.io. *; Import java.net.URL; Import javax.sound.sampled *; Public Enum SoundEffect {BUSY ("resources / phone-busy.wav"), calling ("resource / phone-calling.WW"), disconnected ("resources / phone-disconnect.wav"), ringing ("resource / telephone- Ring.wav "); // Each sound effect has its own clip, the private clip clip is loaded with its own sound file; Private URL URL; Private audioindstream audio inputstream; // Controller to create each element of the em with its own sound file. Sound effects (string sound file name) {try {// Use the URL to read from disk and jar (instead of file) This.url = this.getClass (). GetClassLoader (). GetResource (soundFileName); // Pipe an audio input stream from the sound file. This.audioInputStream = AudioSystem.getAudioInputStream (url); // Get a clip resource clips = audio system .getclip (); // Load sample from open audio clip and audio input stream. clip.open (audioInputStream); } Hold (unsupported audio file exception e) {e.printStackTrace (); } Hold (IOException e) {e.printStackTrace (); } Hold (LineUnavailableException E) {e.printStackTrace (); Play the sound effect from the beginning or play again by rewinding. Public Zero Play () {clip.loop (clip.LOOP_CONTINUOUSLY); } Public Zero Stop () {clip.stop (); // Stop the player if he is still running the clip. Flush (); clip.setFramePosition (0); } // Optional static method to pre-load all sound files; zero init (value); // calls the constructor for all elements} Public Boolean Active () {return clip.isActive (); } Public Boolean isOpen () {return clip.isOpen (); } Public Zero setFramePosition () {clip.setFramePosition (0); }}
import java.awt. *; Import java.awt.event. *; Import javax.swing. *; // Screenshoot Enum Testing in a Curve application @SuppressWarnings ("serial") public class SoundEffectDemo Expands JFrame {// Constructor Public SoundEffectDemo () {// Preload all sound files // UI components set container CP = This .getContentPane (); Cp.setLayout (new flow layout (flow layout.cient, 10, 10)); Jebton BT 1Sund = 1 new pocket ("calling"); BtnSound1.addActionListener (New Extensionist () {@ Override Public Voice Action Crore (Action Event E) {Soundtrack.Caring.Play ();}}); cp.add (btnSound1); Pocket button btnsund 2 = new pocket ("ringing"); BtnSound2.addActionListener (New Executioner) {@ Override Public Voice ActionPerfed (Action Event E) {SoundFeat. stream.play ();}}); cp.add (btnSound2); Jebton BTSIND 3 = New Pocket ("Bush"); BtnSound3.addActionListener (New Executionist) {@ Override Public WAd ActionPerfect (Action Event E) {SoundFet.BUSY.play ();}}); cp.add (btnSound3); Jebton BTissand 4 = New Pocket ("Stop Sound"); {Sound effect: SoundEffect.values ()) {if (value.isActive ()) {value.stop ()}} BtnSound4.addActionListener (New Executionist) {@Override Public Zero Function }}}}); cp.add (btnSound4); This.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); This.setTitle ("Test Sound Effect"); this.pack (); This.setVisible (true); } Public static zero main (string [] args) {new soundfile dumo (); }}
Comments
Post a Comment