0

I am trying to play a sound(a.mp3 ) on button ckick in java.

I tried this code

AudioPlayer.player.start(new FileInputStream(new File("E://a.mp3")));

but the sound is not clear....

What should I do,(I'm a beginner in java).

1
  • 1
    What is the AudioPlayer class? This isn't part of the J2SE API. Commented Aug 18, 2011 at 4:57

3 Answers 3

1
 **For Playing sound in java, please refer to the following code.** import java.io.*; import java.net.URL; import javax.sound.sampled.*; import javax.swing.*; // To play sound using Clip, the process need to be alive. // Hence, we use a Swing application. public class SoundClipTest extends JFrame { // Constructor public SoundClipTest() { this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setTitle("Test Sound Clip"); this.setSize(300, 200); this.setVisible(true); try { // Open an audio input stream. URL url = this.getClass().getClassLoader().getResource("filenamme.mp3"); AudioInputStream audioIn = AudioSystem.getAudioInputStream(url); // Get a sound clip resource. Clip clip = AudioSystem.getClip(); // Open audio clip and load samples from the audio input stream. clip.open(audioIn); clip.start(); } catch (UnsupportedAudioFileException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (LineUnavailableException e) { e.printStackTrace(); } } public static void main(String[] args) { new SoundClipTest(); } } 
Sign up to request clarification or add additional context in comments.

Comments

0

Try taking a look at this article. http://www.morgenstille.at/blog/how-to-play-a-mp3-file-in-java-simple-and-beautiful/

Also JFugue can load and play midi files.

Comments

0

take a look at JLayer. It's the best library I could find a couple of years ago. It's not perfect, but should fit your needs.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.