Exemplo n.º 1
0
 public static void playSound(String filename) {
   try {
     FileInputStream fis = new FileInputStream("sounds/" + filename);
     try {
       Player p = new Player(fis);
       p.play();
     } catch (JavaLayerException e) {
     }
   } catch (FileNotFoundException e) {
   }
 }
 //    ShoutcastStreamer(String urlAsString) {
 //        try {
 //            URL url = new URL(urlAsString);
 //            URLConnection uc = url.openConnection();
 //            uc.addRequestProperty("Icy-MetaData", "1");
 //
 //            //get all headers
 //            Map<String, List<String>> map = uc.getHeaderFields();
 //            for (Map.Entry<String, List<String>> entry : map.entrySet()) {
 //                System.out.println("Key : " + entry.getKey()
 //                        + " ,Value : " + entry.getValue());
 //            }
 //
 //            System.out.println(map.size());
 //            //metaint = Integer.parseInt(uc.getHeaderField("icy-metaint"));
 //            metaint = 32768;
 //            System.out.println(metaint);
 //
 //            InputStream in = uc.getInputStream();
 //            byte[] buffer = new byte[metaint];
 //            for (int i = 0; i < buffer.length; i++) {
 //                buffer[i] = (byte) in.read();
 //            }
 //            int metaLength = in.read() * 16;
 //            String metaData = new String();
 //            for (int i = 0; i < metaLength; i++) {
 //                metaData = metaData.concat(String.valueOf((char) in.read()));
 //            }
 //            System.out.println(metaData);
 //
 //        } catch (IOException ex) {
 //            Logger.getLogger(ShoutcastStreamer.class.getName()).log(Level.SEVERE, null, ex);
 //        }
 //    }
 private void playInputStream(InputStream stream) {
   try {
     if (player != null) {
       player.close();
     }
     player = new Player(stream);
     player.play();
   } catch (Exception ex) {
     Logger.getLogger(ShoutcastPlayer.class.getName()).log(Level.SEVERE, null, ex);
   }
 }
Exemplo n.º 3
0
 /** Runs the thread. */
 public void run() {
   try {
     music.play();
   } catch (Exception e) {
     e.printStackTrace();
   }
 }
Exemplo n.º 4
0
 /** Returns the song time. */
 public String getTime() {
   int position = music.getPosition();
   position /= 1000;
   int seconds = position / 60;
   int minutes = position / 60 / 60;
   String time = minutes + ":" + seconds;
   return time;
 }
Exemplo n.º 5
0
  private static void playSoundRunnable(final String name) {
    try {
      String song = "sounds/" + name + ".mp3";
      FileInputStream input = new FileInputStream(song);
      BufferedInputStream bufferedInput = new BufferedInputStream(input);

      player = new Player(bufferedInput);
      player.play();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
  /** Starts the background music */
  private void playMe() {
    try {

      File file = new File(Resource.SOUND_BACKGROUDN);
      FileInputStream fis = new FileInputStream(file);
      BufferedInputStream bis = new BufferedInputStream(fis);
      player = new Player(bis);
      player.play();

    } catch (Exception e) {
      LOGGER.error(e.getMessage(), e);
    }
  }
Exemplo n.º 7
0
  public void songManager() {
    UI ui = new UI();
    int choice = ui.getChoice();
    Play p = new Play(this);

    while (choice != 5) {
      switch (choice) {
        case 1:
          sortByArtist();
          print();
          break;
        case 2:
          sortByTitle();
          print();
          break;
        case 3:
          Scanner sc = new Scanner(new InputStreamReader(System.in));
          int songChoice = 0;
          while (songChoice < 1 || songChoice > size()) {
            System.out.println("Enter Song Choice: ");
            sortByArtist();
            print();
            songChoice = sc.nextInt();
          }
          try {
            p.playSong(songChoice, this);
          } catch (JavaLayerException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
          }
          break;
        case 4:
          if (player != null) {
            player.close();
          }
          break;
        default:
          break;
      }
      choice = ui.getChoice();
      if (choice == 5) break;
    }
  }
Exemplo n.º 8
0
  @ActionDoc(text = "plays an audio stream from an url")
  public static synchronized void playStream(
      @ParamDoc(name = "url", text = "the url of the audio stream") String url) {
    if (streamPlayer != null) {
      // if we are already playing a stream, stop it first
      streamPlayer.close();
      streamPlayer = null;
    }
    if (url == null) {
      // the call was only for stopping the currently playing stream
      return;
    }
    try {
      if (url.toLowerCase().endsWith(".m3u")) {
        InputStream is = new URL(url).openStream();
        String urls = IOUtils.toString(is);
        for (String line : urls.split("\n")) {
          if (!line.isEmpty() && !line.startsWith("#")) {
            url = line;
            break;
          }
        }
      } else if (url.toLowerCase().endsWith(".pls")) {
        InputStream is = new URL(url).openStream();
        String urls = IOUtils.toString(is);
        for (String line : urls.split("\n")) {
          if (!line.isEmpty() && line.startsWith("File")) {
            Matcher matcher = plsStreamPattern.matcher(line);
            if (matcher.find()) {
              url = matcher.group(1);
              break;
            }
          }
        }
      }
      URL streamUrl = new URL(url);
      URLConnection connection = streamUrl.openConnection();
      InputStream is = null;
      if (connection.getContentType().equals("unknown/unknown")) {
        // Java does not parse non-standard headers used by SHOUTCast
        int port = streamUrl.getPort() > 0 ? streamUrl.getPort() : 80;
        // Manipulate User-Agent to receive a stream
        shoutCastSocket = new Socket(streamUrl.getHost(), port);

        OutputStream os = shoutCastSocket.getOutputStream();
        String user_agent = "WinampMPEG/5.09";
        String req =
            "GET / HTTP/1.0\r\nuser-agent: "
                + user_agent
                + "\r\nIcy-MetaData: 1\r\nConnection: keep-alive\r\n\r\n";
        os.write(req.getBytes());
        is = shoutCastSocket.getInputStream();
      } else {
        is = streamUrl.openStream();
      }
      if (is != null) {
        Player player = new Player(is);
        streamPlayer = player;
        playInThread(player);
      }
    } catch (JavaLayerException e) {
      logger.error("Cannot play stream '{}': JavaLayerException - {}", url, e.getMessage());
    } catch (MalformedURLException e) {
      logger.error("Cannot play stream '{}': MalformedURLException - {}", url, e.getMessage());
    } catch (IOException e) {
      logger.error("Cannot play stream '{}': {}", url, e);
    }
  }
Exemplo n.º 9
0
 /** Returns whether or not music is finished. */
 public boolean isComplete() {
   return music.isComplete();
 }
Exemplo n.º 10
0
 /** Stops the song. You cannot restart it afterwards. */
 public void stopSong() {
   music.close();
 }
 void stop() {
   player.close();
 }