-
Notifications
You must be signed in to change notification settings - Fork 0
/
AudioPlayer.java
82 lines (68 loc) · 2.07 KB
/
AudioPlayer.java
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
74
75
76
77
78
79
80
81
82
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Scanner;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
/**
* @author Natampalli, Ashwin
* 19SP CS-170-01 (076011) (Hybrid)
* Final Project
* Due Date: May 15, 2019
*/
/**
* This class generates the music that plays while the game is running.
*/
public class AudioPlayer {
//To store current position
Long currentFrame;
Clip clip;
AudioInputStream audioInputStream;
static String filePath;
//Constructor to initialize streams and clip
public AudioPlayer(String filePath)
throws UnsupportedAudioFileException,
IOException, LineUnavailableException
{
//Create AudioInputStream object
this.filePath = filePath;
audioInputStream = AudioSystem.getAudioInputStream(new File(filePath).getAbsoluteFile());
//Create clip reference
clip = AudioSystem.getClip();
//Open audioInputStream to the clip
clip.open(audioInputStream);
clip.loop(Clip.LOOP_CONTINUOUSLY);
}
/*
* Method to play the audio
*/
public void play()
{
//start the clip
clip.start();
}
/*
* Main method
*/
public static void main(String[] args)
{
try
{
filePath = "src/audio/PinkPanther30.wav";
AudioPlayer audioPlayer = new AudioPlayer(filePath);
audioPlayer.play();
//keep playing the music until the user quits
while (true)
{
}
}
catch (Exception ex)
{
System.out.println("Error with playing sound.");
ex.printStackTrace();
}
}
}