SoFunction
Updated on 2025-03-06

Making simple music player with java

This article mainly uses swing technology in java and API in JMFjar. It shares the specific implementation code of java music player for your reference. The specific content is as follows

Note: You need to use JDK1.8 to play music MP3

package baidu;

import .*;
import .*;
import .*;
import .*;
import .*;
import .*; //This package needs to use JMF
public class MP3 extends JFrame implements Runnable {
public JLabel shijian;

private JSlider sldDiameter;

public MediaPlayer soumd1;

public JButton playSound, loopSound, stopsound;

public JButton tjian, shanc, baocun, duqu;

public JPanel jp1, jp2, jp3, jp4;

public JList jl;

int zongmiao=0;

public Vector vector, mingcheng;

boolean fo = false, geshi = false;

JLabel jl1, jl2, sj1, sj2;

JTextField jt1, jt2;

JButton queding, xiugai;

int zong = 0;

int a = 0, b = 0, you = 1,mm=0;

int fenzhong, miaozhong;

public MP3() {
 super("Java simple music player--Yan Kai");
 soumd1 = new MediaPlayer();
 Container c = getContentPane();
 (new FlowLayout());
 mingcheng = new Vector();
 jp1 = new JPanel();
 shijian = new JLabel();
 (shijian);
 (jp1);
 playSound = new JButton("Start Play");
 loopSound = new JButton("Loop play");
 stopsound = new JButton("Stop Playing");
 jp2 = new JPanel();
 (playSound);
 (loopSound);
 (stopsound);
 (jp2);
 jp4 = new JPanel();
 sj1 = new JLabel();
 sj2 = new JLabel();
 sldDiameter = new JSlider(, 0, 100, 0);
 (1);
 (true);
 (sj1);
 (sldDiameter);
 (sj2);
 (jp4);
 vector = new Vector();
 jl = new JList(mingcheng);
 (5);
 (40);
 (265);
 (ListSelectionModel.SINGLE_SELECTION);
 (new JScrollPane(jl));
 tjian = new JButton("Add to");
 shanc = new JButton("delete");
 duqu = new JButton("Read");
 baocun = new JButton("save");
 jp3 = new JPanel();
 (tjian);
 (shanc);
 (baocun);
 (duqu);
 (jp3);

 try {
 String s =" C:/Users/Administrator/Music/Zhuo Yiting - Pingju.mp3";
 ObjectInputStream input = new ObjectInputStream(
  new FileInputStream(s));
 lujin a1 = (lujin) ();
 mingcheng = ;
 vector = ;
 (mingcheng);
 } catch (Exception e) {
 ("The music file is not saved by the c disk");
 //();
 }

 (new ActionListener() {
 public void actionPerformed(ActionEvent event) {
 JFileChooser fileChooser = new JFileChooser(); // Instantiate the file selector fileChooser
  .setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); // Set file selection mode, here are both files and directories if (() == JFileChooser.APPROVE_OPTION) { // The file selector pops up and determines whether the Open button has been clicked  String fileName = ()
  .getAbsolutePath(); // Get the absolute path to select a file or directory  mmm(vector, mingcheng, fileName);
 }
 }
 });
 (new ActionListener() {
 public void actionPerformed(ActionEvent event) {
 JFileChooser fileChooser = new JFileChooser(); // Instantiate the file selector fileChooser
  .setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); // Set file selection mode, here are both files and directories if (() == JFileChooser.APPROVE_OPTION) { // The file selector pops up and determines whether the Open button has been clicked  String fileName = ()
  .getAbsolutePath(); // Get the absolute path to select a file or directory  try {
  ObjectInputStream input = new ObjectInputStream(
  new FileInputStream(fileName));
  lujin a1 = (lujin) ();
  mingcheng = ;
  vector = ;
  (mingcheng);
  } catch (Exception e) {
  ();
  }
 }
 }
 });
 (new ActionListener() {
 public void actionPerformed(ActionEvent e) {
 if (() >= 0) {
  String yy = (String) (());
  File ff = new File(yy);
  if (()) {
  if (("[\\S\\s]*.mp3")|| ("[\\S\\s]*.MP3")) {
  if (soumd1 != null) {
  a = 0;
  b = 0;
  you = 0;
  ();
  }
   ("file:/" + yy);
  fo = true;
  ();
  geshi=true;
  try {
  (500);
  } catch (InterruptedException eee) {
  }
  zongmiao=(int)().getSeconds();
  if(zongmiao>10000)
  {
  try {
   (500);
  } catch (InterruptedException ew) {
  }
  zongmiao=(int)().getSeconds();
  }
  zongmiao=(int)().getSeconds();
  String aa=fen(zongmiao);
   (aa);
  } else
  (null,
   "There is an error in the playback file format and cannot be played. It is recommended to delete it");
  } else
  (null,
  "This song file no longer exists, it is recommended to delete it");
 }
 else
  (null, "Please select music file");
 }
 });
 (new ActionListener() {
 public void actionPerformed(ActionEvent e) {
 if (() >= 0) {
  String yy = (String) (());
  File ff = new File(yy);
  if (()) {
  if (("[\\S\\s]*.mp3")|| ("[\\S\\s]*.MP3")) {
  if (soumd1 != null) {
  a = 0;
  b = 0;
  you = 0;
  ();
  }
   ("file:/" + yy);
  fo = true;
  ();
  geshi=true;
  try {
  (500);
  } catch (InterruptedException we) {
  }
  zongmiao=(int)().getSeconds();
  if(zongmiao>10000)
  {
  try {
   (500);
  } catch (InterruptedException we) {
  }
  zongmiao=(int)().getSeconds();
  }
  zongmiao=(int)().getSeconds();
  String aa=fen(zongmiao);
   (aa);
  } else
  (null,
   "There is an error in the playback file format and cannot be played. It is recommended to delete it");
  } else
  (null,
  "This song file no longer exists, it is recommended to delete it");

 }
 else
  (null, "Please select music file");
 }
 });
 (new ActionListener() {
 public void actionPerformed(ActionEvent e) {
 a = 0;
 b = 0;
 you = 0;
 mm=0;
 geshi=false;
 fo = false;
 (100);
 (0);
 (null);
 (null);
 if (() >= 0)
  ();

 }
 });
 (new ActionListener() {
 public void actionPerformed(ActionEvent event) {
 JFileChooser fileChooser = new JFileChooser(); // Instantiate the file selector fileChooser
  .setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); // Set file selection mode, here are both files and directories (new File(".")); // Set the file selector current directory fileChooser
  .setFileFilter(new () {
  public boolean accept(File file) { // Acceptable file types  String name = ().toLowerCase();
  return (".mp3")
   || ();
  }
  public String getDescription() { // Document description  return "Music Files(*.mp3)";
  }
  });
 if (() == JFileChooser.APPROVE_OPTION) { // The file selector pops up and determines whether the Open button has been clicked  String fileName = ()
  .getAbsolutePath(); // Get the absolute path to select a file or directory  (fileName);
  StringBuffer buffer = daoxu(fileName);
  (buffer);
 }
 }
 });
 (new ActionListener() {
 public void actionPerformed(ActionEvent event) {
 if (() >= 0) {
  (());
  (());
  (mingcheng);
 }
 }
 });
 (new MouseAdapter() {
 public void mousePressed(MouseEvent event) {
 if (() == 2) {
  if (() >= 0) {
  String yy = (String) (());
  File ff = new File(yy);
  if (()) {
  if (("[\\S\\s]*.mp3")|| ("[\\S\\s]*.MP3")) {
  if (soumd1 != null) {
   a = 0;
   b = 0;
   you = 0;
   ();
  }
   ("file:/" + yy);
  fo = true;
  ();
  geshi=true;
  try {
   (500);
  } catch (InterruptedException e) {
  }
  zongmiao=(int)().getSeconds();
  if(zongmiao>10000)
  {
   try {
   (500);
   } catch (InterruptedException e) {
   }
  zongmiao=(int)().getSeconds();
  }
  String aa=fen(zongmiao);
   (aa);
  } else
  (null,
   "There is an error in the playback file format and cannot be played. It is recommended to delete it");
  } else
  (null,
   "This song file no longer exists, it is recommended to delete it");

  }
 }
 if (()) {
  if (() >= 0) {
  int a = ();
  xiugai x = new xiugai();
  ((()) + "");
  ((()) + "");
  }
 }
 }
 });
 addWindowListener(new WindowAdapter() {
 public void windowClosing(WindowEvent e) {
 String s = "c:\\";
 mmm(vector, mingcheng, s);
 (0);
  }
 });
 setSize(300, 400);
 setVisible(true);
}

public void run() {
 while (true) {
 Date now = new Date();
 //("Current time: " + ()+"year"+()+"month"+()+"day"+()); ("Current time:"+(new ("yyyy-MM-dd HH:mm:ss")).format(new Date())+"Yan Kai");
 try {
 (1000);
 } catch (InterruptedException e) {
 }
 if(geshi){
 you=(int)().getSeconds();
 mm =you%60;
  if(you!=0)
 if(you%60==0)
 {
  b++;
 }
  (b+":"+mm);
 (zongmiao);
 if(you!=zongmiao)
  (you);
 else {
  (0);
 mm=0;
  b=0;
 }
 }
 }
}

public StringBuffer daoxu(String fileName) {
 String mc = "";
 for (int i = (); i >= 1; i--) {
 if ((i - 1) == '\\')
 break;
 mc += (i - 1);
 }
 StringBuffer buffer = new StringBuffer(mc);
 StringBuffer mm = ();
 return mm;
}

public void mmm(Vector vector, Vector mingcheng, String lujin) {
 lujin a = new lujin(vector, mingcheng);
 try {
 ObjectOutputStream output = new ObjectOutputStream(new FileOutputStream(lujin));
 (a);
 ();
 ();
 } catch (Exception e) {

 }
}
public String fen(int yy) {
 fenzhong = (int) yy / 60;
 miaozhong = (int) yy % 60;
 String sss = fenzhong + ":" + miaozhong;
 return sss;
}

public static void main(String agrs[]) {
 MP3 s = new MP3();
 Thread t1 = new Thread(s);
 ();
 (JFrame.EXIT_ON_CLOSE);
}

class xiugai extends JFrame {
 public xiugai() {
 jl1 = new JLabel("file name");
 jt1 = new JTextField(20);
 jl2 = new JLabel("File Path");
 jt2 = new JTextField(20);
 xiugai = new JButton("Revise");
 Container c = getContentPane();
 (new GridLayout(3, 1));
 JPanel j1 = new JPanel();
 JPanel j2 = new JPanel();
 JPanel j3 = new JPanel();
 (jl1);
 (jt1);
 (jl2);
 (jt2);
 (xiugai);
 (j1);
 (j2);
 (j3);
 (new ActionListener() {
 public void actionPerformed(ActionEvent event) {
  ((), ());
  ((), ());
  (mingcheng);
  dispose();
 }
 });
 setSize(300, 120);
 setVisible(true);
 }
}
}

class lujin implements Serializable {

Vector a = new Vector();

Vector b = new Vector();

public lujin(Vector vector, Vector mingcheng) {
 a = vector;
 b = mingcheng;
}

} 

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.