java实现KTV音乐点播功能

Java实现KTV音乐点播功能,需要使用Java Swing实现图形化界面,并与音乐播放器进行集成。以下是一个简单的实现KTV音乐点播功能的示例代码:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javazoom.jlgui.basicplayer.BasicPlayer;
import javazoom.jlgui.basicplayer.BasicPlayerException;
import javazoom.jlgui.basicplayer.BasicPlayerListener;
 
public class KTVPlayer extends JFrame implements BasicPlayerListener {
 
    private JList<String> songList;
    private JButton playButton, pauseButton, stopButton, previousButton, nextButton;
    private JLabel statusLabel;
    private BasicPlayer player;
    private List<File> songs;
    private int currentSongIndex;
    private Random random;
 
    public KTVPlayer() {
        super("KTV Player");
 
        // 初始化窗口
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(400, 300);
        setLocationRelativeTo(null);
 
        // 初始化播放器
        player = new BasicPlayer();
        player.addBasicPlayerListener(this);
        songs = new ArrayList<>();
        currentSongIndex = -1;
        random = new Random();
 
        // 初始化歌曲列表
        DefaultListModel<String> model = new DefaultListModel<>();
        songList = new JList<>(model);
        JScrollPane scrollPane = new JScrollPane(songList);
        add(scrollPane, BorderLayout.CENTER);
        songList.addListSelectionListener(new ListSelectionListener() {
            public void valueChanged(ListSelectionEvent e) {
                if (!e.getValueIsAdjusting()) {
                    currentSongIndex = songList.getSelectedIndex();
                }
            }
        });
 
        // 初始化操作按钮
        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
        playButton = new JButton("播放");
        playButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (currentSongIndex >= 0 && currentSongIndex < songs.size()) {
                    File song = songs.get(currentSongIndex);
                    try {
                        player.open(song);
                        player.play();
                        statusLabel.setText(song.getName() + " 正在播放...");
                    } catch (BasicPlayerException ex) {
                        ex.printStackTrace();
                    }
                }
            }
        });
        buttonPanel.add(playButton);
 
        pauseButton = new JButton("暂停");
        pauseButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                try {
                    player.pause();
                    statusLabel.setText("暂停");
                } catch (BasicPlayerException ex) {
                    ex.printStackTrace();
                }
            }
        });
        buttonPanel.add(pauseButton);
 
        stopButton = new JButton("停止");
        stopButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                try {
                    player.stop();
                    statusLabel.setText("停止");
                } catch (BasicPlayerException ex) {
                    ex.printStackTrace();
                }
            }
        });
        buttonPanel.add(stopButton);
 
        previousButton = new JButton("上一首");
        previousButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (songs.size() > 0) {
                    currentSongIndex--;
                    if (currentSongIndex < 0) {
                        currentSongIndex = songs.size() - 1;
                    }
                    songList.setSelectedIndex(currentSongIndex);
                    playButton.doClick();
                }
            }
        });
        buttonPanel.add(previousButton);
 
        nextButton = new JButton("下一首");
        nextButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (songs.size() > 0) {
                    currentSongIndex++;
                    if (currentSongIndex >= songs.size()) {
                        currentSongIndex = 0;
                    }
                    songList.setSelectedIndex(currentSongIndex);
                    playButton.doClick();
                }
            }
        });
        buttonPanel.add(nextButton);
 
        add(buttonPanel, BorderLayout.SOUTH);
 
        // 初始化状态标签
        statusLabel = new JLabel("请选择歌曲");
        add(statusLabel, BorderLayout.NORTH);
 
        // 加载歌曲
        addSongs(new File("songs"));
 
        // 显示窗口
        setVisible(true);
    }
 
    private void addSongs(File dir) {
        if (dir.isDirectory()) {
            for (File file : dir.listFiles()) {
                addSongs(file);
            }
        } else if (dir.isFile() && dir.getName().endsWith(".mp3")) {
            songs.add(dir);
            ((DefaultListModel<String>)songList.getModel()).addElement(dir.getName());
        }
    }
 
    public void opened(Object stream, Map properties) {}
    public void progress(int bytesread, long microseconds, byte[] pcmdata, Map properties) {}
    public void stateUpdated(BasicPlayerEvent event) {
        if (event.getCode() == BasicPlayerEvent.PLAYING) {
            int seconds = (int)(player.getMicrosecondPosition() / 1000000);
            int minutes = seconds / 60;
            seconds = seconds % 60;
            String time = String.format("%02d:%02d", minutes, seconds);
            statusLabel.setText(songs.get(currentSongIndex).getName() + " " + time);
        }
    }
    public void setController(BasicController controller) {}
 
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new KTVPlayer();
            }
        });
    }
 
}

以上java实现KTV音乐点播功能示例中,实现了一个简单的KTV音乐点播功能,包括歌曲列表、播放、暂停、停止、上一首、下一首等操作。同时,集成了音乐播放器,并使用BasicPlayerListener接口实现了播放状态的监听,能够实时更新歌曲播放时间。需要注意的是,以上示例仅提供了一个简单的KTV音乐点播功能的实现思路,具体实现还需要根据实际需求进行调整和完善。