构建后的javafxports javafx.scene.media.Media文件路径

时间:2017-09-21 03:22:53

标签: javafxports

所以我试图将我的一个java fx项目转换为使用胶子的Android应用程序。这是一个实现图像查看器等的场景。所以我希望所有文件都可以访问,但是无论我把文件放在哪里或放在路径上,我都无法使用媒体检测.wav我的项目中的文件。我想知道我是否可以获得一些帮助,在哪里放置我的.wav文件以及使用什么作为代码的媒体(“”)部分的URl。

package com.junhong.faceclickb;

import com.gluonhq.charm.down.Services;
import com.gluonhq.charm.down.plugins.VideoService;

import javafx.animation.AnimationTimer;
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.geometry.Bounds;
import javafx.scene.Group;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.ToolBar;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.text.Text;
import javafx.stage.Stage;

public class Graphics extends Application {
    BorderPane borderPane = new BorderPane();
    ToolBar toolbar = new ToolBar();
    Group root = new Group();
    Scene scene = new Scene(root);
    Score score = new Score(0);
    Text generation = new Text("Score: " + score.getScore());
    private Animate animate = new Animate();
    private ImageView img = new ImageView();
    Face face;
    Bounds bounds = borderPane.getBoundsInLocal();
    double dx = 5;
    double dy = 5;

    public static void main(String[] args) {

        launch(args);
    }

    public Parent createContent() {
        // // top
        HBox top = new HBox();
        top.getChildren().add(generation);
        borderPane.setTop(top);
        // face
        face = new Face("normalFace.png");
        EventHandler myMouseEvent = new myMouseEvent();
        img.setImage(new Image(face.getImgName()));
        img.setOnMousePressed(myMouseEvent);
        borderPane.setCenter(img);
        return borderPane;
    }

    private void setRandomDx() {
        dx = (int) (Math.random() * 20);
    }

    private void setRandomDy() {
        dy = (int) (Math.random() * 20);
    }

    // private void directionMove(ImageView img, double speed, double direction) {
    // dx = face.getXValue() + Math.cos(direction * (Math.PI / 180)) * speed;
    // dy = face.getYValue() + Math.sin(direction * (Math.PI / 180)) * speed;
    // moveImg(img, dx, dy);
    //
    // }

    private void speedImg(ImageView img, double dx, double dy) {
        borderPane.getChildren().remove(img);
        face.setXValue(face.getXValue() + dx);
        face.setYValue(face.getYValue() + dy);
        img.setX(face.getXValue());
        img.setY(face.getYValue());
        borderPane.getChildren().add(img);
    }

    @Override
    public void start(Stage stage) {
        stage.setTitle("Face Clicker");
        stage.setScene(new Scene(createContent()));
        stage.sizeToScene();
        stage.show();
        Services.get(VideoService.class).ifPresent(service -> {
            service.getPlaylist().add("china.wav");
            service.play();
        });

    }

    class myMouseEvent implements EventHandler<MouseEvent> {

        @Override
        public void handle(MouseEvent e) {

            if (e.getButton() == MouseButton.PRIMARY) {
                score.addScore(1, 1);
                generation.setText("Score: " + score.getScore());
                // if(Math.random()*5 == 1){
                // face.setRage();
                // }
                // if(face.getRage()){
                setRandomDx();
                setRandomDy();
                // }

                animate.start();

            }

        }

    }

    private class Animate extends AnimationTimer {
        private long n = 100_000_000;
        private long last = 0;

        @Override
        public void handle(long now) {
            if (now - last >= n) {
                // what happens when animation happens
                if (face.getXValue() + img.getBoundsInLocal().getWidth() / 2 > borderPane.getWidth()) {
                    dx = -dx;
                }
                if (face.getXValue() - img.getBoundsInLocal().getWidth() / 2 <= (-1) * borderPane.getWidth() / 2) {
                    dx = -dx;
                }
                if (img.getBoundsInLocal().getHeight() / 2 + face.getYValue() >= borderPane.getHeight() / 2) {
                    dy = -dy;
                }
                if (face.getYValue() - img.getBoundsInLocal().getHeight() / 2 <= (-1) * borderPane.getHeight() / 2) {
                    dy = -dy;
                }
                speedImg(img, dx, dy);

            }
        }
    }

}

这是完整的日志猫:

09-22 16:28:44.475 29162 29167 I art     : Do partial code cache collection, code=26KB, data=30KB
09-22 16:28:44.475 29162 29167 I art     : After code cache collection, code=25KB, data=30KB
09-22 16:28:44.475 29162 29167 I art     : Increasing code cache capacity to 128KB
09-22 16:28:44.634 29162 29185 W art     : Before Android 4.1, method double javafx.scene.text.TextFlow.computeChildPrefAreaHeight(javafx.scene.Node, javafx.geometry.Insets) would have incorrectly overridden the package-private method in javafx.scene.layout.Region
09-22 16:28:44.634 29162 29185 W art     : Before Android 4.1, method double javafx.scene.text.TextFlow.computeChildPrefAreaWidth(javafx.scene.Node, javafx.geometry.Insets) would have incorrectly overridden the package-private method in javafx.scene.layout.Region
09-22 16:28:44.634 29162 29185 I System.out: 0.0,0.0
09-22 16:28:44.635  1241  3128 D CustomFrequencyManagerService: releaseDVFSLockLocked : Getting Lock type frm List : DVFS_MIN_LIMIT  frequency : 2361600  uid : 1000  pid : 1241  tag : APP_LAUNCH@CPU_MIN@6
09-22 16:28:44.636 29162 29185 W art     : Class javafx.scene.image.Image failed lock verification and will run slower.
09-22 16:28:44.644 29162 29185 W art     : Class com.sun.javafx.iio.ImageStorage failed lock verification and will run slower.
09-22 16:28:44.734 29162 29185 W art     : Class javafx.scene.Scene$ScenePeerPaintListener failed lock verification and will run slower.
09-22 16:28:44.737 29162 29185 W art     : Before Android 4.1, method void com.sun.javafx.scene.transform.TransformUtils$ImmutableTransform.ensureCanTransform2DPoint() would have incorrectly overridden the package-private method in javafx.scene.transform.Transform
09-22 16:28:44.748 29162 29185 W art     : Class com.sun.javafx.text.GlyphLayout failed lock verification and will run slower.
09-22 16:28:44.778 29162 29185 I DefaultVideoService: Playlist changed: { [china.wav] added at 0 }
09-22 16:28:44.778 29162 29185 I DefaultVideoService: file: china.wav already exists
09-22 16:28:44.779 29162 29185 I AndroidVideoService: Creating new MediaPlayer for china.wav
09-22 16:28:44.785 29162 29185 I MediaPlayer: Need to enable context aware info
09-22 16:28:44.785 29162 29185 V MediaPlayer-JNI: native_setup
09-22 16:28:44.785 29162 29185 E MediaPlayer-JNI: JNIMediaPlayerFactory: bIsQCMediaPlayerPresent 0
09-22 16:28:44.785 29162 29185 E MediaPlayer-JNI: JNIMediaPlayerFactory: bIsQCMediaPlayerPresent 0
09-22 16:28:44.785 29162 29185 V MediaPlayer: constructor
09-22 16:28:44.788 29162 29185 V MediaPlayer: setListener
09-22 16:28:44.801   757   757 V MediaPlayerService: Create new media retriever from pid 29162
09-22 16:28:44.805 29162 29185 W System.err: Exception in Application start method
09-22 16:28:44.833  1241  1699 I qti_sensors_hal: processReportInd: Accel - X: 0.893 Y: 1.211 Z: 9.90

1 个答案:

答案 0 :(得分:0)

移动版JavaFX不支持媒体。有关当前支持的功能,请参阅此answer

但这并不意味着您无法使用设备中的原生播放器。

最近,Charm Down(使用统一的,独立于平台的API访问设备及其硬件功能的开源库)添加了视频服务。

您可以看到有关如何使用它的简短帖子here

基本上,您需要在构建中包含video插件:

// build.gradle
jfxmobile {
    downConfig {
        version = '3.6.0'
        plugins 'display', 'lifecycle', 'statusbar', 'storage', 'video'
    }
}

将音频/视频文件放在资源文件夹/src/main/resoures中,或提供有效的网址,然后您就可以使用该服务:

Services.get(VideoService.class).ifPresent(service -> {
    service.getPlayList().add("myAudio.wav");
    service.play(); 
});

音频不添加任何可见控件(MediaView),它在后台播放。您可以添加一些按钮来播放/暂停/停止音频。