1、程序功能:自动搜索计算机中的mp3文件,根据mp3文件中的艺术家、流派、专辑、等信息,对mp3进行分类,并置于不同的文件夹中
2、程序源代码
Song.java
package org.iim.media.music; import java.io.File; import java.io.IOException; import org.farng.mp3.MP3File; import org.farng.mp3.TagException; public class Song { protected MP3File mp3File = null; //构造函数:利用文件名和File类构造mp3文件类 public Song(String mp3FilePath) throws IOException, TagException { mp3File = new MP3File(new File(mp3FilePath)); } public Song(File file) throws IOException, TagException{ this.mp3File = new MP3File(file); } public MP3File getMP3File(){ return mp3File; } //将音乐文件按照音乐家-歌曲名进行格式化 public void moveTo(String destinationFolder){ mp3File.getMp3file().renameTo(new File(destinationFolder + "//" + mp3File.getMp3file().getName())); } }
ID3v2.java
package org.iim.media.music; import java.io.File; import java.io.IOException; import org.farng.mp3.TagException; public class ID3v2Song extends Song { public ID3v2Song(String mp3FilePath) throws IOException, TagException { super(mp3FilePath); } public ID3v2Song(File file) throws IOException, TagException { super(file); } @Override public void moveTo(String destinationFolder) { File destFolder = new File(destinationFolder); if (!destFolder.exists()) { // 目标文件夹不存在 destFolder.mkdir(); } File aritistFolder = new File(destinationFolder + "//" + mp3File.getID3v2Tag().getLeadArtist()); if (!aritistFolder.exists()) { // 歌手文件夹不存在 aritistFolder.mkdir(); } mp3File.getMp3file().renameTo(new File(destinationFolder + "//" + mp3File.getID3v2Tag().getLeadArtist() + "//" + mp3File.getMp3file().getName())); } }
SongFactory.java
package org.iim.media.music; import java.io.File; import java.io.IOException; import javax.swing.JOptionPane; import org.farng.mp3.TagException; public class SongFactory { public static Song createSong(File file) throws IOException{ try { Song song = new Song(file); if (song.getMP3File().hasID3v2Tag()) return new ID3v2Song(file); else if (song.getMP3File().hasID3v1Tag()) return new ID3v1Song(file); else return new Song(file); } catch (TagException e) { JOptionPane.showMessageDialog(null, file.getName() + ".mp3文件标签错误! 已跳过此文件的处理"); e.printStackTrace(); return null; } } }
SongArrangement.java
package org.iim.media.music; import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; import javax.swing.JOptionPane; public class SongArrangment { private static List<Song> songs = new ArrayList<Song>(); Pattern pattern = Pattern.compile(".+//.mp3"); public SongArrangment(String sourceFolderPath) throws IOException { _getSongList(sourceFolderPath); } private void _getSongList(String sourceFolderPath) throws IOException{ File file = new File(sourceFolderPath); File[] fileList = file.listFiles(); for (File f : fileList){ if(f.isFile()){ Matcher matcher = pattern.matcher(f.getPath()); if(matcher.find()){ Song s = SongFactory.createSong(f); if (s != null){ songs.add(s); } } }else{ _getSongList(f.getPath()); } } } public void moveTo(String destinationFolder){ for(Song s : songs){ s.moveTo(destinationFolder); } } public static void main(String[] args) { try { SongArrangment sa = new SongArrangment("E://Music"); sa.moveTo("E://MyMusic"); } catch (IOException e) { JOptionPane.showMessageDialog(null, "处理文件出现错误"); e.printStackTrace(); } } }
3、Org.farng.mp3包对ID3进行处理的时候,会出现乱码,不知道怎么解决......
本版积分规则 发表回复 回帖并转播 回帖后跳转到最后一页
QQ咨询|关于我们|Archiver|手机版|小黑屋|( 辽ICP备15012455号-4 ) Powered by 期权论坛 X3.2 © 2001-2016 期权工具网&期权论坛 Inc.
下载期权论坛手机APP