简体   繁体   English

Gluon iOS音频播放器

[英]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. 我陷入其中的NSURL部分。 I've got 2 mp3 tracks in the assets folder of my iOS gluon project. 我的iOS gluon项目的资产文件夹中有2个mp3曲目。 I've made the IOSAudioService Class to handle the playback. 我制作了IOSAudioService类来处理回放。 and I'm passing an argument from the play button in the view to the Play() method. 我将参数从视图中的播放按钮传递给Play()方法。 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. 我收到一个NSError,通过查看代码,该错误为nil值,因此该参数未正确传递或找不到文件。 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. 我在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()));
}

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. 我还遵循了关于使用Javafx for Android(MediaPlayer和NativeAudioService)使用Audio Performance创建服务工厂类和接口的建议, 并删除了添加音频元素,如果可能,我打算逐个视图地进行操作。

Problem solved after must fiddling and looking in the Javadocs.Solved by adding/replacing some code with the below. 解决的问题必须摆弄并在Javadocs中查找。通过在下面添加/替换一些代码来解决。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM