Trying to implement this example
I'm trying to implement the same solution you have provided here in this post, but I end up with a NullPointerException when I try to run my application at the point were I implement service.play(); In my project I have the following settings:
Under Source Packages [Java] --> com.myprojfx8 --> NativeAudioService.java interface
package com.myprojfx8;
public interface NativeAudioService
{ void play();
void pause();
void resume();
void stop();
}
along with my main java file that contains the service.play() line of code.
try
{ service = (NativeAudioService)Class.forName("com.myprojfx8.NativeAndroidAudio").newInstance();
}
catch (ClassNotFoundException | InstantiationException | IllegalAccessException ex)
{ System.out.println("Error " + ex);
}
service.play(); // this is line 1053.
Under Android/Java Packages --> com.myprojfx8 --> NativeAndroidAudio.java
public NativeAndroidAudio(){}
@Override public void play()
{ currentPosition = 0;
try
{ if (mp != null)
{ stop();
}
mp = new MediaPlayer();
AssetFileDescriptor afd = FXActivity.getInstance().getAssets().openFd("ThemeMusic.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);
}
}
Under MyProjFX8 --> src --> android --> assets folder I have my ThemeMusic.mp3 file.
By the way I'm using Netbeans 8.1 as my IDE.
Any idea what I am doing wrong I believe it has something to do with the service in the try statement not being properly linked but I don't know what I have failed to implement?
The error message is:
Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
at com.myprojfx8.MyProjFX8.startGameAction(MyProjFX8.java:1053)