zoukankan      html  css  js  c++  java
  • 音乐播放器

    package cn.hncu.games;

    import java.applet.Applet;
    import java.applet.AudioClip;
    import java.awt.Color;
    import java.awt.Font;
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.awt.List;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;

    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;

    public class MusicPlayer extends JFrame{
    private JLabel songNameLabel=null;//显示播放状态及歌曲名
    private JButton btnLast = null;//上一首
    private JButton btnPlay = null;//播放/停止
    private JButton btnNext = null;//下一首
    private JButton btnLoop = null;//循环

    private List songList = null;
    //歌曲文件名数组---String
    String[] strSongNames={ "song1.wav","song2.wav","song3.wav","song4.wav","song5.wav","song6.wav" };
    final String DIR="songs\";
    //歌曲对象数组---AudioClip
    AudioClip songs[];
    AudioClip currentSong=null;
    int index=0;

    boolean isPlayOrStop = true;//播放状态(播放/停止),默认播放
    boolean isLoop= false;//是否循环播放

    public MusicPlayer() {
    super("音乐播放器");
    this.setBounds(300, 50, 310, 480);
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    this.setLayout(null);//空布局--绝对布局

    //标签:我的音乐播放器/歌曲名
    songNameLabel = new JLabel();
    songNameLabel.setText("我的音乐播放器");
    Font songNameFont = new Font("黑体",Font.ITALIC,18);
    songNameLabel.setFont(songNameFont);
    songNameLabel.setBounds(10, 10, 300, 40);//绝对布局时,由组件自己决定位置和大小
    this.getContentPane().add(songNameLabel);

    //四个播放功能键
    //构造
    btnLast = new JButton();
    btnPlay = new JButton();
    btnNext = new JButton();
    btnLoop = new JButton();
    //位置大小
    btnLast.setBounds(10, 70, 50, 40);
    btnPlay.setBounds(70, 70, 50, 40);
    btnNext.setBounds(130, 70, 50, 40);
    btnLoop.setBounds(190, 70, 50, 40);
    //图片
    btnLast.setIcon( new ImageIcon("images2\1.png") );
    btnPlay.setIcon( new ImageIcon("images2\2.png") );
    btnNext.setIcon( new ImageIcon("images2\3.png") );
    btnLoop.setIcon( new ImageIcon("images2\4.png") );
    //添加监听
    MouseListener ml = new MouseListenerC();
    btnLast.addMouseListener(ml);
    btnPlay.addMouseListener(ml);
    btnNext.addMouseListener(ml);
    btnLoop.addMouseListener(ml);

    //添加到界面
    getContentPane().add(btnLast);
    getContentPane().add(btnPlay);
    getContentPane().add(btnNext);
    getContentPane().add(btnLoop);

    //歌曲播放列表
    //列表上面的提示文字
    JLabel listLabel = new JLabel("播放列表");
    listLabel.setBounds(10, 120,100,30);
    Font listLabelFont = new Font("宋体",Font.BOLD,15);
    listLabel.setFont(listLabelFont);
    getContentPane().add(listLabel);
    //列表
    songList = new List();
    songList.setBounds(10, 150, 260, 260);
    songList.setBackground(Color.CYAN);
    //把歌曲名添加到List中
    songs = new AudioClip[ strSongNames.length ];
    for(int i=0;i<strSongNames.length;i++){
    songList.add( strSongNames[i]);
    songs[i] = loadSound(DIR+ strSongNames[i]);
    }
    getContentPane().add(songList);

    //程序刚启动时,播放第一首歌曲
    MusicRun mr = new MusicRun();
    Thread t = new Thread(mr);
    t.start();

    setVisible(true);
    }

    private AudioClip loadSound(String filename) {
    URL url = null;
    try {
    url = new URL("file:"+filename);
    } catch (MalformedURLException e) {
    e.printStackTrace();
    }
    return Applet.newAudioClip(url);
    }
    public static void main(String[] args) {
    MusicPlayer mp = new MusicPlayer();
    }

    private class MusicRun implements Runnable{
    @Override
    public void run() {
    currentSong = songs[index];
    if(isLoop){
    currentSong.loop();
    }
    if(isPlayOrStop){
    currentSong.play();
    }
    btnPlay.setIcon(new ImageIcon("images2\5.png"));
    }
    }

    private class MouseListenerC implements MouseListener{
    @Override
    public void mouseClicked(MouseEvent e) {
    currentSong.stop();

    if(e.getSource()==btnPlay){
    isPlayOrStop = !isPlayOrStop;
    }else if(e.getSource()==btnNext){
    index++;
    index = index % strSongNames.length;
    isPlayOrStop = true;
    } else if(e.getSource()==btnLast){
    index--;
    if(index<0){
    index = index+strSongNames.length;
    }
    isPlayOrStop = true;
    } else if(e.getSource()==btnLoop){
    isLoop= !isLoop;
    }

    if(isPlayOrStop){
    MusicRun mr = new MusicRun();
    Thread t = new Thread(mr);
    t.start();
    }else{
    btnPlay.setIcon(new ImageIcon("images2\2.png"));
    }
    }
    @Override
    public void mousePressed(MouseEvent e) {
    }
    @Override
    public void mouseReleased(MouseEvent e) {
    }
    @Override
    public void mouseEntered(MouseEvent e) {
    }
    @Override
    public void mouseExited(MouseEvent e) {
    }
    }
    }

  • 相关阅读:
    解决 Mac launchpad 启动台 Gitter 图标无法删除的问题
    React 与 React-Native 使用同一个 meteor 后台
    解决 React-Native mac 运行报错 error Failed to build iOS project. We ran "xcodebuild" command but it exited with error code 65. To debug build logs further, consider building your app with Xcode.app, by ope
    一行命令更新所有 npm 依赖包
    swift学习笔记
    IOS语言总结
    focusSNS学习笔记
    别小看锤子,老罗真的很认真
    windowsphone开发页面跳转到另一个dll中的页面
    【令人振奋】【转】微软潘正磊谈DevOps、Visual Studio 2013新功能、.NET未来
  • 原文地址:https://www.cnblogs.com/1314wamm/p/5600044.html
Copyright © 2011-2022 走看看