SHARE
TWEET

Untitled

a guest Oct 14th, 2019 76 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package _03_jukebox;
  2.  
  3.  
  4.  
  5. import java.io.FileInputStream;
  6. import java.io.FileNotFoundException;
  7. import java.io.InputStream;
  8. import java.net.URL;
  9.  
  10. import javax.swing.Icon;
  11. import javax.swing.ImageIcon;
  12. import javax.swing.JLabel;
  13. import javax.swing.SwingUtilities;
  14.  
  15. import javazoom.jl.player.advanced.AdvancedPlayer;
  16.  
  17. /*   If you don't have javazoom.jar in your project, you can download it from here: http://bit.ly/javazoom
  18.  *   Right click your project and add it as a JAR (Under Java Build Path > Libraries).*/
  19.  
  20. public class Jukebox implements Runnable {
  21.  
  22.     public void run() {
  23.  
  24.         // 1. Find an mp3 on your computer or on the Internet.
  25.         // 2. Create a Song object for that mp3
  26.  
  27.         // 3. Play the Song
  28.  
  29.         /*
  30.          * 4. Create a user interface for your Jukebox so that the user can to
  31.          * choose which song to play. You can use can use a different button for
  32.          * each song, or a picture of the album cover. When the button or album
  33.          * cover is clicked, stop the currently playing song, and play the one
  34.          * that was selected.
  35.          */
  36.     }
  37.    
  38.    
  39.     /* Use this method to add album covers to your Panel. */
  40.     private JLabel loadImage(String fileName) {
  41.         URL imageURL = getClass().getResource(fileName);
  42.         Icon icon = new ImageIcon(imageURL);
  43.         return new JLabel(icon);
  44.     }
  45.  
  46. }
  47.  
  48. class Song {
  49.  
  50.     private int duration;
  51.     private String songAddress;
  52.     private AdvancedPlayer mp3Player;
  53.     private InputStream songStream;
  54.  
  55.     /**
  56.      * Songs can be constructed from files on your computer or Internet
  57.      * addresses.
  58.      *
  59.      * Examples: <code>
  60.      *      new Song("everywhere.mp3");     //from default package
  61.      *      new Song("/Users/joonspoon/music/Vampire Weekend - Modern Vampires of the City/03 Step.mp3");
  62.      *      new Song("http://freedownloads.last.fm/download/569264057/Get%2BGot.mp3");
  63.      * </code>
  64.      */
  65.     public Song(String songAddress) {
  66.         this.songAddress = songAddress;
  67.     }
  68.  
  69.     public void play() {
  70.         loadFile();
  71.         if (songStream != null) {
  72.             loadPlayer();
  73.             startSong();
  74.         } else
  75.             System.err.println("Unable to load file: " + songAddress);
  76.     }
  77.  
  78.     public void setDuration(int seconds) {
  79.         this.duration = seconds * 100;
  80.     }
  81.  
  82.     public void stop() {
  83.         if (mp3Player != null)
  84.             mp3Player.close();
  85.     }
  86.  
  87.     private void startSong() {
  88.         Thread t = new Thread() {
  89.             public void run() {
  90.                 try {
  91.                     if (duration > 0)
  92.                         mp3Player.play(duration);
  93.                     else
  94.                         mp3Player.play();
  95.                 } catch (Exception e) {
  96.                 }
  97.             }
  98.         };
  99.         t.start();
  100.     }
  101.  
  102.     private void loadPlayer() {
  103.         try {
  104.             this.mp3Player = new AdvancedPlayer(songStream);
  105.         } catch (Exception e) {
  106.         }
  107.     }
  108.  
  109.     private void loadFile() {
  110.         if (songAddress.contains("http"))
  111.             this.songStream = loadStreamFromInternet();
  112.         else
  113.             this.songStream = loadStreamFromComputer();
  114.     }
  115.  
  116.     private InputStream loadStreamFromInternet() {
  117.         try {
  118.             return new URL(songAddress).openStream();
  119.         } catch (Exception e) {
  120.             return null;
  121.         }
  122.     }
  123.  
  124.     private InputStream loadStreamFromComputer() {
  125.         try {
  126.             return new FileInputStream(songAddress);
  127.         } catch (FileNotFoundException e) {
  128.             return this.getClass().getResourceAsStream(songAddress);
  129.         }
  130.     }
  131. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top