Gluon iOS音频播放器

时间:2017-07-14 14:29:45

标签: ios audio gluon-mobile

我一直在关注这个主题的各种问题的各种答案,我得出了一些结果和一些看似有效的代码。我对它的NSURL部分感到困惑。我的iOS胶子项目的assets文件夹中有2个mp3曲目。我已经制作了IOSAudioService类来处理播放。我正在从视图中的播放按钮传递一个参数到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()));
}

我也遵循有关创建服务工厂类的建议和来自Audio performance with Javafx for Android (MediaPlayer and NativeAudioService)的接口,取出了添加音频元素,并且如果可能的话,我打算在视图的基础上执行此操作。

1 个答案:

答案 0 :(得分:0)

问题解决后必须摆弄并查看Javadocs.Solved通过添加/替换下面的代码来解决。

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