Exemplo n.º 1
0
  /** Crea Ftes new form PanelFilm */
  public PanelFilm(URL mediaUrl) throws IOException {
    initComponents();
    setLayout(new BorderLayout());
    Manager.setHint(Manager.LIGHTWEIGHT_RENDERER, true);

    drawFilm = new JPanel();

    try {

      mediaPlayer = Manager.createRealizedPlayer(mediaUrl);
      System.out.println(Manager.createRealizedPlayer(mediaUrl).toString());
      video = mediaPlayer.getVisualComponent();
      System.out.println(mediaPlayer.getVisualComponent());
      controls = mediaPlayer.getControlPanelComponent();

      if (video != null) {
        drawFilm.add(video, BorderLayout.CENTER);
      }

      if (controls != null) {
        drawFilm.add(controls, BorderLayout.SOUTH);
      }
      this.setVisible(false);
      drawFilm.setVisible(true);

      mediaPlayer.start();

    } catch (NoPlayerException noPlayerException) {
      noPlayerException.printStackTrace();
    } catch (CannotRealizeException cannotRealizeException) {
      cannotRealizeException.printStackTrace();
    }
  }
 // Play starts the track from the beginning or from the last played part
 public void play() {
   try {
     player.start();
   } catch (Exception e) {
     e.printStackTrace();
   }
 }
Exemplo n.º 3
0
  public MediaPanel(URL mediaURL) {
    setLayout(new BorderLayout()); // use a BorderLayout

    // Use lightweight components for Swing compatibility
    Manager.setHint(Manager.LIGHTWEIGHT_RENDERER, true);

    try {
      // create a player to play the media specified in the URL
      Player mediaPlayer = Manager.createRealizedPlayer(mediaURL);

      // get the components for the video and the playback controls
      Component video = mediaPlayer.getVisualComponent();
      Component controls = mediaPlayer.getControlPanelComponent();

      if (video != null) add(video, BorderLayout.CENTER); // add video component

      if (controls != null) add(controls, BorderLayout.SOUTH); // add controls

      mediaPlayer.start(); // start playing the media clip
    } // end try
    catch (NoPlayerException noPlayerException) {
      System.err.println("No media player found");
    } // end catch
    catch (CannotRealizeException cannotRealizeException) {
      System.err.println("Could not realize media player");
    } // end catch
    catch (IOException iOException) {
      System.err.println("Error reading from the source");
    } // end catch
  } // end MediaPanel constructor
Exemplo n.º 4
0
  // Constructor
  public MediaPlayer(URL mediauUrl) {

    setLayout(new BorderLayout()); // use a BorderLayout

    // Use lightweight components for Swing compatibility
    Manager.setHint(Manager.LIGHTWEIGHT_RENDERER, true);

    try {
      // create a player to play the media specified in the URL
      Player mediaPlayer = Manager.createRealizedPlayer(mediauUrl);

      // get the components for the video and the playback controls
      Component video = mediaPlayer.getVisualComponent();
      Component control = mediaPlayer.getControlPanelComponent();

      if (video != null) {
        add(video, BorderLayout.CENTER); // place the video component in the panel
      }

      if (control != null) {
        add(control, BorderLayout.SOUTH); // place the control component in  the panel
      }
      mediaPlayer.start(); // start playing the media clip
    } catch (NoPlayerException noPlayerException) {
      System.err.println("No media player found: " + noPlayerException);
    } catch (CannotRealizeException cannotRealizeException) {
      System.err.println("Could not realize media player: " + cannotRealizeException);
    } catch (IOException iOException) {
      System.err.println("Error reading from the source: " + iOException);
    }
  }
Exemplo n.º 5
0
  public Capture_video() {
    try {
      // List out available Devices to Capture Video.
      Vector<CaptureDeviceInfo> list = CaptureDeviceManager.getDeviceList(format);
      System.out.println(CaptureDeviceManager.getDeviceList(null).toString());

      // Iterating list
      for (CaptureDeviceInfo temp : list) {
        // Checking whether the current device supports VfW
        // VfW = Video for Windows
        if (temp.getName().startsWith("vfw:")) {

          System.out.println("Found : " + temp.getName().substring(4));
          // Selecting the very first device that supports VfW
          cam = temp;
          System.out.println("Selected : " + cam.getName().substring(4));
          break;
        }
      }

      System.out.println("Put it on work!...");
      // Getting the MediaLocator for Selected device.
      // MediaLocator describes the location of media content
      locator = cam.getLocator();

      if (locator != null) {

        // Create a Player for Media Located by MediaLocator
        player = Manager.createRealizedPlayer(locator);

        if (player != null) {

          // Starting the player
          player.start();

          // Creating a Frame to display Video

          f.setTitle("Test Webcam");
          f.setLayout(new BorderLayout());
          // Adding the Visual Component to display Video captured by Player
          // from URL provided by MediaLocator
          f.add(player.getVisualComponent(), BorderLayout.CENTER);
          f.pack();
          f.setAlwaysOnTop(true);
          f.setVisible(true);
        }
      }

    } catch (Exception e) {
      e.printStackTrace();
    }
  }
Exemplo n.º 6
0
  private void initWebcam() {
    Vector videoDevices = CaptureDeviceManager.getDeviceList(new VideoFormat(null));
    if (videoDevices.size() > 0) {
      webcam = (CaptureDeviceInfo) videoDevices.get(0);
      Format[] formats = webcam.getFormats();
      Format selectedFormat = null;

      try {
        for (Format f : formats) {
          if (f.toString().contains("640") && f.toString().contains("480")) {
            selectedFormat = f;
            break;
          }
        }
      } catch (Exception e) {
        logger.error(
            "Failed to get required webcam resolution (640x480). Taking default format: "
                + e.getMessage());
        selectedFormat = formats[0];
      }

      try {
        webcamPlayer = Manager.createRealizedPlayer(webcam.getLocator());

        FormatControl fc =
            (FormatControl) webcamPlayer.getControl("javax.media.control.FormatControl");
        System.out.println("Selected webcam format: " + selectedFormat.toString());
        fc.setFormat(selectedFormat);

        webcamPlayer.start();

        webcamAvailable = true;

        Timer timer = new Timer();
        timer.schedule(
            new TimerTask() {
              @Override
              public void run() {
                frameGrabber =
                    (FrameGrabbingControl)
                        webcamPlayer.getControl("javax.media.control.FrameGrabbingControl");
              }
            },
            2500);
      } catch (Exception e) {
        logger.error("Failed to get webcam feed: " + e.getMessage());
      }
    } else {
      logger.error("No webcam available");
    }
  }
Exemplo n.º 7
0
  public Webcam(Player _player) {
    f = new Frame("SwingCapture");

    setLayout(new BorderLayout());
    setSize(640, 480);
    this.player = _player;
    Component comp;

    try {
      if ((comp = player.getVisualComponent()) != null) {
        add(comp, BorderLayout.CENTER);
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
    f.addWindowListener(
        new WindowAdapter() {
          public void windowClosing(WindowEvent e) {
            f.dispose();
            // playerclose();
            // System.exit(0);
          }
        });

    f.add("Center", this);
    f.pack();
    f.setSize(new Dimension(640, 480));
    f.setVisible(true);

    FormatControl formatControl =
        (FormatControl) player.getControl("javax.media.control.FormatControl");
    // player.stop();
    Component co = formatControl.getControlComponent();
    if (co != null) {
      player.stop();
      JDialog d = new JDialog(f, "Format Control", true);
      f.add(co);
      f.pack();
      f.setLocationRelativeTo(f);
      f.setVisible(true);
      f.dispose();
      player.start();
    }
  }
  /** @param args */
  public static void main(String[] args) {
    String url = "rtp://192.168.1.1:22224/audio/16";

    MediaLocator mrl = new MediaLocator(url);

    // Create a player for this rtp session
    Player player = null;
    try {
      player = Manager.createPlayer(mrl);
    } catch (NoPlayerException e) {
      e.printStackTrace();
      System.exit(-1);
    } catch (MalformedURLException e) {
      e.printStackTrace();
      System.exit(-1);
    } catch (IOException e) {
      e.printStackTrace();
      System.exit(-1);
    }

    if (player != null) {
      System.out.println("Player created.");
      player.realize();
      // wait for realizing
      while (player.getState() != Controller.Realized) {
        try {
          Thread.sleep(10);
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
      }
      System.out.println("Starting player");
      player.start();
    } else {
      System.err.println("Player doesn't created.");
      System.exit(-1);
    }

    System.out.println("Exiting.");
  }
  public static void main(String args[]) throws Exception {

    // Take the path of the audio file from command line

    File f = new File("C:\\Users\\Larry\\AppData\\Local\\Temp\\jgoogle_tts-588881786930740869.mp3");
    // Create a Player object that realizes the audio
    final Player p = Manager.createRealizedPlayer(f.toURI().toURL());
    // Start the music
    p.start();
    // Create a Scanner object for taking input from cmd
    Scanner s = new Scanner(System.in);

    // Read a line and store it in st

    String st = s.nextLine();

    // If user types 's', stop the audio

    if (st.equals("s")) {

      p.stop();
    }
  }
Exemplo n.º 10
0
  public void capture(String device) {

    CaptureDeviceInfo deviceInfo = CaptureDeviceManager.getDevice(device);
    System.out.println("-----------------------------------------------------------");
    System.out.println("CaptureDevice Name is " + deviceInfo.getName());
    System.out.println("-----------------------------------------------------------");
    System.out.println("CaptureDevice is " + deviceInfo);
    System.out.println("-----------------------------------------------------------");
    Format[] formatsSupported = deviceInfo.getFormats();
    System.out.println("Supports " + formatsSupported.length + " formats");
    for (int findex = 0; findex < formatsSupported.length; findex++) {
      System.out.println("Unique encoding name is " + formatsSupported[findex].getEncoding());
      System.out.println("Format attributes are " + formatsSupported[findex]);
    }
    System.out.println("-----------------------------------------------------------");
    System.out.println("Media Locator is " + deviceInfo.getLocator());
    System.out.println("***********************************************************");
    try {
      player = Manager.createPlayer(deviceInfo.getLocator());
    } catch (java.io.IOException e) {
      System.out.println("IOException");
    } catch (javax.media.NoPlayerException npe) {
      System.out.println("NoPlayerException");
    }
    player.addControllerListener(this);

    System.out.println("About to call start on player");
    player.realize();
    waitForState(player.Realized);
    addVisualElements();
    setVisible(true);
    player.start();
    System.out.println("Called start on player");
    waitForState(player.Started);
    System.out.println("Player started");
  }
 /**
  * Casts the Controller to a Player, calls start(), and blocks the current thread until the player
  * is Started.
  *
  * @return boolean indicating whether the transition was successful.
  * @exception ClassCastException If the Controller is not a Player
  */
 public boolean blockingStart() {
   setState(Controller.Started);
   Player player = (Player) controller;
   player.start();
   return waitForState();
 }
  /**
   * 播放歌曲
   *
   * @param songInfo
   */
  private void playInfoMusic(SongInfo msongInfo) {
    this.songInfo = msongInfo;
    if (songInfo == null) {

      SongMessage msg = new SongMessage();
      msg.setType(SongMessage.SERVICEERRORMUSIC);
      msg.setErrorMessage(SongMessage.ERRORMESSAGEPLAYSONGNULL);
      ObserverManage.getObserver().setMessage(msg);

      return;
    }
    File songFile = new File(songInfo.getFilePath());

    if (!songFile.exists()) {
      logger.error("播放文件不存在!");
      // 下一首
      SongMessage songMessage = new SongMessage();
      songMessage.setType(SongMessage.NEXTMUSIC);
      ObserverManage.getObserver().setMessage(songMessage);

      return;
    }

    try {
      if (mediaPlayer == null) {
        // mediaPlayer = new MediaPlayer();
        // // mediaPlayer.setDataSource(songInfo.getFilePath());
        // //
        // // float playedRate = (float) songInfo.getPlayProgress()
        // // / songInfo.getDuration();
        // // mediaPlayer.seekTo(playedRate);
        // mediaPlayer.setMediaLocator(new MediaLocator(songInfo
        // .getFilePath()));
        File file = new File(songInfo.getFilePath());
        mediaPlayer = Manager.createRealizedPlayer(new MediaLocator(file.toURL()));

        initVolume();

        mediaPlayer.prefetch(); // 获取媒体数据

        double totalTime = mediaPlayer.getDuration().getSeconds();
        double rate = songInfo.getPlayProgress() * 1.00 / songInfo.getDuration();
        mediaPlayer.setMediaTime(new Time(totalTime * rate));
        mediaPlayer.prefetch();
        mediaPlayer.start();
      }
    } catch (Exception e) {
      logger.error("不能播放此文件:" + songInfo.getFilePath());
      e.printStackTrace();

      SongMessage songMessage = new SongMessage();
      songMessage.setType(SongMessage.NEXTMUSIC);
      ObserverManage.getObserver().setMessage(songMessage);
    }
    if (mediaPlayer != null) {
      mediaPlayer.addControllerListener(
          new ControllerListener() {

            @Override
            public void controllerUpdate(ControllerEvent e) {
              // 当媒体播放结束时
              if (e instanceof EndOfMediaEvent) {
                mediaPlayer.setMediaTime(new Time(0));
                mediaPlayer = null;
                SongMessage songMessage = new SongMessage();
                songMessage.setType(SongMessage.NEXTMUSIC);
                ObserverManage.getObserver().setMessage(songMessage);
              }
            }
          });
      // mediaPlayer.setOnCompletionListener(new OnCompletionListener() {
      //
      // @Override
      // public void onProgressChanged() {
      //
      // float playedRate = mediaPlayer.getPlayedRate();
      //
      // long playProgress = (long) (songInfo.getDuration() * playedRate);
      //
      // if (songInfo != null) {
      // songInfo.setPlayProgress(playProgress);
      // SongMessage msg = new SongMessage();
      // msg.setSongInfo(songInfo);
      // msg.setType(SongMessage.SERVICEPLAYINGMUSIC);
      // ObserverManage.getObserver().setMessage(msg);
      // }
      // }
      //
      // @Override
      // public void onCompletion() {
      //
      // SongMessage songMessage = new SongMessage();
      // songMessage.setType(SongMessage.NEXTMUSIC);
      // ObserverManage.getObserver().setMessage(songMessage);
      // }
      // });
    }

    if (lrcThread == null) {
      lrcThread = new Thread(new LrcRunable());
      lrcThread.start();
    }
  }
Exemplo n.º 13
0
  public void actionPerformed(ActionEvent e) {

    if (e.getSource() == play_time) {
      dure.setText(toString(lecteur.getMediaTime()) + " - " + toString(lecteur.getDuration()));

      deplacement.setValue(
          (int) (lecteur.getMediaTime().getSeconds() / lecteur.getDuration().getSeconds() * 100));

      if (lecteur.getMediaTime().getSeconds() == lecteur.getDuration().getSeconds()) {
        lecteur.stop();
        lecteur.close();
        lecteur = null;

        deplacement.setValue(0);
        dure.setText("00:00 - " + toString(lecteur.getDuration()));
        play_time.stop();
      }
    }

    if (e.getSource() == open) {
      fc = new JFileChooser();
      fc.setAcceptAllFileFilterUsed(false);
      fc.showOpenDialog(this);

      if (fc.getDialogType() == JFileChooser.APPROVE_OPTION) {
        fichier = fc.getSelectedFile();
        name.setText(fichier.getName().substring(0, (int) fichier.getName().length() - 4));
      }
    }

    if (e.getSource() == play) {
      try {
        if (fichier != null) {
          if (lecteur == null) {
            lecteur = Manager.createPlayer(fichier.toURL());
            lecteur.start();
            play_time.start();
          }

          if (enPause == true) {
            lecteur.start();
            play_time.start();
            enPause = false;
          }
        }
      } catch (Exception ex) {
        ex.printStackTrace();
      }
    }

    if (e.getSource() == pause) {
      if (enPause == false && lecteur != null) {
        lecteur.stop();
        play_time.stop();
        enPause = true;
      }
    }

    if (e.getSource() == stop) {
      if (enPause == false && lecteur != null) {
        lecteur.stop();
        lecteur.close();
        lecteur = null;

        play_time.stop();
        dure.setText("00:00 - 00:00");
        deplacement.setValue(0);
      }
    }
  }
  public WebcamCaptureAndFadePanel(String saveDir, String layout) {

    System.out.println("Using " + saveDir + " as directory for the images.");
    saveDirectory = saveDir;

    getImages();
    images_used = new ArrayList<Integer>();
    images_lastadded = new ArrayList<Integer>();
    images_nevershown = new ArrayList<Integer>();

    Vector devices = (Vector) CaptureDeviceManager.getDeviceList(null).clone();
    Enumeration enumeration = devices.elements();
    System.out.println("- Available cameras -");
    ArrayList<String> names = new ArrayList<String>();
    while (enumeration.hasMoreElements()) {
      CaptureDeviceInfo cdi = (CaptureDeviceInfo) enumeration.nextElement();
      String name = cdi.getName();
      if (name.startsWith("vfw:")) {
        names.add(name);
        System.out.println(name);
      }
    }

    // String str1 = "vfw:Logitech USB Video Camera:0";
    // String str2 = "vfw:Microsoft WDM Image Capture (Win32):0";
    if (names.size() == 0) {
      JOptionPane.showMessageDialog(
          null,
          "Ingen kamera funnet. " + "Du må koble til et kamera for å kjøre programmet.",
          "Feil",
          JOptionPane.ERROR_MESSAGE);
      System.exit(0);
    } else if (names.size() > 1) {

      JOptionPane.showMessageDialog(
          null,
          "Fant mer enn 1 kamera. " + "Velger da:\n" + names.get(0),
          "Advarsel",
          JOptionPane.WARNING_MESSAGE);
    }

    String str2 = names.get(0);
    di = CaptureDeviceManager.getDevice(str2);
    ml = di.getLocator();

    try {
      player = Manager.createRealizedPlayer(ml);
      formatControl = (FormatControl) player.getControl("javax.media.control.FormatControl");

      /*
      Format[] formats = formatControl.getSupportedFormats();
      for (int i=0; i<formats.length; i++)
      	System.out.println(formats[i].toString());
      */

      player.start();
    } catch (javax.media.NoPlayerException e) {
      JOptionPane.showMessageDialog(
          null,
          "Klarer ikke å starte" + " programmet pga. feil med kamera. Sjekk at det er koblet til.",
          "IOException",
          JOptionPane.ERROR_MESSAGE);
      System.exit(0);
    } catch (Exception e) {
      e.printStackTrace();
      System.exit(0);
    }

    /*
     * Layout
     *
     * Add
     * - comp
     * - imagepanels
     */

    if (layout.equals("1024v2")) {
      layout1024v2();
    } else if (layout.equals("1280")) {
      layout1280();
    } else {
      layout1024();
    }

    // Capture Window
    if (captureWindow) {
      cw = new JFrame("Capture from webcam");
      cw.setAlwaysOnTop(true);
      cw.setSize(sizeCaptureWindow_x, sizeCaptureWindow_y);
      cw.addKeyListener(new captureWindowKeyListner());
      cw.setUndecorated(true);

      // Add webcam
      if ((comp = player.getVisualComponent()) != null) {
        cw.add(comp);
      }

      // Add panel to window and set location of window
      cw.setLocation(cwLocation_x, cwLocation_y);
    }

    // Text window
    cwText = new rotatedText("");

    /*
     * Timer for update
     */
    Timer thread = new Timer();
    thread.schedule(new frameUpdateTask(), 0, (1000 / fps));
  }