标题:求助,关于音乐播放器的程序
只看楼主
danialhu
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2008-4-10
 问题点数:0 回复次数:1 
求助,关于音乐播放器的程序
小弟自己编了个简易的mp3播放器,, 这个程序一运行,就能弹出个界面,然后手动载入音乐文件就能播放音乐了.  现在的问题是,我希望我一运行这个程序,不需要通过手动载入音乐文件,而是自动载入某首音乐,,  然后就可以播放音乐了.  我的意思就是我一运行这个程序,就会有音乐自动响起来,请问各位前辈,应该怎样修改这个程序呢.程序如下:

import java.awt.BorderLayout;
import java.awt.CheckboxMenuItem;
import java.awt.Color;
import java.
import java.awt.Container;
import java.awt.FileDialog;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.MenuItem;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.media.ControllerClosedEvent;
import javax.media.ControllerEvent;
import javax.media.ControllerListener;
import javax.media.EndOfMediaEvent;
import javax.media.GainControl;
import javax.media.Manager;
import javax.media.MediaLocator;
import javax.media.NoPlayerException;
import javax.media.Player;
import javax.media.PrefetchCompleteEvent;
import javax.media.RealizeCompleteEvent;
import javax.media.Time;
import javax.swing.JFrame;


class MediaPlayer
    extends JFrame
    implements ActionListener, ControllerListener, ItemListener {

    Player player;
    Component vc, cc, gcc;
    GainControl gc;
    
    boolean first = true, loop = false;
    
    String currentDirectory;

    MediaPlayer(String title) {
        super(title);

        
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                
                dispose();
            }
            public void windowClosed(WindowEvent e) {
                if (player != null)
                    player.close();
                System.exit(0);
            }
        });

        
        setupMenu();

        setSize(400, 400);
        setVisible(true);

    }

    
    public void setupMenu() {

        
        Menu f = new Menu("file");

        
        MenuItem mi = new MenuItem("open");
        mi.addActionListener(this);
        f.add(mi);
        f.addSeparator();

        CheckboxMenuItem cbmi = new CheckboxMenuItem("cycle", false);
        cbmi.addItemListener(this);
        f.add(cbmi);
        f.addSeparator();

        mi = new MenuItem("exit");
        mi.addActionListener(this);
        f.add(mi);

        Menu l = new Menu("playlist");

        Menu c = new Menu("control menu");

        MenuItem move = new MenuItem("play");
        move.addActionListener(this);
        c.add(move);
        c.addSeparator();

        MenuItem pause = new MenuItem("pause");
        pause.addActionListener(this);
        c.add(pause);
        c.addSeparator();

        MenuItem stop = new MenuItem("stop");
        stop.addActionListener(this);
        c.add(stop);
        c.addSeparator();

        MenuBar mb = new MenuBar();

        
        mb.add(f);
        mb.add(c);
        mb.add(l);

    
        setMenuBar(mb);
    }

    
    public void actionPerformed(ActionEvent e) {

        
        String cufile , selcectfile;

        if (e.getActionCommand().equals("exit")) {
            
            dispose();
            return;
        }

        
        if (e.getActionCommand().equals("play")) {
            if (player != null) {
                player.start();
            }
            return;
        }

        
        if (e.getActionCommand().equals("pause")) {

            if (player != null) {
                player.stop();
            }
            return;
        }

        
        if (e.getActionCommand().equals("stop")) {
            if (player != null) {
                player.stop();
                player.setMediaTime(new Time(0));
            }
            return;
        }

        
        if (e.getActionCommand().equals("open")) {

            FileDialog fd = new FileDialog(this, "open mediafile", FileDialog.LOAD);
            fd.setDirectory(currentDirectory);
            fd.show();

            
            if (fd.getFile() == null)
                return;

            
            selcectfile = fd.getFile();
            currentDirectory = fd.getDirectory();
            cufile = currentDirectory + selcectfile;

            
            MenuItem mi = new MenuItem(selcectfile);
            //mi.setActionCommand(cufile);
            MenuBar mb = getMenuBar();
            Menu m = mb.getMenu(2);
            mi.addActionListener(this);
            m.add(mi);

        } else {
            
            cufile = e.getActionCommand();
            selcectfile = cufile;
        }

        
        if (player != null)
            player.close();
        try {
            player = Manager.createPlayer(new MediaLocator("file:" + cufile));

        } catch ( e2) {
            System.out.println(e2);
            return;
        } catch (NoPlayerException e2) {
            System.out.println("can not find player.");
            return;
        }
        if (player == null) {
            System.out.println("can not set up player.");
            return;
        }

        first = false;
        setTitle(selcectfile);
        
        player.addControllerListener(this);
        player.prefetch();
    }

    
    public void controllerUpdate(ControllerEvent e) {

        Container tainer = getContentPane();

        
        if (e instanceof ControllerClosedEvent) {
            if (vc != null) {
                remove(vc);
                vc = null;
            }
            if (cc != null) {
                remove(cc);
                cc = null;
            }
            return;
        }

        
        if (e instanceof EndOfMediaEvent) {
            player.setMediaTime(new Time(0));
            if (loop) {
                player.start();
            }
            return;
        }

        
        if (e instanceof PrefetchCompleteEvent) {
            player.start();
            return;
        }

        
        if (e instanceof RealizeCompleteEvent) {
            
            vc = player.getVisualComponent();
            if (vc != null)
                tainer.add(vc, BorderLayout.CENTER);

            
            cc = player.getControlPanelComponent();
            cc.setBackground(Color.BLUE);
            if (cc != null)
                tainer.add(cc, BorderLayout.SOUTH);

            
            /*gc = player.getGainControl();
            gcc = gc.getControlComponent();
            if (gcc != null)
                tainer.add(gcc, BorderLayout.NORTH);*/

                
            if (vc != null) {
                pack();
                return;
            } else {

                setSize(300, 75);
                setVisible(true);
                return;
            }

        }

    }

    
    public void itemStateChanged(ItemEvent e) {
        loop = !loop;
    }

    
    public void update(Graphics g) {
        paint(g);
    }

    public static void main(String[] args) {
        new MediaPlayer("Simpler mediaplayer");
    }

    public void paint(Graphics g) {
 
        super.paint(g);

        //interface    
        if (first) {
            int w = getSize().width;
            int h = getSize().height;
            g.setColor(Color.magenta);
            g.fillRect(0, 0, w, h);
            Font f = new Font("DialogInput", Font.BOLD, 18);
            g.setFont(f);
            FontMetrics fm = g.getFontMetrics();
            int swidth = fm.stringWidth(" HU's MP3 PLAYER ");
            g.setColor(Color.white);
            g.drawString(
                " HU's MP3 PLAYER ",
                (w - swidth) / 2,
                (h + getInsets().top) / 2);
        }
    }

}
搜索更多相关主题的帖子: awt 音乐播放器 import java mp3播放器 
2008-04-10 20:20
黑衣骑士
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2009-12-29
得分:0 
朋友,你的程序能不能同步显示歌词啊,我急需一个能同步显示歌词的音乐播放器,界面和功能越简单越好,希望大虾有时间帮我一下!
2009-12-31 10:26



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-208278-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017705 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved