符合中小企业对网站设计、功能常规化式的企业展示型网站建设
本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...
商城网站建设因基本功能的需求不同费用上面也有很大的差别...
手机微信网站开发、微信官网、微信商城网站...
java中打开音乐播放器的方式是使用audioclip类来播放音乐,实例如下:
创新互联建站自2013年起,是专业互联网技术服务公司,拥有项目成都网站建设、成都网站制作网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元靖安做网站,已为上家服务,为靖安各地企业和个人服务,联系电话:028-86922220
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import javax.swing.*;
import java.io.File;
class AudioPlayDemo extends JFrame implements ActionListener {
boolean looping = false;
File file1 = new File("music\\明天会更好.wav");
AudioClip sound1;
AudioClip chosenClip;
JButton playButton = new JButton("播放");
JButton loopButton = new JButton("循环播放");
JButton stopButton = new JButton("停止");
JLabel status = new JLabel("选择播放文件");
JPanel controlPanel = new JPanel();
Container container = getContentPane();
public AudioPlayDemo() {
try {
sound1 = Applet.newAudioClip(file1.toURL());
chosenClip = sound1;
} catch(OutOfMemoryError e){
System.out.println("内存溢出");
e.printStackTrace();
} catch(Exception e){
e.printStackTrace();
}
playButton.addActionListener(this);
loopButton.addActionListener(this);
stopButton.addActionListener(this);
stopButton.setEnabled(false);
controlPanel.add(playButton);
controlPanel.add(loopButton);
controlPanel.add(stopButton);
container.add(controlPanel, BorderLayout.CENTER);
container.add(status, BorderLayout.SOUTH);
setSize(300, 130);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //关闭窗口时退出程序
}
public void actionPerformed(ActionEvent event) {
if (chosenClip == null) {
status.setText("声音未载入");
return;
}
Object source = event.getSource(); //获取用户洗涤激活的按钮
if (source == playButton) {
stopButton.setEnabled(true);
loopButton.setEnabled(true);
chosenClip.play();
status.setText("正在播放");
}
if (source == loopButton) {
looping = true;
chosenClip.loop();
loopButton.setEnabled(false);
stopButton.setEnabled(true);
status.setText("正在循环播放");
}
if (source == stopButton) {
if (looping) {
looping = false;
chosenClip.stop();
loopButton.setEnabled(true);
} else {
chosenClip.stop();
}
stopButton.setEnabled(false);
status.setText("停止播放");
}
}
public static void main(String s[]) {
new AudioPlayDemo();
}
}
只能播放wav格式的歌曲
简单的实例,代码如下,纯粹JMF加载MP3并播放:
import javax.media.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class PlayerMusic implements ControllerListener {// ControllerListener
// 控制事件
private Player player;
private boolean first, loop;
private String path;
private List mp3List;
private int mp3NO = 0;
PlayerMusic(List mp3List) {
this.mp3List = mp3List;
}
public void start() {
try {
player = Manager.createPlayer(new MediaLocator("file://" + mp3List.get(mp3NO)));
} catch (NoPlayerException ex) {
ex.printStackTrace();
System.out.println("不能播放文件");
return;
} catch (IOException ex) {
ex.printStackTrace();
return;
}
if (player == null) {
System.out.println("播放器为空");
return;
}
first = false;
player.addControllerListener(this);
// 提取媒体内容
player.prefetch();
}
public void controllerUpdate(ControllerEvent e) {
// 当媒体播放结束时,循环播放
if (e instanceof EndOfMediaEvent) {
mp3NO++;
if(mp3NOthis.mp3List.size()){
this.start();
}
return;
}
// 当预提取媒体的内容结束
if (e instanceof PrefetchCompleteEvent) {
player.start();
return;
}
// 当实例化后
if (e instanceof RealizeCompleteEvent) {
// pack(); //执行pack()操作
return;
}
}
public static void main(String[] args) {
List mp3List = new ArrayList();
mp3List.add("d://a.mp3");
mp3List.add("d://b.mp3");
mp3List.add("d://c.mp3");
PlayerMusic pm = new PlayerMusic(mp3List);
pm.start();
}
}
首先下载播放mp3的包,比如mp3spi1.9.4.jar。在工程中添加这个包。
播放器演示代码如下
package com.test.audio;
import java.io.File;
import java.awt.BorderLayout;
import java.awt.FileDialog;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.List;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.MenuItem;
import java.awt.MenuShortcut;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.SourceDataLine;
public class MusicPlayer extends Frame {
/**
*
*/
private static final long serialVersionUID = -2605658046194599045L;
boolean isStop = true;// 控制播放线程
boolean hasStop = true;// 播放线程状态
String filepath;// 播放文件目录
String filename;// 播放文件名称
AudioInputStream audioInputStream;// 文件流
AudioFormat audioFormat;// 文件格式
SourceDataLine sourceDataLine;// 输出设备
List list;// 文件列表
Label labelfilepath;//播放目录显示标签
Label labelfilename;//播放文件显示标签
public MusicPlayer() {
// 设置窗体属性
setLayout(new BorderLayout());
setTitle("MP3 Music Player");
setSize(350, 370);
// 建立菜单栏
MenuBar menubar = new MenuBar();
Menu menufile = new Menu("File");
MenuItem menuopen = new MenuItem("Open", new MenuShortcut(KeyEvent.VK_O));
menufile.add(menuopen);
menufile.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
open();
}
});
menubar.add(menufile);
setMenuBar(menubar);
// 文件列表
list = new List(10);
list.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
// 双击时处理
if (e.getClickCount() == 2) {
// 播放选中的文件
filename = list.getSelectedItem();
play();
}
}
});
add(list, "Center");
// 信息显示
Panel panel = new Panel(new GridLayout(2, 1));
labelfilepath = new Label("Dir:");
labelfilename = new Label("File:");
panel.add(labelfilepath);
panel.add(labelfilename);
add(panel, "North");
// 注册窗体关闭事件
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
setVisible(true);
}
// 打开
private void open() {
FileDialog dialog = new FileDialog(this, "Open", 0);
dialog.setVisible(true);
filepath = dialog.getDirectory();
if (filepath != null) {
labelfilepath.setText("Dir:" + filepath);
// 显示文件列表
list.removeAll();
File filedir = new File(filepath);
File[] filelist = filedir.listFiles();
for (File file : filelist) {
String filename = file.getName().toLowerCase();
if (filename.endsWith(".mp3") || filename.endsWith(".wav")) {
list.add(filename);
}
}
}
}
// 播放
private void play() {
try {
isStop = true;// 停止播放线程
// 等待播放线程停止
System.out.print("Start:" + filename);
while (!hasStop) {
System.out.print(".");
try {
Thread.sleep(10);
} catch (Exception e) {
}
}
System.out.println("");
File file = new File(filepath + filename);
labelfilename.setText("Playing:" + filename);
// 取得文件输入流
audioInputStream = AudioSystem.getAudioInputStream(file);
audioFormat = audioInputStream.getFormat();
// 转换mp3文件编码
if (audioFormat.getEncoding() != AudioFormat.Encoding.PCM_SIGNED) {
audioFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,
audioFormat.getSampleRate(), 16, audioFormat
.getChannels(), audioFormat.getChannels() * 2,
audioFormat.getSampleRate(), false);
audioInputStream = AudioSystem.getAudioInputStream(audioFormat,
audioInputStream);
}
// 打开输出设备
DataLine.Info dataLineInfo = new DataLine.Info(
SourceDataLine.class, audioFormat,
AudioSystem.NOT_SPECIFIED);
sourceDataLine = (SourceDataLine) AudioSystem.getLine(dataLineInfo);
sourceDataLine.open(audioFormat);
sourceDataLine.start();
// 创建独立线程进行播放
isStop = false;
Thread playThread = new Thread(new PlayThread());
playThread.start();
} catch (Exception e) {
e.printStackTrace();
}
}
class PlayThread extends Thread {
byte tempBuffer[] = new byte[320];
public void run() {
try {
int cnt;
hasStop = false;
// 读取数据到缓存数据
while ((cnt = audioInputStream.read(tempBuffer, 0,
tempBuffer.length)) != -1) {
if (isStop)
break;
if (cnt 0) {
// 写入缓存数据
sourceDataLine.write(tempBuffer, 0, cnt);
}
}
// Block等待临时数据被输出为空
sourceDataLine.drain();
sourceDataLine.close();
hasStop = true;
} catch (Exception e) {
e.printStackTrace();
System.exit(0);
}
}
}
public static void main(String args[]) {
new MusicPlayer();
}
}
作业其实还是自己写的好。要用到JMF包啊,到网上下载一个JMF包,照着说明安装上。
以下是我写的一个很简单的播放器,只能播放mp3,mpeg,mpg,wav等简单的格式。
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.media.ControllerEvent;
import javax.media.ControllerListener;
import javax.media.MediaLocator;
import javax.media.RealizeCompleteEvent;
import javax.media.bean.playerbean.MediaPlayer;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.JList;
import java.awt.BorderLayout;
import javax.swing.JSplitPane;
import java.awt.Component;
import java.util.Vector;
public class JMF_T extends JFrame implements ControllerListener, ActionListener {
MediaPlayer Player;
private String filename = "";
private static final long serialVersionUID = 1L;
private Vector vct = new Vector(); // @jve:decl-index=0:
private JPanel jContentPane = null;
private JSplitPane jSplitPane = null;
private JPanel playPanel = null;
private JList jList = null;
private JSplitPane getJSplitPane() {
if (jSplitPane == null) {
jSplitPane = new JSplitPane();
jSplitPane.setDividerSize(5);
jSplitPane.setResizeWeight(0.8);
jSplitPane.setRightComponent(getJList());
jSplitPane.setLeftComponent(getPlayPanel());
}
return jSplitPane;
}
private JPanel getPlayPanel() {
if (playPanel == null) {
playPanel = new JPanel();
playPanel.setLayout(new BorderLayout());
}
return playPanel;
}
private JList getJList() {
if (jList == null) {
jList = new JList();
jList.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent e) {
// TODO Auto-generated Event stub mouseClicked()
if (e.getClickCount() == 1) {
String str = (String) jList.getSelectedValue();
if (str == null) {
return;
}
filename = str;
System.out.println(str);
}
if (e.getClickCount() == 2) {
String str = (String) jList.getSelectedValue();
if (str == null) {
return;
}
filename = str;
play();
}
}
});
}
return jList;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JMF_T thisClass = new JMF_T();
thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
thisClass.setVisible(true);
}
});
}
public JMF_T() {
initialize();
}
private void OpenFile() {
FileDialog fd = new FileDialog(this, "Choose Video", FileDialog.LOAD);
fd.setVisible(true);
filename = fd.getDirectory() + fd.getFile();
System.out.println(filename);
if (filename.equals("")) {
return;
} else if (filename.equals("nullnull")) {
return;
}
boolean j = false;
for (int i = 0; i vct.size(); i++) {
if (vct.get(i).toString().equals(filename)) {
j = true;
break;
}
}
if (j == false) {
vct.add(filename);
jList.setListData(vct);
}
}
private void stop() {
if (Player != null) {
Player.stop();
Player.deallocate();
}
}
private void play() {
try {
if (filename.equals("")) {
return;
}
if (Player == null) {
Player = new MediaPlayer();
} else {
closePreviosPlayer();
}
Player.setMediaLocator(new MediaLocator("" + filename));
Player.addControllerListener(this);
Player.realize();
Player.start();
} catch (Exception e) {
}
}
public void actionPerformed(ActionEvent e) {
String action = e.getActionCommand().toString();
if (action.equals("Open")) {
OpenFile();
}
if (action.equals("Play")) {
play();
}
if (action.equals("Stop")) {
stop();
}
if (action.equals("Exit")) {
dispose();
System.exit(0);
}
}
private void initialize() {
this.setSize(500, 350);
setLocation(300, 100);
this.setContentPane(getJContentPane());
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
dispose();
System.exit(0);
}
});
MenuBar mb = new MenuBar();
setMenuBar(mb);
Menu fileMenu = new Menu("File");
Menu actMenu = new Menu("Action");
mb.add(fileMenu);
mb.add(actMenu);
MenuItem itemOpen = new MenuItem("Open");
itemOpen.addActionListener(this);
fileMenu.add(itemOpen);
fileMenu.addSeparator();
MenuItem itemPlay = new MenuItem("Play");
itemPlay.addActionListener(this);
actMenu.add(itemPlay);
actMenu.addSeparator();
MenuItem itemStop = new MenuItem("Stop");
itemStop.addActionListener(this);
actMenu.add(itemStop);
MenuItem itemExit = new MenuItem("Exit");
itemExit.addActionListener(this);
fileMenu.add(itemExit);
this.validate();
this.setVisible(true);
}
private JPanel getJContentPane() {
if (jContentPane == null) {
jContentPane = new JPanel();
jContentPane.setLayout(new BorderLayout());
jContentPane.add(getJSplitPane(), BorderLayout.CENTER);
}
return jContentPane;
}
private void closePreviosPlayer() {
if (Player == null)
return;
Player.stop();
Player.deallocate(); // 停止播放并且重新装载DateSource
Component visual = Player.getVisualComponent();
Component control = Player.getControlPanelComponent();
if (visual != null) {
playPanel.remove(visual);
}
if (control != null) {
playPanel.remove(control);
}
}
public synchronized void controllerUpdate(ControllerEvent event) {
if (event instanceof RealizeCompleteEvent) {
Component comp;
if ((comp = Player.getControlPanelComponent()) != null) {
playPanel.add("South", comp);
} else {
closePreviosPlayer();
}
if ((comp = Player.getVisualComponent()) != null) {
playPanel.add("Center", comp);
}
validate();
}
}
}