Пример #1
0
 @ActionDoc(text = "plays a sound from the sounds folder")
 public static void playSound(
     @ParamDoc(name = "filename", text = "the filename with extension") String filename) {
   try {
     InputStream is = new FileInputStream(SOUND_DIR + File.separator + filename);
     if (filename.toLowerCase().endsWith(".mp3")) {
       Player player = new Player(is);
       playInThread(player);
     } else {
       AudioInputStream ais = AudioSystem.getAudioInputStream(is);
       Clip clip = AudioSystem.getClip();
       clip.open(ais);
       playInThread(clip);
     }
   } catch (FileNotFoundException e) {
     logger.error("Cannot play sound '{}': {}", new String[] {filename, e.getMessage()});
   } catch (JavaLayerException e) {
     logger.error("Cannot play sound '{}': {}", new String[] {filename, e.getMessage()});
   } catch (UnsupportedAudioFileException e) {
     logger.error(
         "Format of sound file '{}' is not supported: {}",
         new String[] {filename, e.getMessage()});
   } catch (IOException e) {
     logger.error("Cannot play sound '{}': {}", new String[] {filename, e.getMessage()});
   } catch (LineUnavailableException e) {
     logger.error("Cannot play sound '{}': {}", new String[] {filename, e.getMessage()});
   }
 }
Пример #2
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);
    }
  }