1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Solved: Simple java issue: Want to play sound on button press

Discussion in 'Software Development' started by Inglonias, Mar 30, 2010.

Thread Status:
Not open for further replies.
  1. Inglonias

    Inglonias Thread Starter

    Joined:
    Nov 13, 2009
    Messages:
    82
    Wow. I feel like an utter noob around here.

    I just want a program I can run in case I do something cool to play a little victory tune. And preferably, it can play MP3 files. I'm good with midi, but MP3 would be nice. Code is below. It won't run. Errors at the InputStream part.

    Code:
    package victorytune;
    
    import java.applet.AudioClip;
    import java.awt.Container;
    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.FileInputStream;
    import java.io.InputStream;
    import javax.swing.JApplet;
    import javax.swing.JButton;
    import sun.audio.AudioPlayer;
    import sun.audio.AudioStream;
    
    public class VictoryTune extends JApplet implements ActionListener
    {
    Container con = getContentPane();
    JButton playSound = new JButton("That was awesome!");
    
    public void init()
        {
        con.add(playSound);
        con.setLayout(new FlowLayout());
        playSound.addActionListener(this);
        }
    public void actionPerformed(ActionEvent e)
        {
        InputStream in = new FileInputStream("victory.midi");
        AudioStream as = new AudioStream(in);
        AudioPlayer.player.start(as);
        }
    }
    
     
As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Loading...
Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/913704

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice