-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMusicPlayer.java
More file actions
60 lines (48 loc) · 2.01 KB
/
MusicPlayer.java
File metadata and controls
60 lines (48 loc) · 2.01 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
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
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;
public class MusicPlayer {
public static void main(String[] args) {
// Playing music with Java
String filePath = "Spaced Out.wav";
File file = new File(filePath);
try(AudioInputStream audioStream = AudioSystem.getAudioInputStream(file)) {
Scanner scanner = new Scanner(System.in);
Clip clip = AudioSystem.getClip();
clip.open(audioStream);
String response = "";
while(!response.equals("Q")) {
System.out.println("P = Play");
System.out.println("S = Stop");
System.out.println("R = Reset");
System.out.println("Q = Quit");
System.out.print("Enter your choice: ");
response = scanner.next().toUpperCase();
switch(response) {
case "P" -> clip.start();
case "S" -> clip.stop();
case "R" -> clip.setMicrosecondPosition(0);
case "Q" -> clip.close();
default -> System.out.println("Invalid choice!");
}
}
scanner.close();
} catch(FileNotFoundException e) {
System.out.println("Unable to locate audio file.");
} catch(IOException e) {
System.out.println("Something went wrong.");
} catch (UnsupportedAudioFileException e) {
System.out.println("Audio file is not supported.");
} catch(LineUnavailableException e) {
System.out.println("Unable to access audio file");
} finally {
System.out.println("Bye");
}
}
}