javafxports如何调用android原生Media Player [英] javafxports how to call android native Media Player
问题描述
由于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示例此处( docs 和代码),你会找到一种方法来添加您的JavaFX项目的Android本机代码。
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.
这是一个将 android.media.MediaPlayer
添加到a的简单示例使用Gluon插件的JavaFX项目。
This is a simple example of adding android.media.MediaPlayer
to a JavaFX project using the Gluon plugin.
基于Single View项目,让我们先添加一个带有所需音频方法签名的界面:
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原生Media Player的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!