0

I have a program that asks for user input and opens the corresponding mp3 file from a folder.

import java.awt.*;
import java.io.*;
import java.util.*;
class openmp3{
   public static void main(String[] args)throws Exception{
      Scanner console = new Scanner(System.in);
      Desktop d = Desktop.getDesktop(); 
      System.out.print("Enter song name: ");
      String song = console.nextLine();
      File f = new File("C:\\Users\\Leo\\Music\\" + song + ".mp3"); 
      d.open(f);   
   }
}

This code works fine, but is there a way to ask for user input and close the music file that's playing? Maybe have the user enter 2 and close the playing file.

Roman C
  • 49,761
  • 33
  • 66
  • 176
user3211857
  • 117
  • 3
  • 6
  • I don't think so. [open](http://docs.oracle.com/javase/7/docs/api/java/awt/Desktop.html#open%28java.io.File%29) doesn't return a handle to anything, and the process is "launched" independently. – Elliott Frisch Jan 22 '14 at 19:14

3 Answers3

1

Why not just play it using Java Sound? Then you have total control. I made DukeBox using Java Sound, the JMF MP3 SPI, and BigClip.

Here is a screenshot of DukeBox:

Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • This looks like something I'm interested in. Did you build this? How can I started, if you don't mind teaching? I'm still kind of new to Java programming. – user3211857 Jan 23 '14 at 05:59
  • *"Did you build this?"* Yes. It is my default music player now. *"How can I started,"* Start with the [Java Sound info. page](http://stackoverflow.com/tags/javasound/info) & linked Q&As Then go through the [Java Sound Trail](http://docs.oracle.com/javase/tutorial/sound/) of the tutorial, ignoring the parts on MIDI. *"if you don't mind teaching?"* Sorry, I don't really have the time at the moment. If you come up with specific questions on things of which I'm knowledgeable and ask on SO, I'll do what I can to help. – Andrew Thompson Jan 23 '14 at 07:10
0

If you know the application which plays the music, you can close it like this:

Process proc = rt.exec("taskkill /F /IM musicplayer.exe");
Sujith Surendranathan
  • 2,569
  • 17
  • 21
0

No, this is not possible. java.awt.Desktop#open(File) "Launches the associated application to open the file.". Meaning that it opens the program associated with MP3 files. This will be completely independent from your application and you also don't know what program will be associated with MP3 files. If you want to control playback of MP3s, play them within your application with Media and MediaPlayer.

Community
  • 1
  • 1
Windwaker
  • 111
  • 1
  • 9