Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/99.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Gluon iOS音频播放器_Ios_Audio_Gluon Mobile - Fatal编程技术网

Gluon iOS音频播放器

Gluon iOS音频播放器,ios,audio,gluon-mobile,Ios,Audio,Gluon Mobile,我一直在关注关于这个主题的各种问题的各种答案,我得到了一个结果和一些代码,看起来它是有效的。我被它的NSURL部分卡住了。我的iOS gluon项目的资产文件夹中有两首mp3曲目。我已经创建了IOSSaudioService类来处理播放。我正在将一个参数从视图中的play按钮传递给play()方法。除实际文件外的所有内容都已注册为工作状态。我得到了一个NSError,从代码看它是一个nil值,所以要么参数传递不正确,要么它找不到文件。代码如下 public AVAudioPlayer b

我一直在关注关于这个主题的各种问题的各种答案,我得到了一个结果和一些代码,看起来它是有效的。我被它的NSURL部分卡住了。我的iOS gluon项目的资产文件夹中有两首mp3曲目。我已经创建了IOSSaudioService类来处理播放。我正在将一个参数从视图中的play按钮传递给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()));
}

我还遵循了创建service factory类和接口的建议,去掉了add audio元素,如果可能的话,我打算逐个视图地执行此操作。

在必须修改和查看Javadocs之后,问题得到了解决。通过添加/替换一些代码来解决

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

你能发布错误的stacktrace吗?我没有stacktrace应用程序实际上并没有崩溃。我得到一个从2000开始的错误。细节正在工作中,所以直到星期一才知道。该错误意味着NSURL的值为零,因此无法找到资源,或者存在可变包装错误。我想知道是否需要先使用NSBundle来查找资源路径,但我不确定这是否可行/答案是否正确。错误代码是OSStatus error 2003334207。如果您的mp3文件位于assets文件夹中,则可能不需要斜杠。尝试
services.Play(“audio.mp3”)
。否则,您可以检查有关错误的其他问题,如以下。