音乐播放器_Java代码实现

        这段时间研究了一下java中的Swing组件,虽然功能有限但还是可以实现一定的前端显示效果。基于Swing组件做了一个由Java代码实现的简易音乐播放器,予以记录。

        点击“选择目录”选中目标文件夹,然后点击“search”按钮列出文件夹中所有文件。在左边列表中选中音乐文件点击“播放”或者直接双击文件名称即可播放。界面效果:

        代码:

import jmp123.PlayBack;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;

public class Audio_Player extends JFrame {
    Thread thread = null;
    String initPath = ".";

    public static void main(String[] args) {
        new Audio_Player();
    }

    public Audio_Player(){
        setTitle("Player");
        setLayout(null);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setSize(500, 330);
        setLocation(600, 300);
        setResizable(false);
        Container container = getContentPane();

        JList<String> filesJList = new JList<>();
        filesJList.setFixedCellHeight(30);
        filesJList.setFont(new Font("宋体", Font.BOLD, 24));

        JScrollPane fileListPane = new JScrollPane(filesJList);
        fileListPane.setBounds(0, 0, 300, 280);

        JButton playButton = new JButton("播放");
        playButton.setBounds(300, 20, 150, 30);
        playButton.setFont(new Font("宋体", Font.PLAIN, 24));

        JButton stopButton = new JButton("停止");
        stopButton.setBounds(300, 60, 150, 30);
        stopButton.setFont(new Font("宋体", Font.PLAIN, 24));
        stopButton.addActionListener(e -> {
            if(thread != null && thread.isAlive()){
                thread.stop();
                thread = null;
            }
        });

        JTextField dirField = new JTextField();
        dirField.setEditable(false);
        dirField.setBackground(Color.white);
        dirField.setFont(new Font("宋体", Font.BOLD, 20));
        dirField.setBounds(300, 140, 160, 30);

        JButton searchButton = new JButton("search");
        searchButton.setBounds(300, 100, 150, 30);
        searchButton.setFont(new Font("宋体", Font.PLAIN, 24));
        searchButton.addActionListener(e -> {
            String path = dirField.getText();

            if(!path.isEmpty()){
                File file = new File(path);

                if(file.isDirectory()) {
                    String[] list = file.list();
                    filesJList.setListData(list);
                } else {
                    String name = file.getName();
                    String[] list = new String[]{name};
                    filesJList.setListData(list);
                }
            }
        });

        JButton chooseDirButton = new JButton("选择目录");
        chooseDirButton.setBounds(300, 180, 150, 30);
        chooseDirButton.setFont(new Font("宋体", Font.PLAIN, 24));

        chooseDirButton.addActionListener(e -> {
            JFileChooser fileChooser = new JFileChooser(initPath);
            fileChooser.setMultiSelectionEnabled(true);
            fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
            fileChooser.showOpenDialog(this);

            File file = fileChooser.getSelectedFile();
            if(file != null){
                dirField.setText(file.getAbsolutePath());
                initPath = file.isDirectory() ? file.getAbsolutePath() : getDir(file.getAbsolutePath());
            }
        });

        playButton.addActionListener(e -> {
            String dir = dirField.getText();
            String value = filesJList.getSelectedValue();

            if(!dir.isEmpty() && !value.isEmpty()){
                File file = new File(dir);
                String path = ( file.isDirectory() ? (dir + "\\" + value) : dir );

                if(thread == null || !thread.isAlive()){
                    thread = new Thread("thread"){
                        @Override
                        public void run() {
                            try {
                                PlayBack player = new PlayBack(new jmp123.output.Audio());
                                player.open(path,"");
                                player.start(true);
                            } catch (Exception exp) {
                                exp.printStackTrace();
                            }
                        }
                    };
                } else {
                    thread.stop();
                    thread = null;

                    thread = new Thread("thread"){
                        @Override
                        public void run() {
                            try {
                                PlayBack player = new PlayBack(new jmp123.output.Audio());
                                player.open(path,"");
                                player.start(true);
                            } catch (Exception exp) {
                                exp.printStackTrace();
                            }
                        }
                    };
                }
                thread.start();
            }
        });

        filesJList.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if(e.getClickCount() == 2){
                    String dir = dirField.getText();
                    String value = filesJList.getSelectedValue();

                    if(!dir.isEmpty() && !value.isEmpty()){
                        File file = new File(dir);
                        String path = ( file.isDirectory() ? (dir + "\\" + value) : dir );

                        if(thread == null || !thread.isAlive()){
                            thread = new Thread("thread"){

                                @Override
                                public void run() {
                                    try {
                                        PlayBack player = new PlayBack(new jmp123.output.Audio());
                                        player.open(path,"");
                                        player.start(true);
                                    } catch (Exception exp) {
                                        exp.printStackTrace();
                                    }
                                }
                            };
                        } else {
                            thread.stop();
                            thread = null;

                            thread = new Thread("thread"){
                                @Override
                                public void run() {
                                    try {
                                        PlayBack player = new PlayBack(new jmp123.output.Audio());
                                        player.open(path,"");
                                        player.start(true);
                                    } catch (Exception exp) {
                                        exp.printStackTrace();
                                    }
                                }
                            };
                        }

                        thread.start();
                    }
                }
            }
        });

        container.add(fileListPane);
        container.add(playButton);
        container.add(stopButton);
        container.add(searchButton);
        container.add(dirField);
        container.add(chooseDirButton);

        setVisible(true);
    }

    public String getDir(String path){
        int idx = path.lastIndexOf("\\");
        return path.substring(0, idx);
    }
}

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值