gpt4 book ai didi

Javafx 和 wav 文件

转载 作者:太空宇宙 更新时间:2023-11-04 08:58:34 25 4
gpt4 key购买 nike

我正在使用 javafx,并且我已经修改了 MediaPleyer 演示的代码,试图重现 wav 文件。这不起作用。

/*
* Copyright (c) 2009, SUN Microsystems, Inc.
* All rights reserved.
*/
package javafx.tools.fxd.demos.mediaplayer;

import javafx.scene.*;
import javafx.scene.media.*;
import javafx.stage.*;



var player = javafx.scene.media.MediaPlayer {
repeatCount: 1
media: Media {
source: "{__DIR__}Door_Open.wav"
};
};

class MyMediaPlayerUI extends MediaPlayerUI {
override protected function contentLoaded() {
super.contentLoaded();
var s = player.media.source;
var i = s.lastIndexOf ("/");
if (i >= 0) {
s = s.substring (i + 1);
}
fileName.content = s;
}
}

var stage : Stage;
var ui = MyMediaPlayerUI {};

var skins = [ "{__DIR__}MediaPlayer1.fxz", "{__DIR__}MediaPlayer2.fxz" ];
var index = 0;

ButtonController {
pressed: bind ui.playPressed
hovered: bind ui.playHovered
normal: bind ui.playNormal
activeArea: bind ui.playActiveArea
action: function () {
player.play ();
}
}

ButtonController {
pressed: bind ui.pausePressed
hovered: bind ui.pauseHovered
normal: bind ui.pauseNormal
activeArea: bind ui.pauseActiveArea
action: function () {
player.pause ();
}
}

ButtonController {
pressed: bind ui.switchPressed
hovered: bind ui.switchHovered
normal: bind ui.switchNormal
activeArea: bind ui.switchActiveArea
action: function () {
index = (index + 1) mod skins.size ();
ui.url = skins[index];
}
}

stage = Stage {
title: "Media Player"
//visible: true
resizable: false
onClose: function() { java.lang.System.exit (0); }
scene: Scene {
content: ui
}
}

没有任何异常,wav 文件不会被复制。如果我将repeatCount属性更改为

repeatCount:  javafx.scene.media.MediaPlayer.REPEAT_FOREVER

最终给出堆空间异常:

Exception in thread "PlayerLoop" java.lang.OutOfMemoryError: Java heap space

上面的代码有问题吗?有没有办法重现wav文件?我认为这对于 javafx 来说至关重要,因为 wavs 是一种非常流行的音频格式。

谢谢。

最佳答案

JavaFx 文档在这一点上很奇怪。在一个页面上,它说播放放在 jar 文件中的 wav 文件可以工作,在另一页上它说它不起作用。

对我来说,它不像你那样工作。 (有效的方法是播放未放置在 jar 文件中的 .wav 文件。)

这是我的问题解决方案(我自己的音频播放器)

    import java.net.URL;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.SourceDataLine;

public class AudioPlayer {

private static final int EXTERNAL_BUFFER_SIZE = 128000;
private URL url_;

public AudioPlayer(URL filename) {
url_ = filename;
}

public void play() throws Exception {

AudioInputStream audioInputStream = null;
audioInputStream = AudioSystem.getAudioInputStream(url_);

AudioFormat audioFormat = audioInputStream.getFormat();

SourceDataLine line = null;
DataLine.Info info = new DataLine.Info(SourceDataLine.class,
audioFormat);
line = (SourceDataLine) AudioSystem.getLine(info);
line.open(audioFormat);
line.start();

int nBytesRead = 0;
byte[] abData = new byte[EXTERNAL_BUFFER_SIZE];

while (nBytesRead != -1) {
nBytesRead = audioInputStream.read(abData, 0, abData.length);
if (nBytesRead >= 0) {
line.write(abData, 0, nBytesRead);
}
}

line.drain();
line.close();

}
}


import javafx.async.RunnableFuture;

public class PlayAudioImpl implements RunnableFuture {

private AudioPlayer audio;

public PlayAudioImpl(AudioPlayer audio) {
this.audio = audio;
}

@Override
public void run() throws Exception {
audio.play();
}
}



import javafx.async.JavaTaskBase;
import javafx.async.RunnableFuture;

import java.net.URL;

public class PlayAudio extends JavaTaskBase {
public-init var source:String;

public override function create() : RunnableFuture {
var player = new AudioPlayer(new URL(source));
return new PlayAudioImpl(player);
}

public function play() : Void {
start();
}
}

使用以下方式播放音频:

PlayAudio {
source: "{__DIR__}audio/audio.wav"
}.play();

关于Javafx 和 wav 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1519532/

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