import java.awt.*; //enables access to the Container & BorderLayout
import javax.swing.*; //enables access to the javax.swing.JFrame class
import javax.swing.event.*; //enables access to the ActionListener
import java.io.*; // required for file access for input output
import java.net.MalformedURLException;
import java.net.URL;
import java.awt.event.*; //enables access to ActionListener
import javax.imageio.ImageIO;
import javax.media.Manager; //JMF required
import javax.media.Player; //JMF required
//import java.applet.*; //AudioClip??
public class MP3ID3Tags extends JFrame implements ActionListener //ActionListener for menu only
{
File inputFile; //declare the variable name for the File object (inputFile)
File outputFile; //declare the variable name for the File object (outputFile)
JFileChooser yourFileChooser = new JFileChooser(); //declare file chooser
String currFileName = null; // Full path with filename. null means new / untitled.
JLabel statusBar = new JLabel(); // use a JLabel to create a status bar
JPanel mP3Panel;
JTextField fileField, artistField, albumField;
JLabel fileLabel, artistLabel, albumLabel;
JButton jBPlay, jBStop;
//sound??????????
//AudioClip sound1;
//Player myMp3File = null;
JLabel jLabelArtWork;
JTextArea editorTextArea; //declare Text Area for editor
JScrollPane scrollsVH; //declare vertical & horizontal scrolling
Container yourContainer; //declare container
JMenuBar topMenuBar; //declare Menu Bar
JMenu fileMenu, editMenu, searchMenu, helpMenu; //declare sub-Menus
JMenuItem openItem, saveItem, exitItem, copyItem, pasteItem, helpItem, aboutItem; //declare sub-Menu Items
public MP3ID3Tags() //could use (String title) then super(title) and declare title.
{
super ("MP3 Player with ID3 Tag Retrieval"); //set the JFrame title
yourContainer = getContentPane(); // get content pane and name it
yourContainer.setLayout(new BorderLayout()); // use border layout
editorTextArea = new JTextArea(); //create text area for text editing
scrollsVH = new JScrollPane(editorTextArea, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
yourContainer.add(scrollsVH, BorderLayout.CENTER); //add text area with V & H scrolling to container
yourContainer.add(statusBar, BorderLayout.SOUTH); //add status bar to bottom of frame
mP3Panel = new JPanel();
mP3Panel.setPreferredSize(new Dimension(400, 600));
fileLabel = new JLabel(" File Name:");
mP3Panel.add(fileLabel);
fileField = new JTextField(30);
mP3Panel.add(fileField);
artistLabel = new JLabel("Artist Name (TPE1):");
mP3Panel.add(artistLabel);
artistField = new JTextField(30);
mP3Panel.add(artistField);
albumLabel = new JLabel("Album Name (TALB):");
mP3Panel.add(albumLabel);
albumField = new JTextField(30);
mP3Panel.add(albumField);
jBPlay = new JButton("Play");
jBPlay.addActionListener(this);
mP3Panel.add(jBPlay);
jBStop = new JButton("Stop");
jBStop.addActionListener(this);
mP3Panel.add(jBStop);
[COLOR="Red"]//Add album artwork to place in mP3Panel[/COLOR]
ImageIcon icon = createImageIcon("AlbumArt.jpg", /*middle.gif",*/"a pretty but meaningless splat");
jLabelArtWork = new JLabel(icon);
mP3Panel.add(jLabelArtWork);
yourContainer.add(mP3Panel, BorderLayout.EAST); //add MP3 panel to the East
menuSetup(); //standard menu set up method - requires actionListener
setExtendedState(MAXIMIZED_BOTH);
//setSize(600, 450); //set default size of JFrame to width=500 height=350
setVisible(true); //display the JFrame
}
/** Returns an ImageIcon, or null if the path was invalid. */
protected static ImageIcon createImageIcon(String path, String description)
{
java.net.URL imgURL = MP3ID3Tags.class.getResource(path);
if (imgURL != null) {
return new ImageIcon(imgURL, description);
} else {
//try {
//System.err.println("Couldn't find file: " + path);
}
return null;
}
private static Image getImage(String string) {
// TODO Auto-generated method stub
return null;
}
public void menuSetup()
{
topMenuBar = new JMenuBar(); //create a menu bar
setJMenuBar(topMenuBar); //set the menu bar to the JFrame
fileMenu = new JMenu("File"); // File menu, with open, save, exit
openItem = new JMenuItem("Open"); //OPEN item
fileMenu.add(openItem); //add the items to the menu
openItem.addActionListener(this); //add the listener to the item
saveItem = new JMenuItem("Save"); //SAVE item
fileMenu.add(saveItem); //add the items to the menu
saveItem.addActionListener(this); //add the listener to the item
exitItem = new JMenuItem("Exit"); //EXIT item
fileMenu.add(exitItem); //add the items to the menu
exitItem.addActionListener(this); //add the listener to the item
topMenuBar.add(fileMenu); //add the menu to the menu bar
editMenu = new JMenu("Edit"); // edit menu, could have copy, paste
topMenuBar.add(editMenu );
searchMenu = new JMenu("Search");
topMenuBar.add(searchMenu);
helpMenu = new JMenu("Help"); // help menu, with help topics, about application
helpItem = new JMenuItem("Help Topics");
helpMenu.add(helpItem);
helpItem.addActionListener(this);
aboutItem = new JMenuItem("About...");
helpMenu.add(aboutItem);
aboutItem.addActionListener(this);
topMenuBar.add(helpMenu);
}
public void actionPerformed(ActionEvent e)
{
//Player myMp3File;
// String strFileName = "15032.mp3";
File myFile = new File("c:/java_projects/PlayMP3Thread/bin/Whiskey in the jar.mp3");
PlayMP3Thread myMp3 = new PlayMP3Thread(myFile);
if (e.getSource() == openItem)
{ // JFileChooser yourFileChooser = new JFileChooser(); declared above
if (JFileChooser.APPROVE_OPTION == yourFileChooser.showOpenDialog(this))
{ // Call openFile to attempt to load the text from file into JTextArea
openFile(yourFileChooser.getSelectedFile().getPath());
}
this.repaint();//repaints menu after item is selected (even if cancelled).
}
if (e.getSource() == jBPlay)
{
//openFile("c:/java_projects/PlayMP3Thread/bin/Whiskey in the jar.mp3");
try
{
myMp3.run();
//myMp3File = Manager.createPlayer(new File(strFileName).toURI().toURL());
//myMp3File.start();
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
if (e.getSource() == jBStop)
{
try
{
System.out.println("Stop - pressed");
//myMp3.stopPlayer();// .run();
//myMp3File = Manager.createPlayer(new File(strFileName).toURI().toURL());
// myMp3File.stop();//timer
// myMp3File.deallocate();//stops player
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
if (e.getSource() == exitItem)
{
System.exit(0);
}
if (e.getSource() == saveItem)
saveFile();
// JOptionPane.showMessageDialog(null, "Save chosen.");
if (e.getSource() == copyItem)
JOptionPane.showMessageDialog(null, "Copy chosen.");
if (e.getSource() == pasteItem)
JOptionPane.showMessageDialog(null, "Paste chosen.");
if (e.getSource() == helpItem)
JOptionPane.showMessageDialog(null, "Help Topics chosen.");
if (e.getSource() == aboutItem)
JOptionPane.showMessageDialog(null, "Program: MP3 Player with ID3 Tag Embedding");
}
void openFile(String fileName)// Open named file; read text from file into jTextArea1;
{ //report to statusBar.
try
{
inputFile = new File(fileName); // Open a file of the given name.
int size = (int)inputFile.length(); // Get the size of the opened file (cast to integer).
int chars_read = 0; // Set to zero a counter for counting the number of
// characters that have been read from the file.
FileReader in = new FileReader(inputFile);// Create an input reader based on the file, so we can read its data.
// FileReader handles international character encoding conversions.
char data[] = new char[size]; // Create a character array of the size of the file,
// to use as a data buffer, into which we will read
// the text data.
while(in.ready()) // Read all available characters into the buffer if ready()=
//Tells whether this stream is ready to be read.
//An InputStreamReader is ready if its input buffer is not empty,
{
chars_read += in.read(data, chars_read, size - chars_read); // Increment the count for each character read,
} // and accumulate them in the data buffer.
in.close(); //close the input file/stream
String fileDataString = new String(data, 0, chars_read);
editorTextArea.setText(fileDataString); // Create a temporary string containing the data,
// and set the string into the JTextArea.
this.currFileName = fileName; // Cache the currently opened filename for use at save time...
statusBar.setText("Opened "+fileName);// Display the name of the opened directory+file in the statusBar.
mP3(fileDataString);
}
catch (IOException e)
{
statusBar.setText("Error opening "+fileName);
}
//sound1.getAudioClip(getDocumentBase(), currFilename);
//sound1.play();
}
void mP3(String fileDataString)
{
mP3Panel.setLayout(new FlowLayout()); // use default flow layout
System.out.println("File Name \t"+currFileName);
fileField.setText(currFileName);
System.out.println("Tag format: \t"+ fileDataString.substring(0, 3)+"\n version: "+fileDataString.substring(8, 10));
System.out.println("Track Name: \t"+ fileDataString.substring(10, 14)+"\n "+fileDataString.substring(20, 31));
System.out.println("Band Name: \t"+ fileDataString.substring(32, 36)+"\n "+fileDataString.substring(40, 57));
System.out.println("The index position of the beginning of the "+ "substring \"TT2\" is : " + fileDataString.indexOf("TT2"));
int nTT2 = fileDataString.indexOf("TT2");
//String artistString = fileDataString.substring(40, 57);
String artistString = fileDataString.substring(nTT2+3, nTT2+33);
//remove leading spaces.
//retrieve image local - hard coded address
//retrieve image by artist name or album name in the folder
//retrieve wildcard/filter any 1 jpg and load
//load from web
//load from amazon etc.
artistField.setText(artistString);
System.out.println("Composer Name: \t"+ fileDataString.substring(59, 63)+"\n "+fileDataString.substring(65, 75));
System.out.println("Album Name: \t"+ fileDataString.substring(76, 80)+"\n "+fileDataString.substring(87, 102));
//search for substring TALB: this will return the position of T,
//say int nTALB then you know to pick-up the substring between T+4 and T+50
int nTAL = fileDataString.indexOf("TAL");
//String albumString = fileDataString.substring(87, 102);
String albumString = fileDataString.substring(87, 102);
albumField.setText(albumString);
System.out.println("Track No.: \t"+ fileDataString.substring(102, 107)+"\n "+fileDataString.substring(114, 119));
System.out.println("Track Year: \t"+ fileDataString.substring(134,138)+"\n "+fileDataString.substring(145,150));
System.out.println("Genre: \t"+ fileDataString.substring(130, 133)+"\n "+fileDataString.substring(155,179));
System.out.println(artistString+albumString);
/*System.out.println("The test string is: " + test);
System.out.println("Length of this test string is: "+ test.length());
System.out.println("The character at position 10 is: "+ test.charAt(10));
System.out.println("The substring from 68 to 72 is: "+ test.substring(68, 72));
System.out.println("The substring from position 48 onwards is: "+ test.substring(48));
System.out.println("The index position of the character v is: "+ test.indexOf('v'));
System.out.println("The index position of the beginning of the "
+ "substring \"Java\" is : " + test.indexOf("Java"));
System.out.println("The string in upper case: "+ test.toUpperCase());
*/
}
void saveFile()
{ // Save current file; handle not yet having a filename; report to statusBar.
// Handle the case where we don't have a file name yet.
if (currFileName == null)
{
JOptionPane.showMessageDialog(null, "File does not exist\n Open an existing file.");
//return saveAsFile(); //could adapt the program for a 'save as' option
}
try // an exception (error) could occur therefore try & catch (or declare thrown).
{
outputFile = new File (currFileName); // Open a file of the current name.
FileWriter out = new FileWriter(outputFile); // Create an output writer that will write to that file.
// FileWriter handles international characters encoding conversions.
String saveText = editorTextArea.getText(); //get the text from the text area
out.write(saveText); //the write method writes the String to the out file
out.close(); //close the output file/stream
}
catch (IOException e)
{
statusBar.setText("Error saving "+currFileName);
}
}
public static void main(String[] args)
{
try
{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch (Exception e)
{
System.err.println("Couldn't use the system "+ "look and feel: " + e);
}
MP3ID3Tags test = new MP3ID3Tags(); //EditorJMenu("Text EditorJMenu Application");
test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //close frame the Swing
//JDK1.3 version of addWindowListener
}
}// end class MP3ID3Tags