Wednesday, 2 October 2013

I want to play, pause and stop songs using an android app in my java server

I want to play, pause and stop songs using an android app in my java server

I am working with my an android app that would play a song in a java
server Could you guys please help me how do i stop or even pause songs in
my java server?
My server code goes like this
public server(String filename) {
this.filename = filename;
}
public static void main(String[] args) throws Exception
{
try
{
serverSocket = new ServerSocket(PORT, 0,
InetAddress.getLocalHost());
System.out.println("IP: " + serverSocket.getInetAddress() + "
Port: " + serverSocket.getLocalPort());
} catch (IOException e)
{
System.out.println("Could not listen on port: 7777");
}
System.out.println("Server started. Listening to the port 7777");
while (true)
{
try
{
clientSocket = serverSocket.accept(); // Code para sa
client connection
inputStreamReader = new
InputStreamReader(clientSocket.getInputStream());
bufferedReader = new BufferedReader(inputStreamReader); //
irread ng inputstream yung message na sinend
message = bufferedReader.readLine();
System.out.println(message);{
if(message.equals("play1")){
AudioInputStream inputStream =
AudioSystem.getAudioInputStream(new
File("D:/Kym/From BB/kayMani.wav"));
Clip clip1 = AudioSystem.getClip();
// getAudioInputStream() accepts a File or
InputStream
clip1.open(inputStream);
clip1.start();
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JOptionPane.showMessageDialog(null,
"Classical song is playing!");
}
});
}
if(message.equals("play2")) {
AudioInputStream inputStream =
AudioSystem.getAudioInputStream(new
File("D:/Kym/From BB/kayMani.wav"));
Clip clip2 = AudioSystem.getClip();
// getAudioInputStream() accepts a File or
InputStream
clip2.open(inputStream);
clip2.start();
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JOptionPane.showMessageDialog(null,
"Novelty song is playing!");
}
});
}
if(message.equals("play3")) {
AudioInputStream inputStream =
AudioSystem.getAudioInputStream(new File("D:/Kym/From
BB/kayMani.wav"));
Clip clip2 = AudioSystem.getClip();
// getAudioInputStream() accepts a File or
InputStream
clip2.open(inputStream);
clip2.start();
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JOptionPane.showMessageDialog(null, "OPM
song is playing!");
}
});
}
if(message.equals("play4")) {
AudioInputStream inputStream =
AudioSystem.getAudioInputStream(new File("D:/Kym/From
BB/kayMani.wav"));
Clip clip2 = AudioSystem.getClip();
// getAudioInputStream() accepts a File or
InputStream
clip2.open(inputStream);
clip2.start();
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JOptionPane.showMessageDialog(null, "Pop
song is playing!");
}
});
}
if(message.equals("play5")) {
AudioInputStream inputStream =
AudioSystem.getAudioInputStream(new File("D:/Kym/From
BB/kayMani.wav"));
Clip clip2 = AudioSystem.getClip();
// getAudioInputStream() accepts a File or
InputStream
clip2.open(inputStream);
clip2.start();
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JOptionPane.showMessageDialog(null, "Rock
song is playing!");
}
});
}
}
inputStreamReader.close();
clientSocket.close();
} catch (IOException ex)
{
System.out.println("Problem in message reading");
}
}
}
}
another problem is it plays song but if it receives the same message like
play1 again while the song is playing the server crashes. Please help me
how to prevent that thanks in advance :D

No comments:

Post a Comment