-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmusicplayer.java
More file actions
68 lines (66 loc) · 2.93 KB
/
musicplayer.java
File metadata and controls
68 lines (66 loc) · 2.93 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
import java.io.*;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
import java.io.File;
import java.util.Scanner;
public class musicplayer {
public static void main(String[]args)
{
//How to play audio with java (MP3->Lossy,music ditsribution &streaming,compresses audio to smaller size and low quality but portable
// wav ->uncompressed,,,proffessional audio editing,,stores files in large size with full quality
//au -> uncompressed,,, rarely used developed by sun microsystem for unix system
//aiff ->uncompressed,,,developed by apple used in mac environment for professional audio(like wav)
//copy the file name from clicking right on file in src and then click on copy file path and then name
String filepath="Claim To Fame - The Grey Room _ Clark Sims.wav";
File file=new File(filepath);//assigning filepath to file constructor
try(Scanner sc = new Scanner(System.in);//we can use try with resources in modern java
AudioInputStream audiostream=AudioSystem.getAudioInputStream(file))
{
//get some contro;s to pause ,play
Clip clip=AudioSystem.getClip();
clip.open(audiostream);//player (clip) opens audio
// we will take input from iser to play and pause
String response="";
while(!response.equals("Q"))
{
System.out.println("P=plau");
System.out.println("S=stop");
System.out.println("R=reset");
System.out.println("Q=quit");
System.out.println("Enter you choice");
response=sc.next().toUpperCase();
switch(response)
{
case "P"->clip.start();
case "S"->clip.stop();
case "R"->clip.setMicrosecondPosition(0);
case "Q"->clip.close();
default->System.out.print("invalid input");
}
}
}
catch(FileNotFoundException e)
{
System.out.print("Could not locate the file location");
}
catch(UnsupportedAudioFileException e){//for audio stream
// if used mp3 then this exception
System.out.print("Audio file not supported");
}
catch(LineUnavailableException e)//for clip
{
System.out.print("Unable to access audio resource");
}
catch(IOException e)
{
System.out.print("Something went wrong");
}
finally{
//optional
System.out.print("Bye!");
}
}
}