-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path48_Audio
More file actions
75 lines (59 loc) · 2.27 KB
/
48_Audio
File metadata and controls
75 lines (59 loc) · 2.27 KB
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
<Main.java>
import javax.sound.sampled.*; //Remember
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws UnsupportedAudioFileException, IOException, LineUnavailableException {
Scanner scanner = new Scanner(System.in); //Audio play in backthread, do so to avoid terminate as computer wait for input
File file = new File("World's Sunrise - Jimena Contreras.wav");
AudioInputStream audioStream = AudioSystem.getAudioInputStream(file);
Clip clip = AudioSystem.getClip();
clip.open(audioStream);
clip.start();
String response = scanner.next();
}
}
>> Played audio
//To have more functions
<Main.java>
import javax.sound.sampled.*;
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws UnsupportedAudioFileException, IOException, LineUnavailableException {
Scanner scanner = new Scanner(System.in); //Audio play in backthread, do so to avoid terminate as computer wait for input
File file = new File("World's Sunrise - Jimena Contreras.wav");
AudioInputStream audioStream = AudioSystem.getAudioInputStream(file);
Clip clip = AudioSystem.getClip();
clip.open(audioStream);
String response = "";
while(!response.equals("Q")){
System.out.println("P = play, S = Stop, R = Reset, Q = Quit");
System.out.print("Enter your choice: ");
response = scanner.next();
response = response.toUpperCase();
switch(response) {
case ("P"):
clip.start();
break;
case ("S"):
clip.stop();
break;
case ("R"):
clip.setMicrosecondPosition(0);//any digit, if reset then 0
break;
case ("Q"):
clip.close();
break;
default:
System.out.println("Not a valid response");
}
}
System.out.println("Byeeee!");
}
}
>>
P = play, S = Stop, R = Reset, Q = Quit
Enter your choice: q
Byeeee!
//Audio played when p, stop when s, played from start when r, quit the while loop when q