Page 1 of 2 12 LastLast
Results 1 to 20 of 37
  1. #1
    Crathes is offline Member
    Join Date
    May 2011
    Posts
    20
    Rep Power
    0

    Default Help with Mp3 player

    Hello

    Just to start, I tried using the search engine but could not find a post with then answers i was looking for.

    I am programming an Mp3 player for a school project, but i am a bit stuck.
    At the moment i have a GUI layout with three buttons created in a button array and added to a panel by a loop.
    I have two classes that implements runnable.
    One of them fetches a file from a URL location on my pc, and get the player started.
    The other class is supposed to stop the player.
    I have two if sentences(?) that check which button i press.
    When i press the play button, everything works as it is supposed to. But when i press the stop button, nothing happens.

    Please help me :)

    My plan is to add a playlist and some other stuff, but i have to get this stuff to work.

    (tell me if you guys need me to post the code)
    EDIT: posted code what i thought was relevant (no import stuff):
    Java Code:
    public class oppg2vaar2011Design extends JFrame implements ActionListener{
    
    	ImageIcon playimg = new ImageIcon("src/playbutton1.gif");
    	ImageIcon pauseimg = new ImageIcon("src/pausebutton1.gif");
    	ImageIcon stopimg = new ImageIcon("src/stopbutton1.gif");
    	
            private AdvancedPlayer player;
            private PlaybackListener listener;
    	JPanel p1 = new JPanel();
    	private GridLayout g1 = new GridLayout(1,3);	
    	JButton btn[] = new JButton[3];
    
              oppg2vaar2011Design() {
    		setSize(500,250);
    		setTitle("MP3 Spiller");
    		setDefaultCloseOperation(EXIT_ON_CLOSE);
    
    		this.getContentPane().setLayout(new BorderLayout());
    		this.getContentPane().add(p1);
    		
    		this.p1.setLayout(g1);
    		
    		// add buttons
    		for (int i=0; i<btn.length;i++){
    			btn[i]= new JButton();
    			btn[i].addActionListener(this);
    			btn[i].putClientProperty( "JButton.buttonType", "roundRect" ); 
    			p1.add(btn[i]);
    	         }
    		//Sett ikon på knappene
    		this.btn[1].setIcon(playimg);
    		this.btn[2].setIcon(stopimg);
    		
    		}
    	
    	//Play thread, linked to play button in an if sentence
    	class Playthr implements Runnable{ //Start player thread
    
    		public void run() {
    			try{
    				URL url = new URL("file:///C://a.mp3");
    				InputStream in = url.openStream();
                                    player = new AdvancedPlayer(in);
    				player.setPlayBackListener(listener); //added listener
    				player.play();
    			}
    			catch(Exception e){
                                    e.printStackTrace();
    			}
    		}
    	}
    	//stop thread linked to stop button in if sentence
    	class Stopthr implements Runnable{ //Stop player thread
    
    		public void run() {
    			try{
    				//player.stop();
                                    player.close();
    				
    			}
    			catch(Exception e){
                             e.printStackTrace();
    			}
    		}
    	}
    	
    
    	public void actionPerformed(ActionEvent e) {
    		
    		Runnable runplay = new Playthr(); //get commands in Play thread
    		Runnable runstop = new Stopthr();//get commands in stop thread
    		Thread playthread = new Thread(runplay);//make play thread
    		Thread stopthread = new Thread(runstop);//make stop thread
    				for (int i=0; i<btn.length;i++) // using for loop to scan buttons
    			if (btn[1]== e.getSource()){ // the button[i] is being pressed
    				try{
    					playthread.start();
    				}
    				catch(Exception ex){
                                            e.printStackTrace();
    
    				}
    			break;
    			}
    			else if (btn[0]== e.getSource()){ // the button[i] is being pressed
    					try{
    						playthread.interrupt();
    						stopthread.start();
    					}
    					catch(Exception ex){
                                                    e.printStackTrace();
    					}
    				}
    		}
    Last edited by Crathes; 05-08-2011 at 11:32 AM.

  2. #2
    Crathes is offline Member
    Join Date
    May 2011
    Posts
    20
    Rep Power
    0

    Default

    Bump? I really need help guys :(
    Is my post understandable? I'm asking because if it is, I'll have to try to explain my problem better.

  3. #3
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default

    Why don't you start by handling your exceptions? Especially for blocks that you have problems with, like the block where you stop your player. Besides that, I don't see the if statements nor the stop() method or the AdvancedPlayer class.

  4. #4
    Crathes is offline Member
    Join Date
    May 2011
    Posts
    20
    Rep Power
    0

    Default

    what do you mean handling the exceptions? Displaying them through a system.out.prinln?

    This is the block that is supposed to stop the player:
    Java Code:
    class Stopthr implements Runnable{ //Stop player thread
    
    		public void run() {
    			try{
    				player.stop();
    				
    			}
    			catch(Exception e){
    			}
    It is supposed to run when i press button [0]. Thats at least what i tried in this if statement:
    Java Code:
    else if (btn[0]== e.getSource()){ // the button[i] is being pressed
    					try{
    						playthread.interrupt();
    						stopthread.start();
    					}
    					catch(Exception ex){
    
    					}
    I try both interrupting the thread that is playing the mp3 file and i try starting the thread that stops the player.

    The Advanced player is made further up in the code(i chose not to include that part, because its just a lot of declaring variables and stuff) through this code:
    Java Code:
    private AdvancedPlayer player;

  5. #5
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default

    Do you know what exceptions are there for? There not just something you have to write, they tell you what's wrong when you have a problem like this (in other words, they're extremely useful). So yes, use e.printStackTrace() or System.out.println(e) for exceptions, especially when something is wrong in that block of code.

    Why don't you add an ActionListener for your buttons? Here's one you can use for your stop button, so that when the button is pressed it executes the code within the actionPerformed() method.

    Java Code:
    ActionListener buttonListener1 = new ActionListener() {
    public void actionPerformed(ActionEvent evt){
    try{
    playthread.interrupt();
    stopthread.stop();
    }catch(Exception e){e.printStackTrace();}
    }
    };
    button[0].addActionListener(buttonListener1);

  6. #6
    Crathes is offline Member
    Join Date
    May 2011
    Posts
    20
    Rep Power
    0

    Default

    Oh, ok... I hope i understand the error messages then ^^

    The action listener for the buttons are added in the same loop that ads the buttons to a panel. Part of the code i didn't think was necessary.

    I'll just add it to the OP

    EDIT: added full code to OP

    The error message from the stop button gives me the message "null". This means that i get no data when pressing the button?
    Last edited by Crathes; 05-07-2011 at 06:41 PM.

  7. #7
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default

    You ignored what I said twice. Add a e.printStackTrace() to your exception handling code for the block that stops your player. Then, tell me the error if any you get.

    EDIT: Copy and paste the entire error here.
    Last edited by Solarsonic; 05-07-2011 at 06:43 PM.

  8. #8
    Crathes is offline Member
    Join Date
    May 2011
    Posts
    20
    Rep Power
    0

    Default

    No, I added an system.out.println to the exceptions. The message i got was "null"
    Should i add both a system.out.println and an e.printStackTrace()?

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Crathes View Post
    No, I added an system.out.println to the exceptions. The message i got was "null"
    Should i add both a system.out.println and an e.printStackTrace()?
    I don't see any code in your catch blocks in the code you've posted above. And yes, you'll want to add e.printStackTrace(); in the catch blocks, not println.

  10. #10
    Crathes is offline Member
    Join Date
    May 2011
    Posts
    20
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    I don't see any code in your catch blocks in the code you've posted above. And yes, you'll want to add e.printStackTrace(); in the catch blocks, not println.
    Oh, sorry... forgot to update the code up top.
    Doing it now.

  11. #11
    Crathes is offline Member
    Join Date
    May 2011
    Posts
    20
    Rep Power
    0

    Default

    The error message i got from e.printStackTrace() says:

    java.lang.NullPointerException
    at javazoom.jl.player.advanced.AdvancedPlayer.stop(Un known Source)
    at oppg2vaar2011Design$Stopp.run(oppg2vaar2011Design. java:82)
    at java.lang.Thread.run(Unknown Source)

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Crathes View Post
    The error message i got says:

    java.lang.NullPointerException
    at javazoom.jl.player.advanced.AdvancedPlayer.stop(Un known Source)
    at oppg2vaar2011Design$Stopp.run(oppg2vaar2011Design. java:82)
    at java.lang.Thread.run(Unknown Source)
    Now we're getting somewhere. What line is line 82 of your file?

  13. #13
    Crathes is offline Member
    Join Date
    May 2011
    Posts
    20
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Now we're getting somewhere. What line is line 82 of your file?
    Just wait a sec... have no idea how i check line in eclipse. Have to download notepad ++.

  14. #14
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Crathes View Post
    Just wait a sec... have no idea how i check line in eclipse. Have to download notepad ++.
    In Eclipse all you have to do is click on the exception.

  15. #15
    Crathes is offline Member
    Join Date
    May 2011
    Posts
    20
    Rep Power
    0

    Default

    Great, thanks. Learning new stuff all the time :)

    line 82:
    player.stop();

    can be found here:
    class Stopthr implements Runnable{ //Stop player thread

    public void run() {
    try{
    player.stop();

    }
    catch(Exception e){
    e.printStackTrace();
    }

  16. #16
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Then player is null when you try to call the stop method on it above. Now you've got to trace back into your code and figure out why.

  17. #17
    Crathes is offline Member
    Join Date
    May 2011
    Posts
    20
    Rep Power
    0

    Default

    So, when i press the stop button, the player is already null?
    How can that be? I hear the music playing.

  18. #18
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Crathes View Post
    So, when i press the stop button, the player is already null?
    How can that be? I hear the music playing.
    You likely have more than one Player variables, one null, the other not. This can happen for instance if you have created more than one oppg2vaar2011Design objects.

  19. #19
    Crathes is offline Member
    Join Date
    May 2011
    Posts
    20
    Rep Power
    0

    Default

    Right at the start i have make the player variable through this:
    private AdvancedPlayer player;

    further down i do this to get the file the player is supposed to play:
    InputStream in = url.openStream();
    player = new AdvancedPlayer(in);

    could that second new AdvancedPlayer mess stuff up?

  20. #20
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Crathes View Post
    Right at the start i have make the player variable through this:
    private AdvancedPlayer player;

    further down i do this to get the file the player is supposed to play:
    InputStream in = url.openStream();
    player = new AdvancedPlayer(in);

    could that second new AdvancedPlayer mess stuff up?
    That is only one AdvancedPlayer object -- you only use the new keyword once.

    How many times do you call new oppg2vaar2011Design()?

Page 1 of 2 12 LastLast

Similar Threads

  1. Wav player not playing
    By RightGoodEnt in forum CLDC and MIDP
    Replies: 0
    Last Post: 02-13-2011, 04:23 PM
  2. JMF player.setSource
    By lambi in forum Advanced Java
    Replies: 2
    Last Post: 02-22-2010, 01:09 PM
  3. SWT Flash Player
    By forthe in forum SWT / JFace
    Replies: 0
    Last Post: 07-29-2008, 08:48 AM
  4. mp3 player applet
    By willemjav in forum Java Applets
    Replies: 0
    Last Post: 05-20-2008, 01:16 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •