Download Playing Audio (Application Example) Java programs can play audio

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Playing Audio (Application Example)
Java programs can play audio that is stored in a variety sound file formats.
.aif or .aiff (Macintosh Audio File)
.au (Sun Audio File)
.mid or .rmi (MIDI File)
.wav (Windows Wave File)
Playing audio in a Application differs from playing it in an Applet
AudioClip:
If you need to load a sound file to be played multiple times, use an AudioClip object.
a. The AudioClip interface specifies the following three methods:
 play – plays a sound one time.
 loop – repeatedly plays a sound.
 stop – causes a sound to stop playing.
b. The Applet static method newAudioClip (URL url) – returns an audio clip from the given URL
c. Use URI and URL objects found in java.net package to create the base location
d. If a valid URL cannot be formed a MalformedURLException is thrown. This is found in java.net package.
Notice the constructor and the main method must throw the MalformedURLException if it is not
caught.
import java.applet.*;
import java.net.*;
import java.io.*;
//AudioClip, Applet
// MalformedURLException, URI, URL
//File
try
{
File file = new File("step.wav");
URI uri = file.toURI();
URL url = uri.toURL();
// Create a file object for the step.wav file.
// Get a URI object for the audio file.
// Get a URL for the audio file.
AudioClip sound;
sound = Applet.newAudioClip(url);
//Get an AudioClip object for the sound file
sound.play();
}
catch (MalformedURLException e)
{
System.out.println(“Error playing sound”);
e.printStackTrace();
}
/** This application uses the AudioClip class to play a sound. */
import java.awt.* ; // FlowLayout
import java.applet.*; //AudioClip, Applet
import java.awt.event.*; //ActionListener, ActionEvent
import javax.swing.*; //JFrame, JLabel, JButton;
import java.io.*; //File
import java.net.*; // MalformedURLException, URI, URL
public class AudioFrame extends JFrame
{ private JLabel credit;
// Displays NASA credit
private JButton playButton; // Plays the sound clip
private JButton loopButton; // Loops the clip
private JButton stopButton; // Stops the clip
private AudioClip sound;
// Holds the sound clip
public AudioFrame() throws MalformedURLException
{ setTitle("JFrame Audio Demo");
setDefaultCloseOperation(EXIT_ON_CLOSE);
// Make the credit label and PlayLoop and Stop buttons.
credit = new JLabel("Audio source: NASA");
playButton = new JButton("Play");
loopButton = new JButton("Loop");
stopButton = new JButton("Stop");
// Register an action listener with each button.
playButton.addActionListener(new ButtonListener());
loopButton.addActionListener(new ButtonListener());
stopButton.addActionListener(new ButtonListener());
// Add the buttons to the content pane in a flowLayout
setLayout(new FlowLayout());
add(credit);
add(playButton);
add(loopButton);
add(stopButton);
File file = new File("step.wav");
// Create a file object for the step.wav file.
URI uri = file.toURI();
// Get a URI object for the audio file.
URL url = uri.toURL();
// Get a URL for the audio file.
sound = Applet.newAudioClip(url); //Get an AudioClip object for the sound file
pack();
setVisible(true);
}
private class ButtonListener implements ActionListener
{ public void actionPerformed(ActionEvent e)
{ if (e.getSource() == playButton)
sound.play();
else if (e.getSource() == loopButton)
sound.loop();
else if (e.getSource() == stopButton)
sound.stop();
}
}
public static void main(String[] args) throws MalformedURLException
{ new AudioFrame(); }
}
Related documents