javafxports how to call android native Media Player

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.

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();
}

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");
    }
}

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);
        }
    }
}

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

Leave a Comment