📄 songinfo.java
字号:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.hadeslee.music;
/**
* 一个歌曲信息的类的结构表示
* 这个歌曲是使用ID3V1的信息存储结构的
* @author hadeslee
*/
public class SongInfo {
private final String TAG = "TAG"; //文件头1-3
private String songName; //歌曲名4-33
private String artist; //歌手名34-63
private String album; //专辑名61-93
private String year; //年94-97
private String comment; //备注98-125
private byte r1, r2, r3; //三个保留位126,127,128
private boolean valid; //是否合法
public transient String fileName; //此歌曲对应的文件名,没有封装
public SongInfo(byte[] data) {
if (data.length != 128) {
throw new RuntimeException("数据长度不合法:" + data.length);
}
String tag = new String(data, 0, 3);
//只有前三个字节是TAG才处理后面的字节
if (tag.equalsIgnoreCase("TAG")) {
valid = true;
songName = new String(data, 3, 30).trim();
artist = new String(data, 33, 30).trim();
album = new String(data, 63, 30).trim();
year = new String(data, 93, 4).trim();
comment = new String(data, 97, 28).trim();
r1 = data[125];
r2 = data[126];
r3 = data[127];
} else {
valid = false;
}
}
public SongInfo() {
}
/**
* 返回是否合法
* @return 是否
*/
public boolean isValid() {
return valid;
}
/**
* 得到此对象的128个字节的表示形式
* @return
*/
public byte[] getBytes() {
byte[] data = new byte[128];
System.arraycopy(TAG.getBytes(), 0, data, 0, 3);
byte[] temp = songName.getBytes();
System.arraycopy(temp, 0, data, 3, temp.length > 30 ? 30 : temp.length);
temp = artist.getBytes();
System.arraycopy(temp, 0, data, 33, temp.length > 30 ? 30 : temp.length);
temp = album.getBytes();
System.arraycopy(temp, 0, data, 63, temp.length > 30 ? 30 : temp.length);
temp = year.getBytes();
System.arraycopy(temp, 0, data, 93, temp.length > 4 ? 4 : temp.length);
temp = comment.getBytes();
System.arraycopy(temp, 0, data, 97, temp.length > 28 ? 28 : temp.length);
data[125] = r1;
data[126] = r2;
data[127] = r3;
return data;
}
public String getArtist() {
return artist;
}
public void setArtist(String authorName) {
this.artist = authorName;
}
public String getComment() {
return comment;
}
public void setComment(String comment) {
this.comment = comment;
}
public byte getR1() {
return r1;
}
public void setR1(byte r1) {
this.r1 = r1;
}
public byte getR2() {
return r2;
}
public void setR2(byte r2) {
this.r2 = r2;
}
public byte getR3() {
return r3;
}
public void setR3(byte r3) {
this.r3 = r3;
}
public String getSongName() {
return songName;
}
public void setSongName(String songName) {
if(songName==null){
throw new NullPointerException("歌名不能是null!");
}
valid=true;
this.songName = songName;
}
public String getAlbum() {
return album;
}
public void setAlbum(String specialName) {
this.album = specialName;
}
public String getYear() {
return year;
}
public void setYear(String year) {
this.year = year;
}
}
编辑对话框的代码:
/*
* SongInfoDialog.java
*
* Created on 2007年11月26日, 下午4:12
*/
package com.hadeslee.music;
import java.awt.Dialog;
import java.awt.Frame;
import java.io.File;
import java.io.RandomAccessFile;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author hadeslee
*/
public class SongInfoDialog extends javax.swing.JDialog {
private SongInfo info; //歌曲信息对象
private File file; //文件对象
private boolean valid; //表示原来这个文件是不是合法的,如果不是,就要重新写入128个字节
/** Creates new form SongInfoDialog */
public SongInfoDialog(java.awt.Frame parent, boolean modal) {
super(parent, modal);
initComponents();
}
public SongInfoDialog(Dialog parent, boolean modal) {
super(parent, modal);
initComponents();
}
public SongInfoDialog(Frame parent, boolean modal, File file) {
this(parent, modal);
this.file = file;
init();
}
public SongInfoDialog(Dialog parent, boolean modal, File file) {
this(parent, modal);
this.file = file;
init();
}
/**
* 初始化
*/
private void init() {
try {
fileName.setText(file.toString());
RandomAccessFile ra = new RandomAccessFile(file, "r");
byte[] buffer = new byte[128];
ra.seek(ra.length() - 128);
ra.read(buffer);
info = new SongInfo(buffer);
valid = info.isValid();
title.setText(info.getSongName());
artist.setText(info.getArtist());
album.setText(info.getAlbum());
year.setText(info.getYear());
comment.setText(info.getComment());
r2.setText("" + info.getR2());
r3.setText("" + info.getR3());
ra.close();
} catch (Exception ex) {
Logger.getLogger(SongInfoDialog.class.getName()).log(Level.SEVERE, null, ex);
}
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
//
private void initComponents() {
jLabel5 = new javax.swing.JLabel();
fileName = new javax.swing.JTextField();
jPanel1 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
title = new javax.swing.JTextField();
jLabel2 = new javax.swing.JLabel();
artist = new javax.swing.JTextField();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
album = new javax.swing.JTextField();
jLabel6 = new javax.swing.JLabel();
r2 = new javax.swing.JTextField();
year = new javax.swing.JTextField();
jLabel7 = new javax.swing.JLabel();
r3 = new javax.swing.JTextField();
jLabel8 = new javax.swing.JLabel();
jScrollPane1 = new javax.swing.JScrollPane();
comment = new javax.swing.JTextArea();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
jLabel9 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
jLabel5.setText("文件名:");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -