Ścieżka do pliku JavaFX

0

Witam,

Media media = new Media("e:/b.avi");

Jak poprawie dodać ścieżkę do pliku na dysku??

0

Media media = new Media("file:///c:/Users/Giacomo/workspace/TFxMedia/a.flv");

Tak zadziałało :D

EUREKA !

dzięki bro

0

no niestety ale nie działa :) testowałem właśnie :)

0

Jednak, nie do końca działa.

 
package pl.goraj.media.app;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ResourceBundle;

import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.MenuItem;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaView;
import javafx.stage.FileChooser;
import javafx.stage.FileChooser.ExtensionFilter;
import javafx.stage.Stage;

public class MainController implements Initializable {

	@FXML
	private MediaView mediaView;

	@FXML
	private MenuItem fileMenuItem;

	@FXML
	private MenuItem closeMenuItem;

	@Override
	public void initialize(URL location, ResourceBundle resources) {
		configureMenu();
	}

	private void configureMenu() {

		fileMenuItem.setOnAction(new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent event) {
				FileChooser fc = new FileChooser();
				fc.getExtensionFilters().add(
						new ExtensionFilter("flv", "*.flv")); // filter jakiego
																// typu pliki
																// mogą być
				File file = fc.showOpenDialog(new Stage());
				if (file != null) {
					String path = file.toURI().toASCIIString();
					System.out.println(path);
					try {
						//Media media = new Media(file.toURI().toURL().toExternalForm()); daje NLP
						//Media media = new Media(file.getAbsolutePath()); //film się nie odtwarza
						Media media = new Media(path); //film się nie odtwarza
						MediaPlayer player = new MediaPlayer(media); //daje NLP
						player.setAutoPlay(true);
						mediaView.setMediaPlayer(player);
					} catch (Exception e1) {
						e1.printStackTrace();
					}
				}
			}
		});

		closeMenuItem.setOnAction(new EventHandler<ActionEvent>() {
			public void handle(ActionEvent event) {
				Platform.exit();
			}
		});
	}

}

file:/C:/Users/Marcin/Downloads/o.flv
java.lang.NullPointerException
at pl.goraj.media.app.MainController$1.handle(MainController.java:57)
at pl.goraj.media.app.MainController$1.handle(MainController.java:1)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source)
at com.sun.javafx.event.EventUtil.fireEvent(Unknown Source)
at javafx.event.Event.fireEvent(Unknown Source)
at javafx.scene.control.MenuItem.fire(Unknown Source)
at com.sun.javafx.scene.control.skin.ContextMenuContent$MenuItemContainer.doSelect(Unknown Source)
at com.sun.javafx.scene.control.skin.ContextMenuContent$MenuItemContainer.lambda$createChildren$324(Unknown Source)
at com.sun.javafx.scene.control.skin.ContextMenuContent$MenuItemContainer$$Lambda$149/1356040253.handle(Unknown Source)
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(Unknown Source)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source)
at com.sun.javafx.event.EventUtil.fireEvent(Unknown Source)
at javafx.event.Event.fireEvent(Unknown Source)
at javafx.scene.Scene$MouseHandler.process(Unknown Source)
at javafx.scene.Scene$MouseHandler.access$1500(Unknown Source)
at javafx.scene.Scene.impl_processMouseEvent(Unknown Source)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Unknown Source)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Unknown Source)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(Unknown Source)
at com.sun.glass.ui.View.handleMouseEvent(Unknown Source)
at com.sun.glass.ui.View.notifyMouse(Unknown Source)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$141(Unknown Source)
at com.sun.glass.ui.win.WinApplication$$Lambda$37/1109371569.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)


 
0

A takie nieśmiałe pytanie - gdzie inicjalizujesz zmienną mediaView?

0

@Gjorni dzięki działa. Tzn film się odtwarza. Tylko jest problem chyba z wiodokiem, bo nie widać. Nie wiem czy mediaview jest pod spodem czy co.

 
package pl.goraj.media.app;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ResourceBundle;

import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.MenuItem;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaView;
import javafx.stage.FileChooser;
import javafx.stage.FileChooser.ExtensionFilter;
import javafx.stage.Stage;

public class MainController implements Initializable {

	@FXML
	private MediaView mediaView;

	@FXML
	private MenuItem fileMenuItem;

	@FXML
	private MenuItem closeMenuItem;

	@Override
	public void initialize(URL location, ResourceBundle resources) {
		configureMenu();
		mediaView = new MediaView();
	}

	private void configureMenu() {

		fileMenuItem.setOnAction(new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent event) {
				FileChooser fc = new FileChooser();
				fc.getExtensionFilters().add(
						new ExtensionFilter("flv", "*.flv")); // filter jakiego
																// typu pliki
																// mogą być
				File file = fc.showOpenDialog(new Stage());
				if (file != null) {
					String path = file.toURI().toASCIIString();
					System.out.println(path);
					try {
						//Media media = new Media(file.toURI().toURL().toExternalForm()); daje NLP
						//Media media = new Media(file.getAbsolutePath()); //film się nie odtwarza
						Media media = new Media(path); //film się nie odtwarza
						MediaPlayer player = new MediaPlayer(media); //daje NLP
						player.setAutoPlay(true);
						mediaView.setMediaPlayer(player);
					} catch (Exception e1) {
						e1.printStackTrace();
					}
				}
			}
		});

		closeMenuItem.setOnAction(new EventHandler<ActionEvent>() {
			public void handle(ActionEvent event) {
				Platform.exit();
			}
		});
	}

}

Widok:

 
<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.media.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>

<BorderPane xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="pl.goraj.media.app.MainController">
    <center>
        <AnchorPane BorderPane.alignment="CENTER">
         <children>
            <MediaView fitHeight="200.0" fitWidth="200.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" />
         </children>
        </AnchorPane>
    </center>
    <top>
        <AnchorPane BorderPane.alignment="CENTER">
            <children>
                <MenuBar>
                    <menus>
                        <Menu fx:id="fileMenu" mnemonicParsing="false" text="File">
                            <items>
                                <MenuItem fx:id="fileMenuItem" mnemonicParsing="false" text="Otwórz film" />
                                <MenuItem fx:id="closeMenuItem" mnemonicParsing="false" text="Zamknij" />
                            </items>
                        </Menu>
                        <Menu mnemonicParsing="false" text="Help">
                            <items>
                                <MenuItem fx:id="aboutMenuItem" mnemonicParsing="false" text="About" />
                            </items>
                        </Menu>
                    </menus>
                </MenuBar>
            </children>
        </AnchorPane>
    </top>
</BorderPane>

1 użytkowników online, w tym zalogowanych: 0, gości: 1