javafxports 如何调用android原生媒体播放器 [英] javafxports how to call android native Media Player

查看:20
本文介绍了javafxports 如何调用android原生媒体播放器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于 javafxports Media 尚未实现,我希望改用 Android Native MediaPlayer.有谁知道怎么做.

As the javafxports Media is not yet implemented I'm looking to use the Android Native MediaPlayer instead. Does anyone know how to do this.

推荐答案

如果您查看 GoNative 示例 此处(docscode),您将找到一种将 Android 本机代码添加到 JavaFX 项目的方法.

If you have a look at the GoNative sample here (docs and code), you'll find a way to add Android native code to your JavaFX project.

这是一个使用 Gluon 插件将 android.media.MediaPlayer 添加到 JavaFX 项目的简单示例.

This is a simple example of adding android.media.MediaPlayer to a JavaFX project using the Gluon plugin.

基于单个视图项目,让我们首先添加一个具有所需音频方法签名的接口:

Based on a Single View project, let's add first an interface with the required audio method signatures:

public interface NativeAudioService {
    void play();
    void pause();
    void resume();
    void stop();
}

现在在我们的视图中,我们可以基于实现 NativeAudioService 接口的 AndroidNativeAudio 类的实例创建按钮来调用这些方法:

Now in our View we can create the buttons to call those methods based on an instance of AndroidNativeAudio class that implements the NativeAudioService interface:

public class BasicView extends View {

    private NativeAudioService service;
    private boolean pause;

    public BasicView(String name) {
        super(name);

        try {
            service = (NativeAudioService) Class.forName("com.gluonhq.nativeaudio.AndroidNativeAudio").newInstance();
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException ex) {
            System.out.println("Error " + ex);
        }

        if (service != null) {
            final HBox hBox = new HBox(10, 
                    MaterialDesignIcon.PLAY_ARROW.button(e -> service.play()),
                    MaterialDesignIcon.PAUSE.button(e -> {
                        if (!pause) {
                            service.pause();
                            pause = true;
                        } else {
                            service.resume();
                            pause = false;
                        }
                    }),
                    MaterialDesignIcon.STOP.button(e -> service.stop()));
            hBox.setAlignment(Pos.CENTER);
            setCenter(new StackPane(hBox));
        } else {
            setCenter(new StackPane(new Label("Only for Android")));
        }
    }

    @Override
    protected void updateAppBar(AppBar appBar) {
        appBar.setNavIcon(MaterialDesignIcon.MUSIC_NOTE.button());
        appBar.setTitleText("Native Audio");
    }
}

现在,我们在 Android 文件夹下创建原生类.它将使用android API.它将尝试找到我们必须放置在 /src/android/assets 文件夹下的音频文件 audio.mp3:

Now, we create the native class under the Android folder. It will make use of the android API. It will try to find the audio file audio.mp3 that we have to place under the /src/android/assets folder:

package com.gluonhq.nativeaudio;

import android.content.res.AssetFileDescriptor;
import android.media.AudioManager;
import android.media.MediaPlayer;
import java.io.IOException;
import javafxports.android.FXActivity;

public class AndroidNativeAudio implements NativeAudioService {

    private MediaPlayer mp;
    private int currentPosition;

    public AndroidNativeAudio() { }

    @Override
    public void play() {
        currentPosition = 0;
        try {
            if (mp != null) {
                stop();
            }
            mp = new MediaPlayer();
            AssetFileDescriptor afd = FXActivity.getInstance().getAssets().openFd("audio.mp3");

            mp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
            mp.setAudioStreamType(AudioManager.STREAM_RING);
            mp.setOnCompletionListener(mp -> stop());
            mp.prepare();
            mp.start();
        } catch (IOException e) {
            System.out.println("Error playing audio resource " + e);
        }
    }

    @Override
    public void stop() {
        if (mp != null) {
            if (mp.isPlaying()) {
                mp.stop();
            }
            mp.release();
            mp = null;
        }
    }

    @Override
    public void pause() {
        if (mp != null) {
            mp.pause();
            currentPosition = mp.getCurrentPosition();
        }
    }

    @Override
    public void resume() {
        if (mp != null) {
            mp.start();
            mp.seekTo(currentPosition);
        }
    }
}

最后,我们可以将项目部署到运行 gradlew androidInstall 的 Android 设备上.

Finally, we can deploy the project to an Android device running gradlew androidInstall.

这篇关于javafxports 如何调用android原生媒体播放器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆