首页 > 其他分享 >mp3文件后128字节歌曲信息读写操作

mp3文件后128字节歌曲信息读写操作

时间:2023-07-31 22:07:39浏览次数:37  
标签:String temp 读写操作 mp3 128 byte data public songName


参照网上相关资料,实现向mp3文件后128字节写入指定数据,并读取出来.
项目中为向MP3文件写入歌曲信息,如歌手、专辑、歌名等.
第一步:创建关键类 SongInfo

public class SongInfo {private String TAG = "TAG"; // 文件头1-3
  private String songName = "歌名"; // 歌曲名4-33
  private String artist = "歌手名"; // 歌手名34-63
  private String album = "专辑"; // 专辑名61-93
  private String year = "2007"; // 年94-97
  private String comment = "200"; // 备注98-125       java 放文件大小
  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 = "";
   try {
    tag = new String(data, 0, 3);
   } catch (Exception e) { 
    System.out.println("error >> :"+e.getMessage());
    e.printStackTrace();
   }
   
   // 只有前三个字节是TAG才处理后面的字节
   if (tag.equalsIgnoreCase("TAG")) {
    try {
     valid = true;
     songName = new String(data, 3, 30,"UTF-8").trim();
     artist = new String(data, 33, 30,"UTF-8").trim();
     album = new String(data, 63, 30,"UTF-8").trim();
     year = new String(data, 93, 4).trim();
     comment = new String(data, 97, 28).trim();
     r1 = data[125];
     r2 = data[126];
     r3 = data[127];
     
    } catch (Exception e) {
     System.out.println("error 22>> :"+e.getMessage());
     e.printStackTrace();
    }
   } else {
    valid = false;
   }
  }public SongInfo(String tag,String songName,String artist, String album,String fileSize) {
   this.TAG = tag;
   this.songName = songName;
   this.artist = artist;
   this.album = album;
   this.comment = fileSize;
  }  public boolean isValid() {
   return valid;
  }  public  byte[] getBytes() {
   byte[] data = new byte[128];
   try {
    System.arraycopy(TAG.getBytes(), 0, data, 0, 3);
    byte[] temp;
    temp = songName.getBytes("UTF-8");
    
    System.arraycopy(temp, 0, data, 3, temp.length > 30 ? 30 : temp.length);
    temp = artist.getBytes("UTF-8");
    System.arraycopy(temp, 0, data, 33, temp.length > 30 ? 30: temp.length);
    
    temp = album.getBytes("UTF-8");
    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;
    
   } catch (Exception e) {
    e.printStackTrace();
   }
   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;
  }
 }
 第二步: 调用此类实现写MP3文件后128字节数据操作
 public void setSongInfo(String url, String tag, String songName,
    String artist, String album, String fileSize) {
   FileConnection fc = null;
   SongInfo info = null;
   String fileUrl = url;
   OutputStream out = null;
   byte[] buffer = new byte[128];
   try {
    fc = (FileConnection) Connector.open(fileUrl, Connector.READ_WRITE);  if (fc.exists()) {
     info = new SongInfo(tag, songName, artist, album, fileSize);   buffer = info.getBytes();
   out = fc.openOutputStream(fc.fileSize());
   out.write(buffer);
   out.flush();
    }
   } catch (IOException ioe) {
    ioe.printStackTrace();
   } finally {
    try {
     out.close();
     fc.close();
    } catch (IOException e) {
     e.printStackTrace();
    }
   }
  }第三步:读取后128字节内容
 public SongInfo getSongInfo(String url) {
   FileConnection fc = null;
   SongInfo info = null;
   InputStream in = null;
   String songName = "song";
   long songSize = 200;
   String fileUrl = url;
   byte[] buffer = new byte[128];
   try {
    fc = (FileConnection) Connector.open(fileUrl, Connector.READ_WRITE);
    if (fc.exists()) {
     long size = fc.fileSize();   songName = fc.getName();
     songName = songName.substring(0, songName.length() - 4);
     songSize = fc.fileSize();   long byteOffset = size - 128;
   in = fc.openInputStream();
   in.skip(byteOffset);
   in.read(buffer);
    }
    info = new SongInfo(buffer);
    if (info.getSongName().equals("歌名")) {
     info.setSongName(songName);
    }
    if (info.getComment().equals("200")) {
     info.setComment("" + songSize);
    }
    if (info.getAlbum().equals("")) {
     info.setAlbum("未知");
    } } catch (IOException ioe) {
    m_controll.setCurrent(new MessageAlert("warn",
      " getSongInfo error: " + ioe.getMessage() + curURI, disp,
      m_controll));
    ioe.printStackTrace();
   } finally {
    try {
     in.close();
     fc.close();
    } catch (IOException e) {
     e.printStackTrace();
    }
   }
   return info;
  }

标签:String,temp,读写操作,mp3,128,byte,data,public,songName
From: https://blog.51cto.com/u_3124497/6913699

相关文章

  • Artistdirect.com为Zune播放器提供的热门歌曲MP3
    Artistdirect.com为Zune播放器提供的热门歌曲MP3http://assets.artistdirect.com/Downloads/artd/listen/Smile.mp3http://assets.artistdirect.com/Downloads/artd/listen/cracker-low.mp3http://assets.artistdirect.com/Downloads/artd/listen/DDTS.mp3http://assets.artistdire......
  • 128MTT 学习笔记
    标题是我乱起的名字。在做某题时受到了启发,想出了一种之前没听说过的MTT,在某谷上一问发现有人和我想的一样,立马去学了。这种方法,我叫它128MTT,它用到了科技__int128。主要思想就是找一个\(10^{27}\)以上的大NTT模数,全程使用__int128做NTT。然而longlong取模尚能用......
  • CF1285F Classical?
    根据唯一分解定理,令\(x=p_1^{q_1}p_2^{q_2}\cdotsp_m^{q_m},y=p_1^{k_1}p_2^{k_2}\cdotsp_m^{k_m}\),则\(\text{lcm}(x,y)=p_1^{\max(q_1,k_1)}p_2^{\max(q_2,k_2)}\cdotsp_m^{\max(q_m,k_m)}\)。那么一定存在\(i\midx,j\midy\),使得\(\text{gcd}(i,j)=1\)且\(\te......
  • 128. 最长连续序列
    给定一个未排序的整数数组nums,找出数字连续的最长序列(不要求序列元素在原数组中连续)的长度。请你设计并实现时间复杂度为O(n)的算法解决此问题。示例1:输入:nums=[100,4,200,1,3,2]输出:4解释:最长数字连续序列是[1,2,3,4]。它的长度为4。>方法一:双指针class......
  • Integer和int为什么在-128-127之间比较会相等
    原因:因为在Integer.class文件中,有一个静态内部类IntegerCache;会在系统加载时,将(low=-128到h=127)之间的数据提前包装成Integer对象放入数组cache中;inta=111l;Integerc=111l;System.out.println(a==c);//在次会发生自动的装箱,将a转换成Integer在对比字节码文件可......
  • 如何使用C#中的Lambda表达式操作Redis Hash结构,简化缓存中对象属性的读写操作
    Redis是一个开源的、高性能的、基于内存的键值数据库,它支持多种数据结构,如字符串、列表、集合、散列、有序集合等。其中,Redis的散列(Hash)结构是一个常用的结构,今天跟大家分享一个我的日常操作,如何使用Redis的散列(Hash)结构来缓存和查询对象的属性值,以及如何用Lambda表达式树来简化......
  • codeforces1283F
    题目链接sol:根一定是第一个,然后不太会,去看了洛谷题解题解#include<bits/stdc++.h>usingnamespacestd;typedeflonglongll;typedefpair<int,int>pii;#definefifirst#definesesecond#definefz1(i,n)for((i)=1;(i)<=(n);(i)++)#definefd1(i,n)for((i)......
  • LeetCode 热题 100 之 128. 最长连续序列
    题目描述给定一个未排序的整数数组nums,找出数字连续的最长序列(不要求序列元素在原数组中连续)的长度。请你设计并实现时间复杂度为 O(n)的算法解决此问题。示例1:输入:nums=[100,4,200,1,3,2]输出:4解释:最长数字连续序列是[1,2,3,4]。它的长度为4。示例2:输入:nums......
  • 1280. 学生们参加各科测试的次数
    1280.学生们参加各科测试的次数SQL架构学生表:Students+---------------+---------+|ColumnName|Type|+---------------+---------+|student_id|int||student_name|varchar|+---------------+---------+主键为student_id(学生ID),该表内的......
  • LCD12864单色屏任意位置显示文字图片功能,不在受限于8bit的分行
    /*取模软件image2Lcdv2.9液晶取模方式:扫描方式:数据水平,字节垂直输出灰度:单色最大宽度和高度:128*64字节内像素数据反序*/#defineLCD_REVERSE_FLAG0#defineLCD_DISPLAY_NORMAL0#defineLCD_DISPLAY_REVERSE1#defineLCD_BUFF_BYTE_MAX1024#defineSCREEN_WIDT......