| 论坛注册| 加入收藏 | 设为首页| RSS
Google
您当前的位置:首页 > Linux频道 > Linux开发区 > 软件开发

Linux系统下制作一个简单的MP3播放器

时间:2007-10-16 10:09:05  来源:Linux联盟收集整理  作者:
 

代码(一个简单的GUI,通过菜单打开文件并播放,要停止播放直接关闭窗口即可。) SjzLinux联盟

/** SjzLinux联盟

* @kissplayer.java SjzLinux联盟

* SjzLinux联盟

* SjzLinux联盟

* @author SjzLinux联盟

* @version 1.00 2007/10/13 SjzLinux联盟

*/ SjzLinux联盟

import java.awt.*; SjzLinux联盟

import javax.swing.*; SjzLinux联盟

import java.awt.event.*; SjzLinux联盟

import javax.swing.event.*; SjzLinux联盟

import java.io.*; SjzLinux联盟

import javax.media.*; SjzLinux联盟

import java.io.File; SjzLinux联盟

import java.io.IOException; SjzLinux联盟

import java.net.URL; SjzLinux联盟

import java.net.MalformedURLException; SjzLinux联盟

class KissFrame extends JFrame{ SjzLinux联盟

KissFrame() { SjzLinux联盟

super("Simple Video Player"); SjzLinux联盟

setSize(400, 300); SjzLinux联盟

setDefaultCloseOperation(EXIT_ON_CLOSE); SjzLinux联盟

JMenuBar jmb = new JMenuBar(); SjzLinux联盟

JMenu jMenu1 = new JMenu("文件"); SjzLinux联盟

JMenu jMenu2 = new JMenu("控制"); SjzLinux联盟

JMenuItem jMenuItem1_1 = new JMenuItem("打开"); SjzLinux联盟

JMenuItem jMenuItem1_2 = new JMenuItem("退出"); SjzLinux联盟

jmb.add(jMenu1); SjzLinux联盟

jmb.add(jMenu2); SjzLinux联盟

jMenu1.add(jMenuItem1_1); SjzLinux联盟

jMenu1.add(jMenuItem1_2); SjzLinux联盟

// Add action HERE SjzLinux联盟

MenuItemListener listener = new MenuItemListener(); SjzLinux联盟

jMenuItem1_1.addActionListener(listener); SjzLinux联盟

setJMenuBar(jmb); SjzLinux联盟

Container contentPane = getContentPane(); SjzLinux联盟

} SjzLinux联盟

private class MenuItemListener implements ActionListener { SjzLinux联盟

public void actionPerformed(ActionEvent event) { SjzLinux联盟

// System.out.println("jMenuItem1_1"); SjzLinux联盟

JFileChooser chooser = new JFileChooser("."); // "."表示本目录 SjzLinux联盟

int result = chooser.showOpenDialog(null); SjzLinux联盟

if(result == chooser.APPROVE_OPTION) SjzLinux联盟

{ SjzLinux联盟

File selectedFile = chooser.getSelectedFile(); SjzLinux联盟

SimpleAudioPlayer.beginPlay(selectedFile); SjzLinux联盟

} SjzLinux联盟

else if(result == chooser.CANCEL_OPTION) SjzLinux联盟

{ SjzLinux联盟

// 用户取消了操作 SjzLinux联盟

} SjzLinux联盟

} SjzLinux联盟

} SjzLinux联盟

public static void main (String[] args) { SjzLinux联盟

KissFrame sf = new KissFrame(); SjzLinux联盟

sf.setVisible(true); SjzLinux联盟

} SjzLinux联盟

} SjzLinux联盟

class SimpleAudioPlayer { SjzLinux联盟

private Player audioPlayer = null; SjzLinux联盟

public SimpleAudioPlayer(URL url) throws IOException, SjzLinux联盟

NoPlayerException, SjzLinux联盟

CannotRealizeException { SjzLinux联盟

audioPlayer = Manager.createRealizedPlayer(url); SjzLinux联盟

} SjzLinux联盟

public SimpleAudioPlayer(File file) throws IOException, SjzLinux联盟

NoPlayerException, SjzLinux联盟

CannotRealizeException { SjzLinux联盟

this(file.toURL()); SjzLinux联盟

} SjzLinux联盟

public void play() { SjzLinux联盟

audioPlayer.start(); SjzLinux联盟

} SjzLinux联盟

public void stop() { SjzLinux联盟

audioPlayer.stop(); SjzLinux联盟

audioPlayer.close(); SjzLinux联盟

} SjzLinux联盟

public static void beginPlay(File audioFile) { SjzLinux联盟

try { SjzLinux联盟

SimpleAudioPlayer player = new SimpleAudioPlayer(audioFile); SjzLinux联盟

player.play(); SjzLinux联盟

// player.stop(); SjzLinux联盟

} catch(IOException e) { SjzLinux联盟

System.out.println(e.getMessage()); SjzLinux联盟

} catch(NoPlayerException ee) { SjzLinux联盟

System.out.println(ee.getMessage()); SjzLinux联盟

} catch(CannotRealizeException eee) { SjzLinux联盟

System.out.println(eee.getMessage()); SjzLinux联盟

} SjzLinux联盟

} SjzLinux联盟

} SjzLinux联盟

来顶一下
近回首页
返回首页
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表
相关文章
栏目更新
栏目热门