首页>代码>java swing开发的网易云音乐试听下载、播放>/zuidaima.cnm.163music/src/cnm/Music163DownloadOne.java
package cnm;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JTextArea;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;

import javax.swing.JScrollPane;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;

import javax.swing.SwingConstants;

import cnm.music163.Mp3Info;
import cnm.music163.Mp3Player;
import cnm.music163.Musicer;
import cnm.music163.Util;
import javax.swing.JProgressBar;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.event.ChangeListener;
import javax.swing.event.ChangeEvent;

public class Music163DownloadOne {

	private JFrame frame;
	private JTextField txtHttpmusiccomsongiduserid;
	private JTextArea textArea;
	private JTextField txtHttpwwwzuidaimacomusersharehtm;
	private Musicer m;
	private String MusicSavePath = "mp3";
	private JProgressBar progressBar;
	private JCheckBox chckbxNewCheckBox;
	private JButton btnPlay;
	private JButton btnStop;
	private Mp3Player p=new Mp3Player("");
	private JCheckBox chckbxNewCheckBox_1;
	private JTextArea textArea_1;
	private JButton button;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Music163DownloadOne window = new Music163DownloadOne();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	private void getM() {
		m = new Musicer();
	}

	/**
	 * Create the application.
	 */
	public Music163DownloadOne() {
		initialize();
	}

	private void gogo() {
		// 每次获取一个新m
		// 当发生下载失败时,不会受上一个m的影响
		getM();
		// 初始化进度条
		progressBar.setValue(0);
		btnPlay.setEnabled(false);
		btnStop.setEnabled(false);
		//-------
		String start = "id=";
		String end = "&";
		String musicID = "";
		String str = txtHttpmusiccomsongiduserid.getText();
		textArea.append("\n开始任务[" + str + "]");
		int s = str.indexOf(start);
		if (s >= 0) {
			int e = str.indexOf(end);
			s = s + start.length();
			if (e >= 0) {
				musicID = str.substring(s, e);
			} else {
				musicID = str.substring(s);
			}
		}
		textArea.append("\n获取ID[" + musicID + "]");
		m.setId(musicID);
		String downFile = Util.downloadFileEx(m.getDownUrl(), MusicSavePath, progressBar);
		m.setDownPath(downFile);
		m.setPath(downFile);
		if ("".equals(m.getDownPath())) {
			textArea.append("__[下载失败],可能是网络错误,或者为VIP收费音乐");
			return;
		} else {
			textArea.append("__[下载成功]" + m.getDownPath());
			btnPlay.setEnabled(true);
		}
		textArea.append("\n开始重命名");
		boolean isSetInfo = Mp3Info.setInfo4html(m);
		if (isSetInfo) {
			textArea.append(",[获取到 标题:" + m.getName() + " 作者:" + m.getSinger() + "]");
			if (Util.rename(m, MusicSavePath)) {
				textArea.append("\n_[重命名成功]-->" + m.getPath());
			} else {
				textArea.append("\n_[重命名失败]");
			}
		} else {
			textArea.append("--获取标题失败,放弃重命名");
		}
		textArea.append("\n[----------[任务结束]----------\n");

	}

	private void selectInput() {
		// 选中输入框 清空内容
		txtHttpmusiccomsongiduserid.setText("");
		// 选中输入框并且勾选
		// 将从粘贴板获取内容 粗略判断格式
		if (chckbxNewCheckBox.isEnabled()) {
			//获取粘贴板
			Clipboard clip=Toolkit.getDefaultToolkit().getSystemClipboard();
			Transferable cliptf= clip.getContents(null);
			if(cliptf!=null) {
				//是否为文本
				if(cliptf.isDataFlavorSupported(DataFlavor.stringFlavor)) {
					String t="";
					try {
						t = (String)cliptf.getTransferData(DataFlavor.stringFlavor);
					} catch (UnsupportedFlavorException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					} catch (IOException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
					txtHttpmusiccomsongiduserid.setText(t);
				}
			}
			
			String start = "id=";
			String end = "&";
			String str = txtHttpmusiccomsongiduserid.getText();
			int s = str.indexOf(start);
			int e = str.indexOf(end);
			if (s >= 0 ) {
				if(e>=s||e==-1) {
					textArea.append("\n[从粘贴板获取到任务]"+str);
					gogo();
				}
			}

		}

	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setResizable(false);
		frame.setBounds(100, 100, 710, 400);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(null);

		txtHttpmusiccomsongiduserid = new JTextField();
		txtHttpmusiccomsongiduserid.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				selectInput();
			}
		});
		txtHttpmusiccomsongiduserid.setText("http://music.163.com/song?id=1351551838&userid=123456");
		txtHttpmusiccomsongiduserid.setBounds(10, 7, 430, 21);
		frame.getContentPane().add(txtHttpmusiccomsongiduserid);
		txtHttpmusiccomsongiduserid.setColumns(10);

		JButton btnGoGo = new JButton("Download");
		btnGoGo.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				gogo();
			}
		});
		btnGoGo.setBounds(580, 6, 104, 23);
		frame.getContentPane().add(btnGoGo);
		
		ImageIcon img = new ImageIcon(this.getClass().getResource("qr.gif"));

		textArea = new JTextArea() {
			@Override
			public void append(String str) {
				// TODO Auto-generated method stub
				super.append(str);
				// 实时刷新
				this.paintImmediately(getBounds());
			}
			{
				// 透明
				setOpaque(false);
			}

			protected void paintComponent(Graphics g) {
				g.drawImage(img.getImage(), 8, 45, this);
				super.paintComponent(g);
			}
		};
		textArea.setBounds(10, 35, 564, 285);
		// 自动换行
		textArea.setLineWrap(true);
		// 是否能编辑
		// textArea.setEditable(false);
		textArea.setText("使用说明:在上方输入框填入分享的连接。必须含有关键字[id=],截取内容为[id=]*[&] or [id=]*");
		textArea.append("\n[声明]此源码仅用作技术研究,请勿做它用!");
		textArea.append("\n\n\n\n\n\n\n\n\n\n\n\n\n");
		frame.getContentPane().add(textArea);

		// 放入滚动条
		JScrollPane scrollPane = new JScrollPane(textArea);
		scrollPane.setBounds(10, 35, 564, 285);
		frame.getContentPane().add(scrollPane);

		txtHttpwwwzuidaimacomusersharehtm = new JTextField();
		txtHttpwwwzuidaimacomusersharehtm.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				try {
					Runtime.getRuntime()
							.exec("cmd.exe /c start http://www.zuidaima.com/user/1636574934059008/share.htm");
				} catch (IOException e1) {
					e1.printStackTrace();
				}
			}
		});
		txtHttpwwwzuidaimacomusersharehtm.setHorizontalAlignment(SwingConstants.CENTER);
		txtHttpwwwzuidaimacomusersharehtm.setFont(new Font("宋体", Font.BOLD, 14));
		txtHttpwwwzuidaimacomusersharehtm.setForeground(new Color(30, 144, 255));
		txtHttpwwwzuidaimacomusersharehtm.setText("[\u8FD9\u91CC\u53EF\u4EE5\u70B9\u5F00\u6D4F\u89C8\u5668] ->\u6B64\u8F6F\u4EF6\u6765\u81EA\u8FD9\u91CC<- \u5934\u50CF\u4E5F\u80FD\u6253\u8D4F");
		txtHttpwwwzuidaimacomusersharehtm.setEditable(false);
		txtHttpwwwzuidaimacomusersharehtm.setBounds(10, 330, 564, 21);
		frame.getContentPane().add(txtHttpwwwzuidaimacomusersharehtm);
		txtHttpwwwzuidaimacomusersharehtm.setColumns(10);

		progressBar = new JProgressBar();
		progressBar.setMaximum(100);
		progressBar.setBounds(10, 320, 564, 10);
		frame.getContentPane().add(progressBar);

		chckbxNewCheckBox = new JCheckBox("\u7C98\u8D34\u677F &\u81EA\u52A8\u4E0B\u8F7D");
		chckbxNewCheckBox.setSelected(true);
		chckbxNewCheckBox.setBounds(446, 6, 128, 23);
		frame.getContentPane().add(chckbxNewCheckBox);
		
		btnPlay = new JButton("Play");
		btnPlay.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				/*
				 * TODO BUG:前一首未播放完,或者没停止,再播放,会有两个播放器
				 * */
				btnStop.setEnabled(true);
				p.setFilename(m.getPath());
				p.play();
			}
		});
		btnPlay.setEnabled(false);
		btnPlay.setBounds(580, 39, 104, 23);
		frame.getContentPane().add(btnPlay);
		
		btnStop = new JButton("Stop");
		btnStop.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				p.stop();
				btnPlay.setEnabled(true);
			}
		});
		btnStop.setEnabled(false);
		btnStop.setBounds(580, 72, 104, 23);
		frame.getContentPane().add(btnStop);
		chckbxNewCheckBox_1 = new JCheckBox("\u9690\u85CF\u4E8C\u7EF4\u7801");
		chckbxNewCheckBox_1.setFont(new Font("宋体", Font.PLAIN, 14));
		chckbxNewCheckBox_1.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				if(chckbxNewCheckBox_1.isSelected()) {
					textArea.setOpaque(true);
					textArea.append(" ");
				}else {
					textArea.setOpaque(false);
					textArea.append(" ");
				}
			}
		});
	
		chckbxNewCheckBox_1.setBounds(580, 115, 103, 23);
		frame.getContentPane().add(chckbxNewCheckBox_1);
		
		textArea_1 = new JTextArea();
		textArea_1.setFont(new Font("Monospaced", Font.PLAIN, 16));
		textArea_1.setLineWrap(true);
		textArea_1.setEditable(false);
		textArea_1.setOpaque(false);
		textArea_1.setText("求打赏\n一分不嫌少\n一块也不嫌多\n谢谢各位老板");
		textArea_1.setBounds(581, 149, 104, 114);
		frame.getContentPane().add(textArea_1);
		
		button = new JButton("<-Clear");
		button.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				textArea.setText("");
			}
		});
		button.setHorizontalAlignment(SwingConstants.LEFT);
		button.setBounds(580, 264, 93, 56);
		frame.getContentPane().add(button);
	}
}
最近下载更多
云翳tel青  LV11 2022年3月1日
时光凉薄  LV2 2021年7月9日
满脑子痛苦  LV3 2021年1月10日
pxlpxlpxl  LV4 2020年12月20日
MotleyHighness  LV1 2020年12月1日
与于与  LV2 2020年11月17日
刘晓静  LV5 2020年6月14日
Macjie  LV1 2020年4月24日
w5650100  LV3 2019年10月26日
hw272619  LV6 2019年9月13日
最近浏览更多
是夜在熬我.  LV6 2月19日
cxz2132132  LV11 2023年12月9日
pangzhihui  LV12 2023年11月13日
oldfox  LV18 2023年11月13日
EFWAGGFAWGR 2023年10月19日
暂无贡献等级
lzl111213  LV1 2023年6月21日
runbey6666  LV2 2023年6月15日
空中飞尘  LV13 2023年6月13日
wawayv  LV2 2022年12月13日
过桥次米线  LV2 2022年12月11日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友