求java声音代码,可以播mp3的谢谢了!
2个回答
展开全部
package baseitem;
import java.util.*;
import java.net.*;
import java.lang.reflect.Method;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import javax.media.*;
import javax.media.util.*;
import javax.media.format.*;
import javax.media.control.*;
import javax.media.protocol.*;
import javax.media.bean.playerbean.*;
import javax.media.rtp.*;
import javax.media.rtp.event.*;
import javax.swing.border.*;
public class MediaPlayer extends JPanel implements ControllerListener{
//定义界面组件
JMenuBar mainMenu= new JMenuBar();
JMenu fileMenu = new JMenu("文件");
JMenu playMenu = new JMenu("播放");
JMenu helpMenu = new JMenu("帮助");
JMenuItem openMenuItem= new JMenuItem("打开");
JMenuItem exitMenuItem = new JMenuItem("退出");
JCheckBoxMenuItem loopMenuItem= new JCheckBoxMenuItem("循环");
JMenuItem aboutMenuItem = new JMenuItem("关于");
JLabel status = new JLabel("欢迎使用视频播放系统");
JPanel mediaPanel = new JPanel();
Player player;
Component visualcom, controlcom;
boolean first = true, loop = false;
String currentDirectory="d:/music";//记录所选媒体文件位置
JInternalFrame min;
public MediaPlayer(JInternalFrame mi){
min=mi;
setLayout(new BorderLayout());
setBackground(Color.lightGray);
setLocale(new java.util.Locale("zh", "CN", ""));
mediaPanel.setToolTipText("视频窗口");
mediaPanel.setVerifyInputWhenFocusTarget(true);
mediaPanel.setLayout(new BorderLayout());
status.setToolTipText("状态栏");
mainMenu.setToolTipText("菜单栏");
fileMenu.add(openMenuItem);
openMenuItem.addActionListener(new OpenFile(this));
fileMenu.add(exitMenuItem);
exitMenuItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
min.dispose ();
player.stop();
return;
}
});
playMenu.add(loopMenuItem);
loopMenuItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
loop = !loop;
}
});
helpMenu.add(aboutMenuItem);
aboutMenuItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
//显示"关于"窗口
About dlg = new About();
Dimension dlgSize = dlg.getPreferredSize();
Dimension frmSize = getSize();
Point loc = getLocation();
dlg.setLocation((frmSize.width - dlgSize.width) / 2 + loc.x,
(frmSize.height - dlgSize.height) / 2 + loc.y);
dlg.setModal(true);
dlg.pack();
dlg.setVisible(true);
}
});
mainMenu.add(fileMenu);
mainMenu.add(playMenu);
mainMenu.add(helpMenu);
status.setBorder(BorderFactory.createLoweredBevelBorder());
status.setOpaque(true);
status.setIcon(new ImageIcon(baseitem.MediaPlayer.class.getResource("logo.png")));
add(mainMenu,BorderLayout.NORTH);
add(mediaPanel, BorderLayout.CENTER);
add(status, BorderLayout.SOUTH);
}
public void controllerUpdate (ControllerEvent e){
// 调用player.close()方法时ControllerClosedEvent事件发生
// 如果存在视觉组件,则该组件件应该拆除(为一致起见,
// 我们对控制面板组件也执行同样的操作)
if(e instanceof ControllerClosedEvent){
if(visualcom != null){
mediaPanel.remove(visualcom);
visualcom = null;
}
if(controlcom != null){
mediaPanel.remove(controlcom);
controlcom = null;
}
return;
}
//如播放结束则EndOfMediaEvent事件发生,检查是否为循环播放
//如是则调用播放器start()方法继续播放
if(e instanceof EndOfMediaEvent){
if(loop){
player.setMediaTime(new Time(0));
player.start();
}
return;
}
if(e instanceof PrefetchCompleteEvent){
player.start();
return;
}
if(e instanceof RealizeCompleteEvent){
visualcom = player.getVisualComponent();
if(visualcom != null)
mediaPanel.add(visualcom, BorderLayout.CENTER);
controlcom = player.getControlPanelComponent();
if(controlcom != null)
mediaPanel.add(controlcom, BorderLayout.SOUTH);
}
min.pack();
}
//用于处理"打开"菜单项的类
class OpenFile implements ActionListener{
MediaPlayer mp;
public OpenFile(MediaPlayer mp){
this.mp=mp;
}
public void actionPerformed(ActionEvent e) {
//构造文件选择组件实例并设置其属性
JFileChooser fd=new JFileChooser();
fd.setSelectedFile(new File(currentDirectory));
fd.setFileSelectionMode(JFileChooser.FILES_ONLY);
int result=fd.showOpenDialog(min);
// 如果用户放弃选择文件,则返回
if(result==JFileChooser.CANCEL_OPTION)
return;
currentDirectory = fd.getSelectedFile().toString();
if (player != null)
player.close();
try {
player = Manager.createPlayer(new MediaLocator("file:"+currentDirectory));
}
catch (java.io.IOException e2) {
System.out.println(e2);
return;
}
catch (NoPlayerException e2) {
System.out.println("不能找到播放器.");
return;
}
if (player == null) {
System.out.println("无法创建播放器.");
return;
}
first = false;
min.setTitle("播放文件位置 " + currentDirectory);
//为播放组件注册监听器
player.addControllerListener (mp);
player.prefetch();
}
}
}
import java.util.*;
import java.net.*;
import java.lang.reflect.Method;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import javax.media.*;
import javax.media.util.*;
import javax.media.format.*;
import javax.media.control.*;
import javax.media.protocol.*;
import javax.media.bean.playerbean.*;
import javax.media.rtp.*;
import javax.media.rtp.event.*;
import javax.swing.border.*;
public class MediaPlayer extends JPanel implements ControllerListener{
//定义界面组件
JMenuBar mainMenu= new JMenuBar();
JMenu fileMenu = new JMenu("文件");
JMenu playMenu = new JMenu("播放");
JMenu helpMenu = new JMenu("帮助");
JMenuItem openMenuItem= new JMenuItem("打开");
JMenuItem exitMenuItem = new JMenuItem("退出");
JCheckBoxMenuItem loopMenuItem= new JCheckBoxMenuItem("循环");
JMenuItem aboutMenuItem = new JMenuItem("关于");
JLabel status = new JLabel("欢迎使用视频播放系统");
JPanel mediaPanel = new JPanel();
Player player;
Component visualcom, controlcom;
boolean first = true, loop = false;
String currentDirectory="d:/music";//记录所选媒体文件位置
JInternalFrame min;
public MediaPlayer(JInternalFrame mi){
min=mi;
setLayout(new BorderLayout());
setBackground(Color.lightGray);
setLocale(new java.util.Locale("zh", "CN", ""));
mediaPanel.setToolTipText("视频窗口");
mediaPanel.setVerifyInputWhenFocusTarget(true);
mediaPanel.setLayout(new BorderLayout());
status.setToolTipText("状态栏");
mainMenu.setToolTipText("菜单栏");
fileMenu.add(openMenuItem);
openMenuItem.addActionListener(new OpenFile(this));
fileMenu.add(exitMenuItem);
exitMenuItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
min.dispose ();
player.stop();
return;
}
});
playMenu.add(loopMenuItem);
loopMenuItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
loop = !loop;
}
});
helpMenu.add(aboutMenuItem);
aboutMenuItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
//显示"关于"窗口
About dlg = new About();
Dimension dlgSize = dlg.getPreferredSize();
Dimension frmSize = getSize();
Point loc = getLocation();
dlg.setLocation((frmSize.width - dlgSize.width) / 2 + loc.x,
(frmSize.height - dlgSize.height) / 2 + loc.y);
dlg.setModal(true);
dlg.pack();
dlg.setVisible(true);
}
});
mainMenu.add(fileMenu);
mainMenu.add(playMenu);
mainMenu.add(helpMenu);
status.setBorder(BorderFactory.createLoweredBevelBorder());
status.setOpaque(true);
status.setIcon(new ImageIcon(baseitem.MediaPlayer.class.getResource("logo.png")));
add(mainMenu,BorderLayout.NORTH);
add(mediaPanel, BorderLayout.CENTER);
add(status, BorderLayout.SOUTH);
}
public void controllerUpdate (ControllerEvent e){
// 调用player.close()方法时ControllerClosedEvent事件发生
// 如果存在视觉组件,则该组件件应该拆除(为一致起见,
// 我们对控制面板组件也执行同样的操作)
if(e instanceof ControllerClosedEvent){
if(visualcom != null){
mediaPanel.remove(visualcom);
visualcom = null;
}
if(controlcom != null){
mediaPanel.remove(controlcom);
controlcom = null;
}
return;
}
//如播放结束则EndOfMediaEvent事件发生,检查是否为循环播放
//如是则调用播放器start()方法继续播放
if(e instanceof EndOfMediaEvent){
if(loop){
player.setMediaTime(new Time(0));
player.start();
}
return;
}
if(e instanceof PrefetchCompleteEvent){
player.start();
return;
}
if(e instanceof RealizeCompleteEvent){
visualcom = player.getVisualComponent();
if(visualcom != null)
mediaPanel.add(visualcom, BorderLayout.CENTER);
controlcom = player.getControlPanelComponent();
if(controlcom != null)
mediaPanel.add(controlcom, BorderLayout.SOUTH);
}
min.pack();
}
//用于处理"打开"菜单项的类
class OpenFile implements ActionListener{
MediaPlayer mp;
public OpenFile(MediaPlayer mp){
this.mp=mp;
}
public void actionPerformed(ActionEvent e) {
//构造文件选择组件实例并设置其属性
JFileChooser fd=new JFileChooser();
fd.setSelectedFile(new File(currentDirectory));
fd.setFileSelectionMode(JFileChooser.FILES_ONLY);
int result=fd.showOpenDialog(min);
// 如果用户放弃选择文件,则返回
if(result==JFileChooser.CANCEL_OPTION)
return;
currentDirectory = fd.getSelectedFile().toString();
if (player != null)
player.close();
try {
player = Manager.createPlayer(new MediaLocator("file:"+currentDirectory));
}
catch (java.io.IOException e2) {
System.out.println(e2);
return;
}
catch (NoPlayerException e2) {
System.out.println("不能找到播放器.");
return;
}
if (player == null) {
System.out.println("无法创建播放器.");
return;
}
first = false;
min.setTitle("播放文件位置 " + currentDirectory);
//为播放组件注册监听器
player.addControllerListener (mp);
player.prefetch();
}
}
}
2008-11-22
展开全部
到:
http://www.javazoom.net/javalayer/javalayer.html
下载MP3解码器类库!(jl1.jar)
一个演示:
import java.io.File;
import java.io.FileInputStream;
import javazoom.jl.player.Player;//jl1.jar
public class Mp3Player{
static Thread pt = null;
public static void play(final File af){
if(pt!=null && pt.isAlive())
return;
pt = new Thread(){
public void run(){
try {
new Player(new FileInputStream(af)).play();
} catch (Exception e) {}
}
};
pt.start();
}
public static void play(String fs){
play(new File(fs));
}
public static void play(){
play("d:\\1.mp3");
}
public static void main(String[] args){
play();//播放d:/1.mp3
}
}
http://www.javazoom.net/javalayer/javalayer.html
下载MP3解码器类库!(jl1.jar)
一个演示:
import java.io.File;
import java.io.FileInputStream;
import javazoom.jl.player.Player;//jl1.jar
public class Mp3Player{
static Thread pt = null;
public static void play(final File af){
if(pt!=null && pt.isAlive())
return;
pt = new Thread(){
public void run(){
try {
new Player(new FileInputStream(af)).play();
} catch (Exception e) {}
}
};
pt.start();
}
public static void play(String fs){
play(new File(fs));
}
public static void play(){
play("d:\\1.mp3");
}
public static void main(String[] args){
play();//播放d:/1.mp3
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |