简体   繁体   中英

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.

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:

如果您知道播放音乐的应用程序,则可以这样关闭它:

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM