- Here full source code(full project bottom of the page)
package mp3player;
import java.awt.Color;
import java.io.File;
import java.io.*;
import javax.swing.DefaultListModel;
import java.util.ArrayList;
import javax.swing.ImageIcon;
import javax.swing.UIManager;
/**
*
* @author ali
*/
public class playmp3 extends javax.swing.JFrame {
Playlist pl = new Playlist();
ArrayList updateList = new ArrayList();
javazoom.jl.player.Player player;
File simpan;
playmp3() {
initComponents();
this.setIconImage(new ImageIcon(getClass().getResource("icons8-music-48.png")).getImage());
}
void updateList() {
updateList = pl.getListSong();
DefaultListModel model = new DefaultListModel();
for (int i = 0; i < updateList.size(); i++) {
int j = i + 1;
model.add(i, j + " | " + ((File) updateList.get(i)).getName());
}
jPlaylist.setModel(model);
}
//panel kontrol
void add(){
pl.add(this);
updateList();
}
void remove(){
try{
int akandihapus = jPlaylist.getLeadSelectionIndex();
pl.ls.remove(akandihapus);
updateList();
}catch(Exception e){
}
}
void up(){
try{
int s1 = jPlaylist.getLeadSelectionIndex();
simpan = (File) pl.ls.get(s1);
pl.ls.remove(s1);
pl.ls.add(s1 - 1, simpan );
updateList();
jPlaylist.setSelectedIndex(s1-1);
}catch(Exception e){
}
}
void down(){
try{
int s1 = jPlaylist.getLeadSelectionIndex();
simpan = (File) pl.ls.get(s1);
pl.ls.remove(s1);
pl.ls.add(s1 + 1, simpan );
updateList();
jPlaylist.setSelectedIndex(s1+1);
}catch(Exception e){
}
}
void open(){
pl.openPls(this);
updateList();
}
void save(){
pl.saveAsPlaylist(this);
updateList();
}
File play1;
static int a = 0;
void putar(){
if(a==0){
try{
int p1 = jPlaylist.getSelectedIndex();
play1 = (File) this.updateList.get(p1);
FileInputStream fis = new FileInputStream(play1);
BufferedInputStream bis = new BufferedInputStream(fis);
player = new javazoom.jl.player.Player(bis);
a =1;
}catch(Exception e){
System.out.println("Problem playing file");
System.out.println(e);
}
new Thread(){
@Override
public void run(){
try{
player.play();
}catch (Exception e){
}
}
}.start();
}else{
player.close();
a=0;
putar();
}
}
File sa;
void next(){
if(a==0){
try{
int s1 = jPlaylist.getSelectedIndex() +1;
sa = (File) this.pl.ls.get(s1);
FileInputStream fis = new FileInputStream(sa);
BufferedInputStream bis = new BufferedInputStream(fis);
player = new javazoom.jl.player.Player(bis);
a =1;
jPlaylist.setSelectedIndex(s1);
}catch(Exception e){
System.out.println("Problem playing file");
System.out.println(e);
}
new Thread(){
@Override
public void run(){
try{
player.play();
}catch (Exception e){
}
}
}.start();
}else{
player.close();
a=0;
next();
}
}
void previous(){
if(a==0){
try{
int s1 = jPlaylist.getSelectedIndex() -1;
sa = (File) this.pl.ls.get(s1);
FileInputStream fis = new FileInputStream(sa);
BufferedInputStream bis = new BufferedInputStream(fis);
player = new javazoom.jl.player.Player(bis);
a =1;
jPlaylist.setSelectedIndex(s1);
}catch(Exception e){
System.out.println("Problem playing file");
System.out.println(e);
}
new Thread(){
@Override
public void run(){
try{
player.play();
}catch (Exception e){
}
}
}.start();
}else{
player.close();
a=0;
previous();
}
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jButton2 = new javax.swing.JButton();
jButton6 = new javax.swing.JButton();
rSLabelImage2 = new rojerusan.RSLabelImage();
rSLabelImage1 = new rojerusan.RSLabelImage();
jPanel1 = new javax.swing.JPanel();
jScrollPane1 = new javax.swing.JScrollPane();
jPlaylist = new javax.swing.JList<>();
btnAdd = new javax.swing.JButton();
btnRemove = new javax.swing.JButton();
ply = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
jButton5 = new javax.swing.JButton();
btnUp = new javax.swing.JButton();
btnDown = new javax.swing.JButton();
stop = new javax.swing.JButton();
jButton2.setText("jButton2");
jButton6.setText("jButton6");
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
setTitle("Music Player By Chathura Sanjaya");
getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
rSLabelImage2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/mp3player/icons8-music-48.png"))); // NOI18N
getContentPane().add(rSLabelImage2, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 10, 50, 50));
rSLabelImage1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icon/3359947-apple-player-music-black-blue.jpg"))); // NOI18N
getContentPane().add(rSLabelImage1, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 420, 410));
jPanel1.setBackground(new java.awt.Color(0, 0, 0));
jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
jPlaylist.setBackground(new java.awt.Color(64, 49, 130));
jPlaylist.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
jScrollPane1.setViewportView(jPlaylist);
jPanel1.add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 80, 320, 270));
btnAdd.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
btnAdd.setText("Add Music");
btnAdd.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnAddActionPerformed(evt);
}
});
jPanel1.add(btnAdd, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 20, 250, 40));
btnRemove.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icon/p_remove.png"))); // NOI18N
btnRemove.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnRemoveActionPerformed(evt);
}
});
jPanel1.add(btnRemove, new org.netbeans.lib.awtextra.AbsoluteConstraints(270, 20, 60, 40));
ply.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
ply.setText("PLAY");
ply.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
plyActionPerformed(evt);
}
});
jPanel1.add(ply, new org.netbeans.lib.awtextra.AbsoluteConstraints(140, 360, 90, -1));
jButton3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icon/icons8-back-arrow-64.png"))); // NOI18N
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});
jPanel1.add(jButton3, new org.netbeans.lib.awtextra.AbsoluteConstraints(80, 360, 50, 30));
jButton5.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icon/icons8-forward-button-64.png"))); // NOI18N
jButton5.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton5ActionPerformed(evt);
}
});
jPanel1.add(jButton5, new org.netbeans.lib.awtextra.AbsoluteConstraints(240, 360, 50, 30));
btnUp.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icon/p_up.png"))); // NOI18N
btnUp.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnUpActionPerformed(evt);
}
});
jPanel1.add(btnUp, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 360, 30, 30));
btnDown.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icon/p_down.png"))); // NOI18N
btnDown.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnDownActionPerformed(evt);
}
});
jPanel1.add(btnDown, new org.netbeans.lib.awtextra.AbsoluteConstraints(40, 360, 30, -1));
stop.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
stop.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icon/icons8-resume-button-40.png"))); // NOI18N
stop.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
stopActionPerformed(evt);
}
});
jPanel1.add(stop, new org.netbeans.lib.awtextra.AbsoluteConstraints(300, 360, 30, 30));
getContentPane().add(jPanel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(420, 0, 350, 410));
setSize(new java.awt.Dimension(774, 439));
setLocationRelativeTo(null);
}// </editor-fold>
private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {
add();
}
private void plyActionPerformed(java.awt.event.ActionEvent evt) {
putar();
ply.setBackground(Color.GREEN);
stop.setBackground(Color.white);// TODO add your handling code here:
}
private void btnRemoveActionPerformed(java.awt.event.ActionEvent evt) {
remove(); // TODO add your handling code here:
}
private void btnUpActionPerformed(java.awt.event.ActionEvent evt) {
up(); // TODO add your handling code here:
}
private void btnDownActionPerformed(java.awt.event.ActionEvent evt) {
down(); // TODO add your handling code here:
}
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
previous(); // TODO add your handling code here:
}
private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {
next(); // TODO add your handling code here:
}
private void stopActionPerformed(java.awt.event.ActionEvent evt) {
player.close();
stop.setBackground(Color.red);
ply.setBackground(Color.white);// TODO add your handling code here:
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
UIManager.setLookAndFeel("com.jtattoo.plaf.mint.MintLookAndFeel");
} catch (Exception ex) {
ex.printStackTrace();
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new playmp3().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton btnAdd;
private javax.swing.JButton btnDown;
private javax.swing.JButton btnRemove;
private javax.swing.JButton btnUp;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JButton jButton5;
private javax.swing.JButton jButton6;
private javax.swing.JPanel jPanel1;
private javax.swing.JList<String> jPlaylist;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JButton ply;
private rojerusan.RSLabelImage rSLabelImage1;
private rojerusan.RSLabelImage rSLabelImage2;
private javax.swing.JButton stop;
// End of variables declaration
}
you need to create public class playlist. Here the code of playlist
package mp3player;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
public class Playlist {
JFileChooser fc=new JFileChooser();
ArrayList ls=new ArrayList();
void add(JFrame frame){
fc.setMultiSelectionEnabled(true);
int fileValid=fc.showOpenDialog(frame);
if(fileValid==javax.swing.JFileChooser.CANCEL_OPTION){
return;
} else if(fileValid==javax.swing.JFileChooser.APPROVE_OPTION)
{
File[] file=fc.getSelectedFiles();
ls.addAll(Arrays.asList(file));
}
}
ArrayList getListSong(){
return ls;
}
//save playlist
FileOutputStream fos;
ObjectOutputStream oos;
public void saveAsPlaylist(JFrame frame){
fc.setMultiSelectionEnabled(true);
int Valid=fc.showOpenDialog(frame);
if(Valid==javax.swing.JFileChooser.CANCEL_OPTION){
return;
} else if(Valid==javax.swing.JFileChooser.APPROVE_OPTION)
{
File[] pls=fc.getSelectedFiles();
try{
fos=new FileOutputStream(pls + ".tgr");
oos=new ObjectOutputStream(fos);
for (int i=0; i<ls.size() ; i++){
File tmp = (File) ls.get(i);
oos.writeObject(tmp);
}
oos.close();
}catch(Exception e){
}
}
}
FileInputStream fis;
ObjectInputStream ois;
public void openPls(JFrame frame){
fc.setMultiSelectionEnabled(false);
int Valid = fc.showOpenDialog(frame);
if (Valid == javax.swing.JFileChooser.CANCEL_OPTION){
return;
}
if (Valid == javax.swing.JFileChooser.APPROVE_OPTION){
File pls = fc.getSelectedFile();
try{
fis = new FileInputStream(pls);
ois = new ObjectInputStream(fis);
File tmp;
while ((tmp = (File) ois.readObject()) != null){
ls.add(tmp);
}
if ((tmp = (File) ois.readObject()) == null){
ls.isEmpty();
}
ois.close();
}catch (Exception e){
}
}
}
}
0 Comments