-1

1st Class :

jb4=new JButton("Select the File");
jb4.addActionListener( new ActionListener() {
  public void actionPerformed(ActionEvent ae) {
    final JFrame JF=new JFrame("Video Player");
    JFileChooser fc=new JFileChooser();
    fc.showOpenDialog(null);
    try {
       mediaURL = fc.getSelectedFile().toURI();
    } catch (MalformedURLException ex) {
      System.out.println(ex.getMessage());
    }
    final  MediaPlayer mp= new MediaPlayer(mediaURL) ;
    panel4.add(mp);
    }
    });
tabbedPane.addTab("Video Player", createImageIcon("images/VideoPlayer.png"), panel4,"This tab is for Video Player");
panel4.add(jb4);
jb=new JButton("Close Video Frame");
jb.addActionListener( new ActionListener() {
    public void actionPerformed(ActionEvent e) {
                                       
            // WHAT SHOULD I WRITE HERE???                    
    }
});
panel4.add(jb);


     

2nd Class :

public class MediaPlayer extends JPanel { 
 JPanel JP=new JPanel();
 JButton jb=new JButton("Close");
public MediaPlayer(URL mediauUrl)  {
    
      try{
       UIManager.setLookAndFeel(new SyntheticaAluOxideLookAndFeel());
      }catch (Exception e)
    {         e.printStackTrace();      }
  
setLayout(new GridLayout(1,1));
 
try { 
final Player mediaPlayer=Manager.createRealizedPlayer(new MediaLocator(mediauUrl));  // LINE A
   Component video=mediaPlayer.getVisualComponent();
   Component control=mediaPlayer.getControlPanelComponent();
   
   if (video!=null) {
      add(video, BorderLayout.CENTER);           
  }
    add(control, BorderLayout.SOUTH);           
    mediaPlayer.start();
       
     } catch (Exception e) {
 e.printStackTrace();
   }   
}
}

In plain simple words :

  1. I was building a Video Player using JMF.

  2. In 1st Class we can see a button "jb4". On clicking this it will create an object of 2nd class[MediaPlayer Class] and the Video Player starts.

  3. Now on clicking the other Button "jb" the video player should stop and close itself.For stopping and Closing Video I needed to access the Player Object(mediaPlayer) - SEE LINE A in 2nd class.

  4. So my problem was that I declared that object locally inside 2nd Class.

  5. I thought there might be some way to access that mediaPlayer variable of 2nd class through 1st class. I mean I thought I could write some code in ActionListner of Button "jb" which will access that mediaPlayer variable. THIS THINKING WAS WRONG.

  6. My problem was resolved when I declared Player Object outside the constructor which is seen in my Answer below.

4

1 回答 1

0

我自己解决了这个问题。感谢cs!!
问题是我在本地创建了播放器对象。
一旦我在 MedialPlayer() 构造函数之外创建了 Player 对象,我就可以通过 MediaPlayer 类对象访问它。媒体播放器类代码:

import de.javasoft.plaf.synthetica.SyntheticaAluOxideLookAndFeel;
import java.net.URL;
import javax.media.Manager;
import javax.media.MediaLocator;
import javax.media.Player;
import javax.swing.UIManager;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.text.ParseException;
import javax.media.*;
import javax.swing.UnsupportedLookAndFeelException;


public class MediaPlayer extends javax.swing.JPanel{


// THIS IS THE SIMPLE CHANGE CHANGE THAT RESOLVED ALL THE PROBLEMS

// 我在类之外声明了 Player 对象,它解决了我所有的问题 Player mediaPlayer;

public MediaPlayer(URL mediaUrl) throws Exception{    

      try{
       UIManager.setLookAndFeel(new SyntheticaAluOxideLookAndFeel());
  }catch (ParseException | UnsupportedLookAndFeelException e) {    
          e.printStackTrace();      
  }

setLayout(new BorderLayout());

try {  
   mediaPlayer=Manager.createRealizedPlayer(new MediaLocator(mediaUrl));
   Component video=mediaPlayer.getVisualComponent();
   Component control=mediaPlayer.getControlPanelComponent();

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

 //mediaPlayer.start();
 Thread.sleep(1000);

     } catch (IOException | NoPlayerException | CannotRealizeException | NullPointerException  e) {
 e.printStackTrace();
}

}
}
于 2013-07-21T15:22:18.320 回答