Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: MP3 Player: ID3 Tag Image Retrieval Problem for the Applet from internet

  1. #1
    Junior Member
    Join Date
    May 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default MP3 Player: ID3 Tag Image Retrieval Problem for the Applet from internet

    Hey, could anyone help me please..?

    I'm currently creating an MP3 Player which needs to find album artwork off the internet once a music file is opened.

    I have included code below. The problem lies after the highlighted red text. I'm trying to find a way for the applet to display an image gathered from the internet should the local "AlbumArt.jpg" file not be present.

    I would be eternally grateful if someone could show me how the ID3 Tags within the "artistField" and "albumField" could be used to find appropriate album artwork from the Amazon website.

    Thanks in advance!


    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
    Last edited by Deep_4; November 7th, 2012 at 10:07 PM.


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default Re: MP3 Player: ID3 Tag Image Retrieval Problem

    Hello JavaJames, welcome to the Java Programming Forums.

    Does Amazon supply an API to allow you to extract album artwork from their website or are you going to need to use some programming magic?
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  3. #3
    Junior Member
    Join Date
    May 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: MP3 Player: ID3 Tag Image Retrieval Problem

    Hello and thankyou for the kind welcome,

    There is an Amazon Web Services API, however I am quite new to Java so having trouble in using it. I do not need a complex solution, just something which will insert my artist and album ID3 Tags into a search within a URL.

    It is not essential I use Amazon if there is a more convenient solution.

  4. #4
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default Re: MP3 Player: ID3 Tag Image Retrieval Problem

    What is the URL that you need to insert the artist & album tags into?

    Please provide me with links to the Amazon web services API.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  5. #5
    Junior Member
    Join Date
    May 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: MP3 Player: ID3 Tag Image Retrieval Problem

    The link to the Amazon Web Services is What is AWS? and the APIs appear to all be held here Typica - Amazon EC2/SQS/SimpleDB Client

    A direct link to an album artwork image on the Amazon website produces an address such as this: Fractured Life: Air Traffic I do not see where within the address the ID3 Tags could be inserted however?

    Thanks for the help!

  6. #6
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default Re: MP3 Player: ID3 Tag Image Retrieval Problem

    Hello James,

    I have just been reading about the Amazon API.
    First things first - you need an Amazon Web Service (AWS) account. This will give you a (public) access key ID, and a (private) secret access key.
    This will enable you to start making web service calls.

    Please go here to sign up:

    Amazon Web Services

    You will also need an Amazon Associates (aka Affiliates) ID. Sign up for one here:

    Amazon Affiliate Program

    Once you have your accounts created. Please PM me the details and I will help you put this into your application.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

Similar Threads

  1. GUI problem with image in java
    By Kok in forum AWT / Java Swing
    Replies: 6
    Last Post: May 17th, 2009, 04:17 AM
  2. How images stores in awt.Image class?
    By BharatT in forum AWT / Java Swing
    Replies: 0
    Last Post: February 24th, 2009, 04:10 AM
  3. How to read character from image area(jpg image)?
    By sundarjothi in forum Java Theory & Questions
    Replies: 5
    Last Post: August 6th, 2008, 02:08 AM
  4. How to import an .tiff image in JSP?
    By jazz2k8 in forum JavaServer Pages: JSP & JSTL
    Replies: 4
    Last Post: May 12th, 2008, 05:55 AM

Tags for this Thread