0

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)
Community
  • 1
  • 1
zermatt
  • 33
  • 9
  • Can you post the exception stacktrace? Are you adding the `NativeAudioService` interface? I don't see the `@Override` in `play()`. – José Pereda Oct 15 '16 at 10:21
  • Hi Jose, I've edited the post again with more info. This is the first time I've tried incorporating native android in a JavaFX project so I will not be surprised if I missed something, as it is all a learning curve at the moment. – zermatt Oct 15 '16 at 11:27
  • Are you running on desktop? In that case, you are not providing a service class, and `service` will be null. With this current simple implementation, this can only run on Android. You could provide a NativeDesktopAudioService, with empty methods, just to run and test on desktop before deploying to Android. You will need to check the platform before resolving `Class.forName` and invoking either Desktop or Android audio service. – José Pereda Oct 15 '16 at 11:32
  • Yes, at present I was just trying to test it on the desktop first. – zermatt Oct 15 '16 at 11:35
  • Have a look at this [question](http://stackoverflow.com/q/40912656/3956070), it provides a solution for you. – José Pereda Dec 03 '16 at 14:16

0 Answers0