Question

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.

Was it helpful?

Solution

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:

OTHER TIPS

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

Process proc = rt.exec("taskkill /F /IM musicplayer.exe");

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top