资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

mp3播放器java代码的简单介绍

用java编程 mp3播放器 怎么实现联网下载歌曲的功能呢,什么代码?

1、web.xml文件中增加

创新互联是一家专注于成都网站制作、网站设计与策划设计,洛隆网站建设哪家好?创新互联做网站,专注于网站建设10余年,网设计领域的专业建站公司;建站业务涵盖:洛隆等地区。洛隆做网站价格咨询:18982081108

mime-mapping

extensiondoc/extension

mime-typeapplication/vnd.ms-word/mime-type

/mime-mapping

2、程序如下:

%@page language="java" contentType="application/x-msdownload" import="java.io.*,java.net.*" pageEncoding="gb2312"%

%

//关于文件下载时采用文件流输出的方式处理:

//加上response.reset(),并且所有的%后面不要换行,包括最后一个;

//因为Application Server在处理编译jsp时对于%和%之间的内容一般是原样输出,而且默认是PrintWriter,

//而你却要进行流输出:ServletOutputStream,这样做相当于试图在Servlet中使用两种输出机制,

//就会发生:getOutputStream() has already been called for this response的错误

//详细请见《More Java Pitfill》一书的第二部分 Web层Item 33:试图在Servlet中使用两种输出机制 270

//而且如果有换行,对于文本文件没有什么问题,但是对于其它格式,比如AutoCAD、Word、Excel等文件

//下载下来的文件中就会多出一些换行符0x0d和0x0a,这样可能导致某些格式的文件无法打开,有些也可以正常打开。

response.reset();//可以加也可以不加

response.setContentType("application/x-download");//设置为下载application/x-download

// /../../退WEB-INF/classes两级到应用的根目录下去,注意Tomcat与WebLogic下面这一句得到的路径不同,WebLogic中路径最后没有/

System.out.println(this.getClass().getClassLoader().getResource("/").getPath());

String filenamedownload = this.getClass().getClassLoader().getResource("/").getPath() + "/../../系统解决方案.doc";

String filenamedisplay = "系统解决方案.doc";//系统解决方案.txt

filenamedisplay = URLEncoder.encode(filenamedisplay,"UTF-8");

response.addHeader("Content-Disposition","attachment;filename=" + filenamedisplay);

OutputStream output = null;

FileInputStream fis = null;

try

{

output = response.getOutputStream();

fis = new FileInputStream(filenamedownload);

byte[] b = new byte[1024];

int i = 0;

while((i = fis.read(b)) 0)

{

output.write(b, 0, i);

}

output.flush();

}

catch(Exception e)

{

System.out.println("Error!");

e.printStackTrace();

}

finally

{

if(fis != null)

{

fis.close();

}

java音乐播放器的代码报错,求大神

报错的具体信息是什么

中文的变量名,难于检查,不同的平台又容易出错。

用java编写MP3播放器

作业其实还是自己写的好。要用到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();

}

}

}

java写mp3播放器

--------------不支持MP3---------------------

public AudioClip loadSound(String filename) { // 返回一个AudioClip对象

URL url = null; // 因为newAudioClip()的参数为URL型

try {

url = new URL("file:" + filename); // 指定文件,“file:"不能少

} catch (MalformedURLException e) {

}

return JApplet.newAudioClip(url); // 通过newAudioClip(

// )方法装载声音,此方法为JDK后添加的方法,太老的JDK里可能没有

}

AudioClip s1 = loadSound("声音//TableStopGetPrice.wav");

s1.play();

------------------支持mp3--------------------------

见附件

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.InputStream;

import javazoom.jl.decoder.Bitstream;

import javazoom.jl.decoder.BitstreamException;

import javazoom.jl.decoder.Decoder;

import javazoom.jl.decoder.Header;

import javazoom.jl.decoder.JavaLayerException;

import javazoom.jl.decoder.SampleBuffer;

import javazoom.jl.player.AudioDevice;

import javazoom.jl.player.FactoryRegistry;

/**

* The codePlayer/code class implements a simple player for playback of an

* MPEG audio stream.

* @author Mat McGowan

* @since 0.0.8

*/

public class Player

{

/**

 * The current frame number.

 */

private int frame = 0;

/**

 * The MPEG audio bitstream.

 */

// javac blank final bug.

/* final */private Bitstream bitstream;

/**

 * The MPEG audio decoder.

 */

/* final */private Decoder decoder;

/**

 * The AudioDevice the audio samples are written to.

 */

private AudioDevice audio;

/**

 * Has the player been closed?

 */

private boolean closed = false;

/**

 * Has the player played back all frames from the stream?

 */

private boolean complete = false;

private int lastPosition = 0;

/**

 * Creates a new codePlayer/code instance.

 */

public Player ( InputStream stream ) throws JavaLayerException

{

this (stream, null);

}

public Player ( InputStream stream, AudioDevice device ) throws JavaLayerException

{

bitstream = new Bitstream (stream);

decoder = new Decoder ();

if (device != null)

{

audio = device;

}

else

{

FactoryRegistry r = FactoryRegistry.systemRegistry ();

audio = r.createAudioDevice ();

}

audio.open (decoder);

}

public void play () throws JavaLayerException

{

play (Integer.MAX_VALUE);

}

/**

 * Plays a number of MPEG audio frames.

 * 

 * @param frames

 *            The number of frames to play.

 * @return true if the last frame was played, or false if there are more

 *         frames.

 */

public boolean play ( int frames ) throws JavaLayerException

{

boolean ret = true;

while (frames--  0  ret)

{

ret = decodeFrame ();

}

if (!ret)

{

// last frame, ensure all data flushed to the audio device.

AudioDevice out = audio;

if (out != null)

{

out.flush ();

synchronized (this)

{

complete = ( !closed );

close ();

}

}

}

return ret;

}

/**

 * Cloases this player. Any audio currently playing is stopped immediately.

 */

public synchronized void close ()

{

AudioDevice out = audio;

if (out != null)

{

closed = true;

audio = null;

// this may fail, so ensure object state is set up before

// calling this method.

out.close ();

lastPosition = out.getPosition ();

try

{

bitstream.close ();

}

catch (BitstreamException ex)

{}

}

}

/**

 * Returns the completed status of this player.

 * 

 * @return true if all available MPEG audio frames have been decoded, or

 *         false otherwise.

 */

public synchronized boolean isComplete ()

{

return complete;

}

/**

 * Retrieves the position in milliseconds of the current audio sample being

 * played. This method delegates to the code

 * AudioDevice/code that is used by this player to sound the decoded audio

 * samples.

 */

public int getPosition ()

{

int position = lastPosition;

AudioDevice out = audio;

if (out != null)

{

position = out.getPosition ();

}

return position;

}

/**

 * Decodes a single frame.

 * 

 * @return true if there are no more frames to decode, false otherwise.

 */

protected boolean decodeFrame () throws JavaLayerException

{

try

{

AudioDevice out = audio;

if (out == null)

return false;

Header h = bitstream.readFrame ();

if (h == null)

return false;

// sample buffer set when decoder constructed

SampleBuffer output = (SampleBuffer) decoder.decodeFrame (h, bitstream);

synchronized (this)

{

out = audio;

if (out != null)

{

out.write (output.getBuffer (), 0, output.getBufferLength ());

}

}

bitstream.closeFrame ();

}

catch (RuntimeException ex)

{

throw new JavaLayerException ("Exception decoding audio frame", ex);

}

/*

 * catch (IOException ex) {

 * System.out.println("exception decoding audio frame: "+ex); return

 * false; } catch (BitstreamException bitex) {

 * System.out.println("exception decoding audio frame: "+bitex); return

 * false; } catch (DecoderException decex) {

 * System.out.println("exception decoding audio frame: "+decex); return

 * false; }

 */

return true;

}

public static void main ( String[] args )

{

try

{

Player player = new Player (new FileInputStream (new File ("D:\\Youdagames\\JLayer1.0.1\\abc.mp3")));

player.play ();

}

catch (FileNotFoundException e)

{

e.printStackTrace ();

}

catch (JavaLayerException e)

{

e.printStackTrace ();

}

}

}

求用java编写MP3播放器

这个需要jmf的相关包,去网上下载下,给你写了个例子

public class TestPlay extends JFrame {

private Component vc, cc;

private JButton file = new JButton("file");

private Player player = null;

public TestPlay() throws HeadlessException, NoPlayerException, MalformedURLException, IOException {

this.add(file);

file.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent arg0) {

JFileChooser f = new JFileChooser();

if(f.showOpenDialog(null)==JFileChooser.CANCEL_OPTION)return;

try {

player = Manager.createPlayer(f.getSelectedFile().toURL());

player.addControllerListener(new ControllerListener() {

public void controllerUpdate(ControllerEvent arg0) {

controllerUpdateImp(arg0);

}

});

player.prefetch();

} catch (Exception e) {

System.out.println(e);

JOptionPane.showMessageDialog(null, e.getMessage());

}

}

});

}

public void controllerUpdateImp(ControllerEvent e) {

if (e instanceof EndOfMediaEvent) {

player.setMediaTime(new Time(0));

player.start();

} else if (e instanceof PrefetchCompleteEvent) {

player.start();

} else if (e instanceof RealizeCompleteEvent) {

vc = player.getVisualComponent();

if (vc != null) this.add(vc, BorderLayout.CENTER);

cc = player.getControlPanelComponent();

if (cc != null) this.add(cc, BorderLayout.SOUTH);

this.pack();

this.setResizable(false);

this.setVisible(true);

} else if (e instanceof ControllerClosedEvent) {

System.exit(0);

}

}

public static void main(String[] args) {

TestPlay t;

try {

t = new TestPlay();

t.setDefaultCloseOperation(t.EXIT_ON_CLOSE);

t.pack();

t.setLocationRelativeTo(null);

t.setVisible(true);

} catch (Exception e) {

JOptionPane.showMessageDialog(null, e.getMessage());

}

}

}

java编写 mp3播放器 代码

// 你看看吧。。 必须下载 jmf包 如果不知道下载就问我吧

import java.awt.*;

import java.awt.event.*;

import java.applet.*;

import javax.media.bean.playerbean.MediaPlayer; //必须下载 jmf 媒体播放包

public class player extends Applet implements ActionListener {

Button b1, b2;

MediaPlayer player;

public void init() {

player = new MediaPlayer();

setLayout(new FlowLayout());

try{

player.setMediaLocation("file:/F:\\音乐\\mp3\\黑白配.mp3");// file:/不能删除 音频文件路径

} catch (Exception e) {

System.out.println("文件不存在");

}

b1 = new Button("播放");

b2 = new Button("停止");

add(b1);

add(b2);

b1.addActionListener(this);

b2.addActionListener(this);

setSize(200, 200);

setVisible(true);

}

public void actionPerformed(ActionEvent e) {

if (e.getSource() == b1) {

player.start();

} else if (e.getSource() == b2) {

player.stop();

System.out.println(player.getMediaTime().getSeconds());

}

}

}


分享文章:mp3播放器java代码的简单介绍
转载源于:http://cdkjz.cn/article/phiegg.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220