Results 1 to 20 of 37
Thread: Help with Mp3 player
- 05-05-2011, 06:47 PM #1
Member
- Join Date
- May 2011
- Posts
- 20
- Rep Power
- 0
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.
- 05-07-2011, 03:31 PM #2
Member
- Join Date
- May 2011
- Posts
- 20
- Rep Power
- 0
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.
- 05-07-2011, 04:21 PM #3
Senior Member
- Join Date
- Mar 2011
- Posts
- 261
- Rep Power
- 3
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.
- 05-07-2011, 05:13 PM #4
Member
- Join Date
- May 2011
- Posts
- 20
- Rep Power
- 0
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:
It is supposed to run when i press button [0]. Thats at least what i tried in this if statement:Java Code:class Stopthr implements Runnable{ //Stop player thread public void run() { try{ player.stop(); } catch(Exception e){ }
I try both interrupting the thread that is playing the mp3 file and i try starting the thread that stops the player.Java Code:else if (btn[0]== e.getSource()){ // the button[i] is being pressed try{ playthread.interrupt(); stopthread.start(); } catch(Exception ex){ }
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;
- 05-07-2011, 05:48 PM #5
Senior Member
- Join Date
- Mar 2011
- Posts
- 261
- Rep Power
- 3
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);
- 05-07-2011, 06:29 PM #6
Member
- Join Date
- May 2011
- Posts
- 20
- Rep Power
- 0
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.
- 05-07-2011, 06:39 PM #7
Senior Member
- Join Date
- Mar 2011
- Posts
- 261
- Rep Power
- 3
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.
- 05-07-2011, 06:57 PM #8
Member
- Join Date
- May 2011
- Posts
- 20
- Rep Power
- 0
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()?
-
- 05-07-2011, 07:02 PM #10
Member
- Join Date
- May 2011
- Posts
- 20
- Rep Power
- 0
- 05-07-2011, 07:06 PM #11
Member
- Join Date
- May 2011
- Posts
- 20
- Rep Power
- 0
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)
-
- 05-07-2011, 07:11 PM #13
Member
- Join Date
- May 2011
- Posts
- 20
- Rep Power
- 0
-
- 05-07-2011, 07:18 PM #15
Member
- Join Date
- May 2011
- Posts
- 20
- Rep Power
- 0
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();
}
-
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.
- 05-07-2011, 07:44 PM #17
Member
- Join Date
- May 2011
- Posts
- 20
- Rep Power
- 0
So, when i press the stop button, the player is already null?
How can that be? I hear the music playing.
-
- 05-07-2011, 07:56 PM #19
Member
- Join Date
- May 2011
- Posts
- 20
- Rep Power
- 0
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?
-
Similar Threads
-
Wav player not playing
By RightGoodEnt in forum CLDC and MIDPReplies: 0Last Post: 02-13-2011, 04:23 PM -
JMF player.setSource
By lambi in forum Advanced JavaReplies: 2Last Post: 02-22-2010, 01:09 PM -
SWT Flash Player
By forthe in forum SWT / JFaceReplies: 0Last Post: 07-29-2008, 08:48 AM -
mp3 player applet
By willemjav in forum Java AppletsReplies: 0Last Post: 05-20-2008, 01:16 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks