首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Gluon iOS音频播放器

Gluon iOS音频播放器
EN

Stack Overflow用户
提问于 2017-07-14 22:29:45
回答 1查看 54关注 0票数 0

我一直在关注关于这个主题的各种问题的各种答案,我得到了一个结果和一些代码,看起来它是有效的。我被它的NSURL部分卡住了。我在我的iOS gluon项目的assets文件夹中有两个mp3曲目。我已经创建了IOSAudioService类来处理回放。我将一个参数从视图中的play按钮传递给Play()方法。除实际文件之外的所有内容都注册为工作。我得到了一个NSError,从代码看它是一个空值,所以要么是参数没有正确传递,要么是找不到文件。下面的代码。

代码语言:javascript
复制
    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块中得到了错误。

代码语言:javascript
复制
    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)中关于创建服务工厂类和接口的建议,去掉了add audio元素,如果可能的话,我打算在逐个视图的基础上这样做。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-07-18 21:13:01

问题解决后,必须通过添加/替换一些代码在Javadocs.Solved中添加/替换以下代码。

代码语言:javascript
复制
songURL = new NSURL("file:///" + NSBundle.getMainBundle().findResourcePath(filename, "mp3"));
try {
songURL.checkResourceIsReachable();}
catch(NSErrorException d)  {
    System.out.println("Song not found!" + d);
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45105457

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档