-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMusicPlayer.java
More file actions
62 lines (42 loc) · 1.77 KB
/
MusicPlayer.java
File metadata and controls
62 lines (42 loc) · 1.77 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
import javax.sound.sampled.*;
import java.io.*;
import java.util.Scanner;
public class MusicPlayer {
public static void main(String[] args){
//The audio must be of type (.wav,.au,.aiff)
String filePath="C:\\Users\\Jash Mistry\\Desktop\\A Caring Friend - Bad Snacks.wav"; //LOCATION OF YOUR FILE & FILE NAME WITH IT'S TYPE (.wav, .au , .aiff)
File file = new File(filePath);
try( Scanner scanner = new Scanner(System.in);AudioInputStream audioStream= AudioSystem.getAudioInputStream(file)){
Clip clip = AudioSystem.getClip();
clip.open(audioStream);
String response="";
while(!response.equals("Q")){
System.out.println("P = PLAY\nS = STOP\nR = RESET\nQ = 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");
}
}
}
catch(FileNotFoundException e){
System.out.println("Could not locate file");
}
catch(UnsupportedAudioFileException e){
System.out.println("Audio File not Supported!");
}
catch(LineUnavailableException e){
System.out.println("Unable to access audio resource");
}
catch(IOException e){
System.out.println("Something Went Wrong");
}
finally {
System.out.println("BYE!");
}
}
}