Mark Rees
Mark Rees

Reputation: 287

Gluon iOS audio Player

I've been following various answers to various questions on the subject and I've come to a result and some code which looks like it works. I'm getting stuck with the NSURL part of it. I've got 2 mp3 tracks in the assets folder of my iOS gluon project. I've made the IOSAudioService Class to handle the playback. and I'm passing an argument from the play button in the view to the Play() method. Everything other than the actual file is registering as working. I'm getting an NSError, which from looking at the code is a nil value, so either the argument isn't passing correctly or it can't find the file. Code below.

    public AVAudioPlayer backgroundMusic;
private double currentPosition;
NSURL songURL = null;

@Override
public void Play(String filename){
songURL = new NSURL(filename);

try {
        if (backgroundMusic != null) {
            Resume();
        }
        else {
        //Start the audio at the beginning.
        currentPosition = 0;
        backgroundMusic = new AVAudioPlayer(songURL);
        //create the mendia player and assign it to the audio
        backgroundMusic.prepareToPlay();
        backgroundMusic.play();}
        //catch the audio error
    } catch(NSErrorException e) {
        System.out.println("error: " + e);
    }
}
@Override
public void Stop() {
    backgroundMusic.stop();
    backgroundMusic = null;
}
@Override
public void Pause() {
    currentPosition = backgroundMusic.getCurrentTime();
    backgroundMusic.pause();
}
@Override
public void Resume() {
    backgroundMusic.setCurrentTime(currentPosition);
    backgroundMusic.play();
}

try {
        services = (AudioService) Class.forName("com.gluonhq.charm.down.plugins.ios.IOSAudioService").newInstance();
    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException ex) {
        System.out.println("Error " + ex);
    }

I'm getting the error at the catch block for NSExceptionError e.

    if (services != null) {
        final HBox hBox = new HBox(10, 
                MaterialDesignIcon.PLAY_ARROW.button(e -> services.Play("/audio.mp3")),
                MaterialDesignIcon.PAUSE.button(e -> {
                    if (!pause) {
                        services.Pause();
                        pause = true;
                    } else {
                        services.Resume();
                        pause = false;
                    }
                }),
                MaterialDesignIcon.STOP.button(e -> services.Stop()));
        //set the HBox alignment
        hBox.setAlignment(Pos.CENTER);
        hBox.getStyleClass().add("hbox");
        //create and set up a vbox to include the image, audio controls and the text then set the alignment
        final VBox vBox = new VBox(5, Image(), hBox, text1);
        vBox.setAlignment(Pos.CENTER);
        setCenter(new StackPane(vBox));
    } else {
        //start an error if service is null
        setCenter(new StackPane(new Label("Only for Android")));
    }
    Services.get(LifecycleService.class).ifPresent(s -> s.addListener(LifecycleEvent.PAUSE, () -> services.Stop()));
}

I've also follow the advice on creating the service factory class and the interface from Audio performance with Javafx for Android (MediaPlayer and NativeAudioService) taking out the add audio element and I'm intending to do this on a view by view basis if possible.

Upvotes: 0

Views: 71

Answers (1)

Mark Rees
Mark Rees

Reputation: 287

Problem solved after must fiddling and looking in the Javadocs.Solved by adding/replacing some code with the below.

songURL = new NSURL("file:///" + NSBundle.getMainBundle().findResourcePath(filename, "mp3"));
try {
songURL.checkResourceIsReachable();}
catch(NSErrorException d)  {
    System.out.println("Song not found!" + d);
}

Upvotes: 0

Related Questions