I know how to play audio, the trouble is that the computer skips over the line. I watched a tutorial that said to use the .next() object in the Scanner class, to make the computer wait for user input, to stop the audio(a song) or replay it, but I want my user input to use a JTextField and JButton so how would I go about doing that?
I have 2 classes, the first class takes in the the input of the other(whether to play, pause, skip, stop), and does an action.
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
import javax.sound.sampled.*;
public class Main {
public static void main(String[] args) {
new myFrame();
}
public static void playMusic( String response, String response2) throws
UnsupportedAudioFileException, IOException, LineUnavailableException {
Scanner scanner = new Scanner(System.in);
File song1 = new File("Underground Academy - Hanu Dixit.wav");
File song2 = new File("Body of Water - TrackTribe.wav");
AudioInputStream audioStream = AudioSystem.getAudioInputStream(song1);
Clip clip = AudioSystem.getClip();
long position = 0;
int pause = 0;
while(!response.equals("Q")) {
response = response.toUpperCase();
switch(response) {
case("P"):
//System.out.println("1 = Song 1, 2 = Song 2");
//System.out.println("Enter Your Song Choice: ");
switch(response2) {
case("1"):
clip.stop();
audioStream = AudioSystem.getAudioInputStream(song1);
clip = AudioSystem.getClip();
clip.open(audioStream);
break;
case("2"):
clip.stop();
audioStream = AudioSystem.getAudioInputStream(song2);
clip = AudioSystem.getClip();
clip.open(audioStream);
break;
default: System.out.println("Not a valid song response, I'll play
the default song instead...hahahaha!!!");
}
clip.start();
pause = 0;
break;
case("S"): clip.stop();
pause = 0;
break;
case("R"): clip.setMicrosecondPosition(0);
pause = 0;
break;
case("K"):
if (pause == 0) {
pause = 1;
position = clip.getMicrosecondPosition();
clip.stop();
System.out.println("You paused!");
break;
}
if (pause == 1) {
pause = 0;
clip.setMicrosecondPosition(position);
clip.start();
System.out.println("You unpaused!");
break;
}
case("Q"): clip.close();
break;
default: System.out.println("Not a valid action response");
}
}
System.out.println("You quit the program! Thanks for using Rohan's media player!
Thanks to Bro Code for helping me code this!!!!");
}
}
And the other class which has the GUI and feeds the actions:
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.awt.event.ActionEvent;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.swing.*;
public class myFrame implements ActionListener {
static int timesAnswered = 0;
String answer1;
String answer2;
static JButton button;
static JTextField field;
static JLabel label;
myFrame() {
JPanel panel = new JPanel();
panel.setLayout(new FlowLayout());
JFrame frame = new JFrame();
frame.setSize(250, 250);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setTitle("Rohan's Media Player... jk just audion ):");
frame.setResizable(false);
frame.add(panel);
button = new JButton("ENTER");
button.setPreferredSize(new Dimension(150, 50));
button.addActionListener(this);
field = new JTextField();
field.setPreferredSize(new Dimension(150, 50));
label = new JLabel("P = Play, S = Stop, R = Reset, Q = Quit, K = toggle pause");
panel.add(label);
panel.add(field);
panel.add(button);
frame.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if (timesAnswered == 0) {
answer1 = field.getText();
label.setText("1 = Song 1, 2 = Song 2");
timesAnswered = 2;
return;
}
if (timesAnswered == 2) {
label.setText("P = Play, S = Stop, R = Reset, Q = Quit, K = toggle pause");
answer2 = field.getText();
timesAnswered = 1;
try {
Main.playMusic(answer1, answer2);
} catch (UnsupportedAudioFileException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
} catch (LineUnavailableException e1) {
e1.printStackTrace();
}
}
}
}