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 7 of 7

Thread: MP3 problems

  1. #1
    Junior Member
    Join Date
    May 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default MP3 problems

    well, ive come across another problem. I was getting tired of all these stupid media players that are out right now... WMP has glitches, and will add your music twice to the library, and the way i wanna play my music is it loads the entire library into memory(the String of the path names, of course!), and then play it in ascending artist and album track number(basically, chronological order)... and if wanna listen to another song, then i can just click on it... unlike media players like jaangle, which its completely playlists. so, since im quite a skilled programmer, i decided to make my own based off of JLayer. which turned out great for me, because i dont know how to write one from scratch... but thats still not perfect, and i wrote a very simple gui, and right now(the problem), ive been writing a pause feature... cause thats not built into JLayer. well, guess what... it doesnt work. itll play music..(one...song...at...a...time...)but the pause feature doesnt. so, ima post the code below... hopefully you can help me(its the full program, btw)
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.io.*;
    import java.util.ArrayList;
    import javazoom.jl.player.*;
    import javax.swing.filechooser.*;
    import javax.swing.text.*;
    public class MusicPlayer implements ActionListener
    {
        JFrame frame;
        JPanel panel;
        Dimension dim;
        JTextArea main;
        ArrayList music;
        int pause;
        mp player;
        public MusicPlayer()
        {
            frame = new JFrame();
            panel = new JPanel();
            panel.setLayout(null);
            frame.getContentPane().add(panel);
            dim = Toolkit.getDefaultToolkit().getScreenSize();
            frame.setBounds(dim.width/2-250,dim.height/2-250,500,500);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            music = new ArrayList();
            initGui();
            frame.setVisible(true);
        }
        public void initGui()
        {
            main = new JTextArea();
            main.setEditable(false);
            DefaultCaret caret = (DefaultCaret)main.getCaret();
            caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
            JScrollPane jsp = new JScrollPane(main);
            jsp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
            jsp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
            jsp.setBounds(0,0,250,250);
            panel.add(jsp);
            Box buttons = new Box(BoxLayout.Y_AXIS);
            JButton play = new JButton("Play");
            play.addActionListener(this);
            buttons.add(play);
            JButton pause = new JButton("Pause");
            pause.addActionListener(this);
            buttons.add(pause);
            JButton load = new JButton("Load");
            load.addActionListener(this);
            buttons.add(load);
            buttons.setBounds(250,0,250,250);
            panel.add(buttons);
        }
        public static void main(String args[])
        {
            new MusicPlayer();
        }
        public void actionPerformed(ActionEvent e)
        {
            String a = e.getActionCommand();
            if(a=="Load")
            {
                JFileChooser jfc = new JFileChooser();
                jfc.setMultiSelectionEnabled(true);
                jfc.setFileFilter(new FileNameExtensionFilter("MP3","mp3"));
                jfc.showOpenDialog(frame);
                File file = jfc.getSelectedFile();
                music.add(file.getPath());
                main.append(file.getName());
                panel.repaint();
            }
            else if(a=="Play")
            {
                if(pause==0)
                {
                    String dir = (String)music.get(0);
                    player = new mp(dir);
                }
                else
                {
                    player = new mp(pause);
                }
            }
            else if(a=="Pause")
            {
                player.close();
                pause = player.getPos();
            }
        }
    }
    class mp implements Runnable
    {
        Player ap;
        Thread thread;
        boolean isRun;
        String song;
        int u = Integer.MAX_VALUE;
        int a = 0;
        public mp(String song)
        {
            isRun = true;
            this.song = song;
            thread = new Thread(this);
            thread.start();
        }
        public mp(int i)
        {
            isRun = true;
            this.song = song;
            u = i;
            thread = new Thread(this);
            thread.start();  
        }
        public void run()
        {
            while(isRun==true)
            {
                try
                {
                    FileInputStream fis = new FileInputStream(song);
                    ap = new Player(fis);
                    ap.play(u);
                }
                catch(Exception ex)
                {
                }
            }
        }
        public int getPos()
        {
            return a;
        }
        public void close()
        {
            a = Integer.MAX_VALUE-ap.getPosition();
            ap.close();
            isRun = false;
            thread = null;
        }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: MP3 problems

    When comparing Strings use the equals method. The == operator is for primitives.

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

    Default Re: MP3 problems

    im not comparing strings... im getting an int... i probably shoulda said this earlier, but everything of interest is under the mp class...

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: MP3 problems

    String a = e.getActionCommand();
    if(a=="Load")
    here a is a String

  5. #5
    Junior Member
    Join Date
    May 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: MP3 problems

    oh, thats not the problem... thats just event handling... that works fine... but somehow the transferrance of the number of where the player needs to restart, is not getting sent back to the main program...

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: MP3 problems

    Have you tried debugging the code by adding printlns to show how the contents of the variable changes as it is set and what its value is when it is used?

  7. #7
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: MP3 problems

    Where did you find this player class?

    I mean, the package javazoom.jl.player.*.

    I can't figure out how to get it in JGrasp (my Eclispe got screwed up because I once imported all of the beta Java 7.0 into a program and it made it go really slow. Also, I tried fixing a problem that caused programs to stay at the same stage, whether or not I added new stuff, whether good or error, it still compiled at that same spot. So I tried to fix it and now NOTHING will work there. So I've been using JGrasp for a while. I also once was able to import acm package so I've done the package import thing before, but I've forgotten as it's been over a year since I did whatever I did to import it.)

    As for your code, I don't have the API for that package, but is there anything that will "unpause" it?

    I mean, will it not pause at all or will it pause but then never restart? You just said that the pause thing "didn't work".
    Last edited by javapenguin; July 15th, 2011 at 12:25 PM.

Similar Threads

  1. 2 problems...
    By Day2Day in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 22nd, 2010, 02:51 PM
  2. [SOLVED] Have a few odd problems.
    By javapenguin in forum What's Wrong With My Code?
    Replies: 18
    Last Post: October 19th, 2010, 06:08 PM
  3. rmi problems
    By deepthought in forum Java SE APIs
    Replies: 7
    Last Post: September 7th, 2010, 07:25 PM
  4. How to compile and run java program?
    By ebalari56 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: September 27th, 2009, 09:33 PM
  5. If you have any .NET problems
    By antony_t in forum The Cafe
    Replies: 1
    Last Post: August 26th, 2009, 10:49 AM