blob: 77fb52cc15b39c9d8e5cbddd86886e6d8e255731 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
|
package sound;
import java.io.File;
import java.io.IOException;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineEvent;
import javax.sound.sampled.LineListener;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
//Java program to play audio files. imports file scanning and various
//methods from the java audio class in order to do so.
public class ShortAudioPlayer implements LineListener
{
//indicates whether the playback completes or not
boolean playCompleted;
Clip audioClip;
public void play(String audioFilePath)
{
File audioFile = new File(audioFilePath);
try
{
//creates an audioInput object using the file we
//declared earlier
AudioInputStream audioStream = AudioSystem.getAudioInputStream(audioFile);
//gets the format of the audioStream object
AudioFormat format = audioStream.getFormat();
DataLine.Info info = new DataLine.Info(Clip.class, format);
audioClip = (Clip) AudioSystem.getLine(info);
audioClip.addLineListener(this);
audioClip.open(audioStream);
audioClip.start();
}
catch (UnsupportedAudioFileException ex)
{
System.out.println("The specified audio file is not supported.");
ex.printStackTrace();
}
catch (LineUnavailableException ex)
{
System.out.println("Audio line for playing back is unavailable.");
ex.printStackTrace();
}
catch (IOException ex)
{
System.out.println("Error playing the audio file.");
ex.printStackTrace();
}
}
/**
* Listens to the START and STOP events of the audio line.
*/
@Override
public void update(LineEvent event)
{
//something should prolly go here
}
}
|