0

我试过了

public void makePlayer(File file) throws UnsupportedEncodingException 
{
    convertFileToFilename(file);
    //filename1=filename1.replace("\\", "/");
    //filename1=filename1.replace(" ", "+");
    //JOptionPane.showMessageDialog(null,"1...file:     "+filename);
    filename=filename.toString();
    //filename1=URLEncoder.encode(filename1, "UTF-8"); 
    //JOptionPane.showMessageDialog(null,"2...file:"+filename);
    media = new Media(filename);
    //JOptionPane.showMessageDialog(null,"3...file:"+filename);
    mediaPlayer=new MediaPlayer(media);
   try{
       SwingUtilities.invokeLater(new Runnable(){
           public void run()
           {
            initAndShowGUI();   
           }
       });
    this.start();
   }catch(Exception e){System.out.println(""+e);}
    //JOptionPane.showMessageDialog(null,"4...file:"+filename);
    view=new MediaView(mediaPlayer);
    view.getMediaPlayer().play();
    isplaying=true;
   }
public void initAndShowGUI()
{
//JFrame jfram=new JFrame();
//jfram.add(fxPanel);
//jfram.setVisible(true);
mainPanel.add(fxPanel);

   // mainPanel.update(null);
//fxPanel.setScene(scene);
mainPanel.setVisible(true); 
Platform.runLater(new Runnable(){

public void run()
{
    initFX(fxPanel);
}
});
}
public void initFX(JFXPanel fxp)
{
Group root = new Group();
/// Media media = new Media("F:\\video songs");
// MediaPlayer mediaPlayer = new MediaPlayer(media);
mediaPlayer.play();

MediaView mediaView = new MediaView(mediaPlayer);

root.getChildren().add(mediaView);


   Scene scene = SceneBuilder.create().width(500).height(500).root(root)
        .fill(Color.WHITE).build();   
}
//Video play TRy//
public void start() throws Exception {
//primaryStage.setTitle("Media");


//primaryStage.setScene(scene);
//primaryStage.show();
}
//----------------------------------
public void convertFileToFilename(File file)
{
try {
        filename=file.toURI().toURL().toExternalForm();
    } catch (MalformedURLException ex) {
        Logger.getLogger(MediaPlayerView.class.getName()).log(Level.SEVERE, null, ex);
    }
}

我从上周开始工作。有什么简单的方法可以在 javafx 中播放视频吗?或任何参考?

4

1 回答 1

0

这是从 oracle 获取视频并播放的简单代码

import javafx.application.Application;
import javafx.collections.ListChangeListener;
import javafx.collections.MapChangeListener;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.media.Media;
import javafx.scene.media.MediaView;
import javafx.scene.media.Track;
import javafx.stage.Stage;

/**
 * A sample media player which loops indefinitely over the same video
 */
public class viewVideo extends Application {
private static final String MEDIA_URL = "http://download.oracle.com/otndocs/products/javafx/oow2010-2.flv";
private static String arg1;

    @Override public void start(Stage stage) {
        stage.setTitle("Media Player");

// Create media player
        Media media = new Media((arg1 != null) ? arg1 : MEDIA_URL);
        javafx.scene.media.MediaPlayer mediaPlayer = new javafx.scene.media.MediaPlayer(media);
        mediaPlayer.setAutoPlay(true);
        mediaPlayer.setCycleCount(javafx.scene.media.MediaPlayer.INDEFINITE);

// Print track and metadata information
        media.getTracks().addListener(new ListChangeListener<Track>() {
public void onChanged(Change<? extends Track> change) {
                System.out.println("Track> "+change.getList());
            }
        });
        media.getMetadata().addListener(new MapChangeListener<String,Object>() {
public void onChanged(MapChangeListener.Change<? extends String, ? extends Object> change) {
                System.out.println("Metadata> "+change.getKey()+" -> "+change.getValueAdded());
            }
        });

// Add media display node to the scene graph
        MediaView mediaView = new MediaView(mediaPlayer);
        Group root = new Group();
        Scene scene = new Scene(root,800,600);
        root.getChildren().add(mediaView);
        stage.setScene(scene);
        stage.show();
    }

public static void main(String[] args) {
if (args.length > 0) {
            arg1 = args[0];
        }
        Application.launch(args);
    }
}
于 2013-10-17T12:35:48.013 回答