我正在创建一个简单的视频播放器但是我有问题要显示要在JPanel中流式传输的视频文件。我已经创建并设计了一个JFrame,并在表单中放置了一个方形大小的JPanel。
到目前为止,这是我的代码:
package SoundsTrip;
import java.awt.BorderLayout;
import java.awt.Component;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.media.CannotRealizeException;
import javax.media.Manager;
import javax.media.NoPlayerException;
import javax.media.Player;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
/**
*
* @author jmoreno
*/
public class VideoFrame extends javax.swing.JFrame {
/** Creates new form VideoFrame */
public VideoFrame() {
initComponents();
//this.setExtendedState(VideoFrame.MAXIMIZED_BOTH);
this.setSize(650, 500);
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
jButton1 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
getContentPane().setLayout(null);
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 450, Short.MAX_VALUE)
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 330, Short.MAX_VALUE)
);
getContentPane().add(jPanel1);
jPanel1.setBounds(10, 10, 450, 330);
jButton1.setText("Open Video/Movie");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
getContentPane().add(jButton1);
jButton1.setBounds(470, 10, 160, 23);
pack();
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
try {
openMedia();
} catch (IOException ex) {
Logger.getLogger(SoundBytePlaying.class.getName()).log(Level.SEVERE, null, ex);
} catch (CannotRealizeException ex) {
Logger.getLogger(SoundBytePlaying.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void openMedia() throws IOException, CannotRealizeException{
JFileChooser fileChooser = new JFileChooser();
int result = fileChooser.showOpenDialog(null);
if(result == JFileChooser.APPROVE_OPTION){
URL mediaURL = null;
try{
mediaURL = fileChooser.getSelectedFile().toURL();
}catch(MalformedURLException malformedURLException){
JOptionPane.showMessageDialog(null, "Could not create URL for the file");
}
if(mediaURL != null){
**showVideo() //some error here**
}
}
}
public void showVideo(URL mediaURL){
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 ){
JOptionPane.showMessageDialog(null, "No media player found");
} //end catch
catch ( CannotRealizeException cannotRealizeException ){
JOptionPane.showMessageDialog(null, "Could not realize media player.");
} //end catch
catch ( IOException iOException ){
JOptionPane.showMessageDialog(null, "Error reading from the source.");
} //end catch
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new VideoFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JPanel jPanel1;
// End of variables declaration
}
非常感谢我能得到的任何想法和帮助...... :)。
答案 0 :(得分:5)
尝试将Video and Control
组件添加到jPanel1
...
if ( video != null )
jPanel1.add( video, BorderLayout.CENTER ); //add video component
if ( controls != null )
jPanel1.add( controls, BorderLayout.SOUTH ); //add controls
...
答案 1 :(得分:4)
我建议使用vlcj或者使用{{3}}而不是JFM并在画布中显示视频。 vlcj很简单,就像获取视频窗格的代码一样。
String path = "/usr/lib"; //path for the vlc libs in linux
NativeLibrary.addSearchPath ( "libvlc", path );
System.setProperty ( "jna.library.path", path );
MediaPlayerFactory factory = new MediaPlayerFactory ();
mediaPlayer = factory.newEmbeddedMediaPlayer ();
mediaPlayer.setRepeat ( false );
mediaPlayer.setEnableKeyInputHandling ( false );
mediaPlayer.setEnableMouseInputHandling ( false );
CanvasVideoSurface videoSurface = factory.newVideoSurface ( canvas );
mediaPlayer.setVideoSurface ( videoSurface );
mediaPlayer.playMedia ( "/media/path/" );
答案 2 :(得分:3)
试试这段代码:
if(mediaURL != null)
{
showVideo(mediaURL) //some error here**
}
答案 3 :(得分:1)
在测试基于JMF的项目时,请务必使用可能在我的media page获得的“JMF兼容”媒体。 JMF非常陈旧,并且不支持最近用于媒体的许多媒体类型或编解码器。