真心求教java怎么播放url地址的MP3.

Chris__Chen_ 2012-03-05 01:32:25
修改了网上的代码,可以播放本地的mp3,但是能输入url地址但不知道怎么播放它



import java.io.*;
import java.util.Iterator;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.sound.sampled.*;
import java.net.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javazoom.jl.player.Player;

public class MusicPlayer extends JFrame {

boolean isStop = true;// 控制播放线程
boolean hasStop = true;// 播放线程状态

String filepath;// 播放文件目录
String filename;// 播放文件名称
String urlStr;
AudioInputStream audioInputStream;// 音频文件流
AudioFormat audioFormat;// 音频文件格式
SourceDataLine sourceDataLine;// 接口:源数据行,可以写入音频数据

List list;// 文件列表
JLabel labelfilepath;// 播放目录显示标签
JLabel labelURL;
JLabel labelfilename;// 播放文件显示标签
TextArea textArea;
JMenuBar menubar;
JMenu menufile;
JMenuItem opensong;
JMenuItem openURL;
JPanel panelmain ;
Container container;
JPanel paneright;
JTextField textField;
Container container2;
URL url;

static int testtimethread;
public MusicPlayer() {
// Framework
this.setLayout(new BorderLayout());
this.setTitle("Player");
this.setSize(400, 300);

// Menu
menubar = new JMenuBar();
menufile = new JMenu("File");
opensong = new JMenuItem("Open file");
openURL = new JMenuItem("Open URL");

menufile.add(opensong);
menufile.add(openURL);
menubar.add(menufile);

this.setVisible(true);
this.setJMenuBar(menubar);

opensong.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {
openFile();

}
});
openURL.addActionListener(new ActionListener() { //打开URL按钮事件处理
public void actionPerformed(ActionEvent e) {
String addressName =JOptionPane.showInputDialog(MusicPlayer.this, "URL address");
if (addressName != null)
{
openURL(addressName);
}
}
});
//
list = new List();
list.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
// mouse
if (e.getClickCount() == 2) {
// double kick
filename = list.getSelectedItem();
testtimethread++;
if (filename!=null){
play();
}
else{
playURL pla = new playURL(urlStr);

}
}
}
});

//
panelmain = new JPanel(new GridLayout(2, 1));
container = new Container();
container.setLayout(new GridLayout(1, 1));
this.add(panelmain, BorderLayout.NORTH);
this.add(container, BorderLayout.CENTER);
labelfilepath = new JLabel("file path:");
labelURL = new JLabel("URL");
labelfilename = new JLabel("name:");
panelmain.add(labelfilepath);
panelmain.add(labelURL);
panelmain.add(labelfilename);
textField = new JTextField();
textArea = new TextArea();
textArea.enableInputMethods(false);


container.add(list);

// close
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
this.setVisible(true);
}


private void openFile() {
FileDialog dialog1 = new FileDialog(this, "chose music", FileDialog.LOAD);
dialog1.setVisible(true);

filepath = dialog1.getDirectory();
String filename=dialog1.getFile();
if(!(filename.endsWith("mp3")||filename.endsWith("wav"))){
JOptionPane.showMessageDialog(null, "mp3 or wav");
System.exit(0);
}
if (filepath != null) {
labelfilepath.setText("Play list" + filepath);
list.add(filename);
}
}
private void openURL(String str){
urlStr = str;
try {
url = new URL(str);
} catch (Exception ex) {

}
if(!(str.endsWith("mp3")||str.endsWith("wav"))){
JOptionPane.showMessageDialog(null, "mp3 or wav");
System.exit(0);
}
labelURL.setText("Play list" + str);
list.add(str);
}

private void play() {
try {
isStop = true;// 停止播放线程
// 等待播放线程停止
while (!hasStop) {
//System.out.print(".");
try {
Thread.sleep(10);
} catch (Exception e) {

}
}
File file = new File(filepath + filename);
labelfilename.setText("file" + filename);




// 取得文件输入流
audioInputStream = AudioSystem.getAudioInputStream(file);
audioFormat = audioInputStream.getFormat();

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();//允许源数据行操作(I/O)该数据行

// 创建独立线程进行播放
isStop = false;
Thread playThread = new Thread(new PlayThread());
playThread.start();
} catch (Exception e) {
e.printStackTrace();//输出到标准错误流
}
}

public static void main(String args[]) {
new MusicPlayer();
}

// 播放线程
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) {// ending test
if (isStop)
break;
if (cnt > 0) {
// 将缓存的音频数据写入混频器

sourceDataLine.write(tempBuffer, 0, cnt);

labelfilename.setText("song:" + filename);
}
}
// Block等待临时数据被输出为空
sourceDataLine.drain();
sourceDataLine.close();// 关闭行
hasStop = true;
} catch (Exception e) {

System.exit(0);
}
}
}
}
...全文
436 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
qqhw123 2012-03-05
  • 打赏
  • 举报
回复
你这个应该只能播放本地的mp3文件。
如果是网上资源的话,可以试试ls说的HttpClient去获取网络流数据。
MiceRice 2012-03-05
  • 打赏
  • 举报
回复
如果输入的是URL地址,你用File是不能处理的。

建议可以考虑用HttpClient用GET方式去访问那个URL,然后获取输入流,然后将其传给你的AudioSystem。

建议把访问文件 和 播放,分成两个函数,便于你进行优化改造。
terry21 2012-03-05
  • 打赏
  • 举报
回复
给个示例代码

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod;

/**
* 用HttpClient下载图片
* @author wei
*/
public class TestDownImage {

public static void main(String[] args) throws IOException{
HttpClient client = new HttpClient();
GetMethod get = new GetMethod("http://images.sohu.com/uiue/sohu_logo/beijing2008/2008sohu.gif");
client.executeMethod(get);
File storeFile = new File("c:/2008sohu.gif");
FileOutputStream output = new FileOutputStream(storeFile);
//得到网络资源的字节数组,并写入文件
output.write(get.getResponseBody());
output.close();
}
}
terry21 2012-03-05
  • 打赏
  • 举报
回复
httpclient正解。

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧