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

Thread: HELP: Trying to get mp3 class to play song from playlist class

  1. #1
    Junior Member
    Join Date
    Aug 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default HELP: Trying to get mp3 class to play song from playlist class

    I have created a playlist class that ask the user to enter a song name and etc. What I want to do is have the mp3 class plays the song that the user has entered, but I don't know how. Any suggestions? Thanks in advance!
    Here are my codes:

    import java.util.Scanner;
     
    public class PlaylistDriver 
    {//Start of class
       public static Playlist list;
     
       public static void main(String[] args) 
       {//Start of main
     
          list = new Playlist();
     
          while(true) 
          {
             menu();
             processInput();
          }
     
       }
     
       public static void processInput() 
       {//Start of Process Input
          Scanner keyboard = new Scanner(System.in);
          String input = keyboard.nextLine().toLowerCase();
          switch(input.charAt(0)) 
          {//Start of switch
             case 'a':
                addSong();
                break;
             case 'i':
                printSongByIndex(); 
                break;		
             case 'n':
                removeSongByName();
                break;
             case 'p':
                printSongs();
                break;
             case 's':
                getSize();
                break;
             case 't':
                getTotalTime();
                break;
             case 'f':
                getFormattedTime();
                break;
             case 'c':
                clearSongs();
                break;	
             case 'q':
                System.exit(0);
                break;
             default:
                System.out.println("...........");
          }//End of switch
       }//End of Process Input
       public static void addSong() 
       {//Add Song 
          Scanner keyboard = new Scanner(System.in);
          System.out.print("Please enter a song name: ");
          String name = keyboard.nextLine();
          System.out.print("Please enter song artist: ");
          String artist = keyboard.nextLine();		
          System.out.print("Please enter an album: ");
          String album = keyboard.nextLine();
          System.out.print("Please enter genre: ");
          String genre = keyboard.nextLine();
          System.out.print("Please enter length of song: ");
          double length = keyboard.nextDouble();
     
     
          Song information = new Song(name, artist, album, length, genre);
          list.add(information);
       }//End of Add Song
     
       public static void printSongs()  
       {//Print Song Information 
          System.out.println(list);
       }//End of Print Song Information
     
       public static void printSongByIndex() 
       {//Print all songs
          System.out.println("Please enter index of song to view: ");
          Scanner keyboard = new Scanner(System.in);
          int information = keyboard.nextInt();
          System.out.print(list.get(information));	
       }
     
       public static void removeSongByName() 
       {//To remove song
          System.out.print("\nPlease enter the name of the song to remove: ");
          Scanner keyboard = new Scanner(System.in);
          String name = keyboard.nextLine();
          list.remove( name );
       }//End of remove song
     
       public static void getSize() 
       {//Get size of song
          System.out.println("\nTotal number of songs: " + list.size());
       }
     
       public static void getTotalTime()  
       {//Get Total Time
          System.out.println("Total Time: " + list.totalTime());
       }
     
       public static void getFormattedTime()
       {//Get formatted time
          System.out.println("Total Formatted Time:" + list.formattedTotalTime());
       }
     
       public static void clearSongs() 
       {//Clear Songs
          list.clear();
       }
     
       public static void menu() 
       {//Print Menu
          System.out.println("--------MP3 PLAYLIST--------");
          System.out.println("[A]dd a Song");
          System.out.println("[P]rint songs");
          System.out.println("Pr[i]nt song by index");
          System.out.println("Remove song by [n]ame");
          System.out.println("Get total [s]ize");
          System.out.println("Get total [t]ime");
          System.out.println("Get [f]ormatted time");
          System.out.println("[C]lear all songs");
          System.out.println("[Q]uit");
       }
    }//End of class

    import javax.swing.JButton;
    import javax.swing.JFrame;
    import java.awt.event.ActionListener;
    import java.awt.*;
    import java.awt.event.*;
    import java.io.FileInputStream;
    import java.io.BufferedInputStream;
    import javazoom.jl.player.advanced.AdvancedPlayer;	
     
    public class MP3PLAYER
    {//Start of class
       private AdvancedPlayer player;
     
       public static void main(String[] args) 
       {
          new MP3PLAYER().SETUP();
       }
     
       public void SETUP()
       {
          JFrame frame = new JFrame("MP3 PLAYER");
          frame.setSize(200, 100);
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          JButton btnplay = new JButton("Play");
          JButton btnstop = new JButton("Stop");
          btnplay.addActionListener(new playMusicListener());
          btnstop.addActionListener(new stopMusicListener());
          frame.getContentPane().add(BorderLayout.WEST, btnplay);
          frame.getContentPane().add(BorderLayout.EAST, btnstop);      
          frame.setVisible(true);
       }
     
     
       public class playMusicListener implements ActionListener
       {
          public void actionPerformed(ActionEvent a)
          {
             try
             {
                FileInputStream file = new FileInputStream("mp3 goes here");
                player = new AdvancedPlayer(file);
                player.play();
             }  
     
             catch(Exception e)
             {
                System.out.println(e);
             }
          }
       }
     
       public class stopMusicListener implements ActionListener
       {
          public void actionPerformed(ActionEvent a)
          {
             try
             {
                FileInputStream file = new FileInputStream("");  
                player = new AdvancedPlayer(file);
                player.stop();
             }
             catch(Exception ex)
             {
                System.out.println(ex);
             }
          }
       }
     
    }//End of class


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: HELP: Trying to get mp3 class to play song from playlist class

    Welcome to the forum! Thanks for taking the time to learn how to post code correctly. If you haven't already, please read this topic to learn other useful info for new members.

Similar Threads

  1. How do you play wma or mp3 files in java?
    By GoodbyeWorld in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 24th, 2014, 10:42 AM
  2. Java Song class
    By brandalicious78 in forum What's Wrong With My Code?
    Replies: 11
    Last Post: April 23rd, 2013, 09:41 AM
  3. need to make basic class and implementation class (base class without void main)
    By javanewbie101 in forum Object Oriented Programming
    Replies: 1
    Last Post: September 19th, 2012, 08:03 PM
  4. Creating a class to play sounds.
    By Archibold9 in forum Java Theory & Questions
    Replies: 8
    Last Post: December 21st, 2011, 06:14 PM
  5. How to take input dynamically and display?
    By abielm_007 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 29th, 2009, 05:51 PM