mp3播放器java代碼的簡單介紹

用java編程 mp3播放器 怎么實現(xiàn)聯(lián)網(wǎng)下載歌曲的功能呢,什么代碼?

1、web.xml文件中增加

創(chuàng)新互聯(lián)是一家專注于成都網(wǎng)站制作、網(wǎng)站設(shè)計與策劃設(shè)計,洛隆網(wǎng)站建設(shè)哪家好?創(chuàng)新互聯(lián)做網(wǎng)站,專注于網(wǎng)站建設(shè)10余年,網(wǎng)設(shè)計領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:洛隆等地區(qū)。洛隆做網(wǎng)站價格咨詢: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"%

%

//關(guān)于文件下載時采用文件流輸出的方式處理:

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

//因為Application Server在處理編譯jsp時對于%和%之間的內(nèi)容一般是原樣輸出,而且默認是PrintWriter,

//而你卻要進行流輸出:ServletOutputStream,這樣做相當于試圖在Servlet中使用兩種輸出機制,

//就會發(fā)生:getOutputStream() has already been called for this response的錯誤

//詳細請見《More Java Pitfill》一書的第二部分 Web層Item 33:試圖在Servlet中使用兩種輸出機制 270

//而且如果有換行,對于文本文件沒有什么問題,但是對于其它格式,比如AutoCAD、Word、Excel等文件

//下載下來的文件中就會多出一些換行符0x0d和0x0a,這樣可能導(dǎo)致某些格式的文件無法打開,有些也可以正常打開。

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

response.setContentType("application/x-download");//設(shè)置為下載application/x-download

// /../../退WEB-INF/classes兩級到應(yīng)用的根目錄下去,注意Tomcat與WebLogic下面這一句得到的路徑不同,WebLogic中路徑最后沒有/

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

String filenamedownload = this.getClass().getClassLoader().getResource("/").getPath() + "/../../系統(tǒng)解決方案.doc";

String filenamedisplay = "系統(tǒng)解決方案.doc";//系統(tǒng)解決方案.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播放器

作業(yè)其實還是自己寫的好。要用到JMF包啊,到網(wǎng)上下載一個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()的參數(shù)為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的相關(guān)包,去網(wǎng)上下載下,給你寫了個例子

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://muchs.cn/article16/phiegg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供搜索引擎優(yōu)化、網(wǎng)站排名、微信公眾號、網(wǎng)站導(dǎo)航、網(wǎng)站建設(shè)、網(wǎng)站設(shè)計公司

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設(shè)