0

I've been trying to code a music player (using java and JFugue) in Eclipse Mars, however, everytime I run the program it terminates after a few seconds with no output. Additionally, when I go to the "run as" drop down menu, there are no options. I feel like the problem is probably glaringly obvious, but I'm a bit new to java so without an error message I'm lost.

This is the main class

import java.util.Scanner;
import java.awt.*;
import java.awt.event.*;
import javax.sound.midi.MidiUnavailableException; 
import javax.swing.*;

import java.io.IOException;

import org.jfugue.player.Player;
import org.jfugue.pattern.Pattern;


public class MusicPlayerMain
{
   public static String songTitle;
   public static JButton play = new JButton("play");
public static JButton back = new JButton("back");
public static JButton pause = new JButton("pause");
public static JButton forward = new JButton("forward");
public static JLabel label;
public static JComboBox dropDown;
public static JButton repeat = new JButton("repeat");
public static JButton selector;

public static void main(String[] args)
{
    //-----------------------------------------------------------------
    //starts up window

    JFrame frame = new JFrame ("MusicPlayer");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLayout(new GridLayout(3 , 1, 0, 50));

    JPanel panelTop = new JPanel(new GridLayout(2, 2, 20, 50));
    panelTop.add(new JLabel("Welcome to MusicPlayer!" + "\n" + "Please select a song!"));
    String [] songs = {"WinterTime Ladybug Dance", "Song2", "Song3", "Song4", "Song5", "Song6", "Song7", "Song8", "Song9", "Song10"};
    dropDown = new JComboBox();
    for (int count1 = 0; count1 < 10; count1++)
    {
        dropDown.addItem(songs[count1]);
    }
    panelTop.add(dropDown);
    selector = new JButton("Select");
    panelTop.add(selector);


    JPanel panelMiddle = new JPanel(new GridLayout(2, 1, 20, 50));
    panelMiddle.setSize(500,200);

    JPanel panelBottom = new JPanel(new GridLayout(1, 4, 20, 50));
    panelBottom.setSize(500,200);

    JPanel panelBottom2 = new JPanel(new GridLayout(1,1,20,50));
    panelBottom2.setSize(500,100);

    frame.add(panelTop);
    frame.add(panelMiddle);
    frame.add(panelBottom);
    frame.add(panelBottom2);

    //-----------------------------------------------------------------
    //sets an action for the select button (displays song menu and plays song)

    selector.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent event)
    {
        label = new JLabel();
        //make it so that label changes to song title
        String titleOfSong = (String)dropDown.getSelectedItem();
        label.setText(titleOfSong);

        panelMiddle.add(label);
        JProgressBar songProgress = new JProgressBar();
        panelMiddle.add(songProgress); //shows progress of song with timer to show duration of song

        panelBottom.add(back);//the song's array list index -1
        panelBottom.add(play);//a player class
        panelBottom.add(pause);//ManagedPlayer class
        panelBottom.add(forward);//the song's array list index +1

        panelBottom2.add(repeat);//repeats a playlist, song, or returns to normal

        Music musical = new Music(selector, dropDown, back, play, pause, forward, label, repeat, songTitle);

        try
        {
            musical.PutSongsIntoArrayList();
        }
        catch(IOException ioe)
        {
            System.out.println("No music found!");
        }

        frame.pack();

        musical.playTheMusic(dropDown, songTitle);
    }
    });

    //-----------------------------------------------------------------------
    //navigates the music

    play.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent event)
    {
        Music musical = new Music(selector, dropDown, back, play, pause, forward, label, repeat, songTitle);
        musical.playTheMusic(dropDown, songTitle);
    }
    });

    pause.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent event)
    {
        try
    {
        Music musical = new Music(selector, dropDown, back, play, pause, forward, label, repeat, songTitle);

        musical.pauseTheMusic(play);
    }
    catch(MidiUnavailableException md)
    {
        System.out.println("no song found!");
    }
    }
    });

    back.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent event)
    {
        Music musical = new Music(selector, dropDown, back, play, pause, forward, label, repeat, songTitle);
        musical.goBack();
    }
    });

    forward.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent event)
    {
        Music musical = new Music(selector, dropDown, back, play, pause, forward, label,repeat,songTitle);
        musical.goForward();
    }
    });

    Music musical = new Music(selector, dropDown, back, play, pause, forward, label, repeat, songTitle);
    musical.keepGoing();

    repeat.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent event)
    {
        Music musical = new Music(selector, dropDown, back, play, pause, forward, label,repeat,songTitle);
        musical.setToRepeat();
    }
    });
}

}

This is the class that supplies the methods to navigate the music

import java.awt.*;
import javax.swing.*;
import org.jfugue.player.*;
import org.jfugue.pattern.*;
import javax.sound.midi.MidiUnavailableException;
import java.util.Scanner;
import java.io.File;
import java.util.ArrayList;
import java.io.IOException;
import java.util.Iterator;

public class Music
{
 //take out the ones I don't need later
 private JButton slctr;
private JComboBox drpDwn;
private JButton bck;
private JButton ply;
private JButton pse;
private JButton frwrd;
private JButton rpt;
private JLabel lbl;
private String title;
String tokens;

//declaring classes
ArrayList <String> TheTokens = new <String> ArrayList();
File musicFile = new File("Tokens.txt");
Pattern pat = new Pattern(tokens);
Player plays = new Player();

public Music(JButton s, JComboBox d, JButton b, JButton p, JButton ps, JButton f, JLabel l, JButton r, String t) //constructor
{
    slctr = s;
    drpDwn = d;
    bck = b;
    ply = p;
    pse = ps;
    frwrd = f;
    lbl = l;
    rpt = r;
    title = t;
}

public void PutSongsIntoArrayList() throws IOException //puts the song tokens into an ArrayList so it can be manipulated
{
    Scanner fileScanner = new Scanner(musicFile);

    while(fileScanner.hasNext())
    {
        String readIt = fileScanner.nextLine();
        Scanner scanLine = new Scanner(readIt);
        scanLine.useDelimiter(",");
        title = scanLine.next();
        tokens = scanLine.nextLine();
        TheTokens.add(title);
        TheTokens.add(tokens);
        scanLine.close();
    }
    fileScanner.close();
}

public void playTheMusic(JComboBox d, String title)//used for when song is selected, also when song is stopped and must be played again
{ 
    String selectedSong = (String)d.getSelectedItem();
    Iterator itr = TheTokens.iterator();
    while(itr.hasNext())
    {
        if (selectedSong.equals(title))
        {
            plays.play(pat);
        }
    }
}

public void pauseTheMusic(JButton ply) throws MidiUnavailableException //pauses the currently playing song
{
    plays.getManagedPlayer().pause();

    while(!ply.getModel().isPressed()) //allows the method to pause the song indefinitely. Will only end once the song is resumed
    {
        if(ply.getModel().isPressed())
        {
            plays.getManagedPlayer().resume(); //resumes the song
        }
    }
}

public void goBack() //allows the user to listen to the previous song mid-song
{
    int index = TheTokens.indexOf(title)-1;
    TheTokens.get(index);
    plays.play(pat);
}

public void goForward() //allows the user to listen to the next song mid-song
{
    int index = TheTokens.indexOf(title)+1;
    TheTokens.get(index);
    plays.play(pat);
}

public void keepGoing() //causes the next song to play after the first one finishes.
    //NOTE: is similar to musical.goForward() but instead of proceeding to next song when a button is pressed,
    //this method proceeds to the next song after the first song finishes
{
    boolean NextSong = plays.getManagedPlayer().isFinished();
    if(NextSong == true)
    {
        int index = TheTokens.indexOf(title)+1;
        TheTokens.get(index);
        plays.delayPlay(2000, pat);
        plays.play(pat);
    }
}

public void setToRepeat()//cycles through repeat playlist, repeat song, and normal
    //in main class, pressing button will change text on button to show what part of cycle player is in
{
    Iterator itr = TheTokens.iterator();
    while(!rpt.getModel().isPressed())
    {
        if(!itr.hasNext())
        {
            String inDEX = TheTokens.get(0);
        }
        if(rpt.getModel().isPressed())
        {
            while(!rpt.getModel().isPressed())
            {
                pat.repeat(1);
                plays.play(pat);

                if(rpt.getModel().isPressed())
                {
                    break;
                }
            }
        }
    }
}
}

1 Answer 1

2

Try adding frame.setVisible(true) at the end of your main method.

Sign up to request clarification or add additional context in comments.

1 Comment

oh whoops, turns out that's what the problem was, that and I needed to pack it too. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.