gpt4 book ai didi

ios - Gluon iOS 音频播放器

转载 作者:行者123 更新时间:2023-12-03 01:43:46 25 4
gpt4 key购买 nike

我一直在关注有关该主题的各种问题的各种答案,并且得出了一个结果和一些看起来可行的代码。我被它的 NSURL 部分卡住了。我的 iOS gluon 项目的 assets 文件夹中有 2 个 mp3 轨道。我已经制作了 IOSAudioService 类来处理播放。我正在将 View 中播放按钮的参数传递给 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 block 中收到错误。
    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) 中关于创建服务工厂类和接口(interface)的建议。取出添加音频元素,如果可能的话,我打算逐个查看。

最佳答案

在必须摆弄和查看 Javadocs 后解决了问题。通过添加/替换以下代码解决了问题。

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

关于ios - Gluon iOS 音频播放器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45105457/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com