user3211857
user3211857

Reputation: 117

open and close a mp3 file in java

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.

Upvotes: 0

Views: 3933

Answers (3)

Andrew Thompson
Andrew Thompson

Reputation: 168825

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:

Upvotes: 1

Windwaker
Windwaker

Reputation: 111

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.

Upvotes: 0

Sujith Surendranathan
Sujith Surendranathan

Reputation: 2579

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

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

Upvotes: 0

Related Questions