Gluon iOS 音频播放器

Gluon iOS audio Player

我一直在关注关于这个主题的各种问题的各种答案,我得出了一个结果和一些看起来可行的代码。我被它的 NSURL 部分困住了。我的 iOS gluon 项目的资产文件夹中有 2 个 mp3 曲目。我让 IOSAudioService Class 来处理播放。我将视图中播放按钮的参数传递给 Play() 方法。除了实际文件之外的所有内容都在注册为工作。我收到一个 NSError,从查看代码来看它是一个 nil 值,所以要么参数没有正确传递,要么它找不到文件。下面的代码。

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

我在 NSExceptionError e 的 catch 块中收到错误。

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

我还遵循了关于创建服务工厂 class 和 接口的建议,删除了添加音频元素,我打算逐个视图执行此操作如果可能的话。

必须通过 adding/replacing 一些代码在 Javadocs.Solved 中摆弄和查找后问题已解决。

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