简体   繁体   English

MediaPlayer-javaFX中使用简单的URI和本地路径

[英]Simple URI and local path use in MediaPlayer-javaFX

I have the following code to display the spectrum of audio.But it uses URI.But how to use simple paths like C:/abc/asas.wav ...or something?Any idea? 我有以下代码来显示音频的频谱。但它使用URI。但是如何使用简单的路径,如C:/abc/asas.wav ......或者什么?有什么想法吗? It gives me Exception when i use local paths: 当我使用本地路径时,它给出了Exception:

Exception in thread "main" java.lang.RuntimeException: Exception in Application start method
        at com.sun.javafx.application.LauncherImpl.launchApplication1(Unknown Source)
        at com.sun.javafx.application.LauncherImpl.access$000(Unknown Source)
        at com.sun.javafx.application.LauncherImpl$1.run(Unknown Source)
        at java.lang.Thread.run(Thread.java:722)

Code: 码:

package chartaudiobar;

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.chart.BarChart;
import javafx.scene.chart.CategoryAxis;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.scene.media.AudioSpectrumListener;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.stage.Stage;


public class ChartAudioBar extends Application {

    private XYChart.Data<String, Number>[] series1Data;
    private AudioSpectrumListener audioSpectrumListener;

    private static final String AUDIO_URI = System.getProperty("demo.audio.url","http://download.oracle.com/otndocs/products/javafx/oow2010-2.flv");
    private static MediaPlayer audioMediaPlayer;
    private static final boolean PLAY_AUDIO = Boolean.parseBoolean(System.getProperty("demo.play.audio","true"));

    private void init(Stage primaryStage) {
        Group root = new Group();
        primaryStage.setScene(new Scene(root));
        root.getChildren().add(createChart());
        audioSpectrumListener = new AudioSpectrumListener() {
            @Override public void spectrumDataUpdate(double timestamp, double duration,
                    float[] magnitudes, float[] phases) {
                for (int i = 0; i < series1Data.length; i++) {
                    series1Data[i].setYValue(magnitudes[i] + 60);
                }
            }
        };
    }

    public void play() {
        this.startAudio();
    }

    @Override public void stop() {
        this.stopAudio();
    }

    protected BarChart<String, Number> createChart() {
        final CategoryAxis xAxis = new CategoryAxis();
        final NumberAxis yAxis = new NumberAxis(0,50,10);
        final BarChart<String,Number> bc = new BarChart<String,Number>(xAxis,yAxis);
        bc.setId("barAudioDemo");
        bc.setLegendVisible(false);
        bc.setAnimated(false);
        bc.setBarGap(0);
        bc.setCategoryGap(1);
        bc.setVerticalGridLinesVisible(false);
        // setup chart
        bc.setTitle("Live Audio Spectrum Data");
        xAxis.setLabel("Frequency Bands");
        yAxis.setLabel("Magnitudes");
        yAxis.setTickLabelFormatter(new NumberAxis.DefaultFormatter(yAxis,null,"dB"));
        // add starting data
        XYChart.Series<String,Number> series1 = new XYChart.Series<String,Number>();
        series1.setName("Data Series 1");
        //noinspection unchecked
        series1Data = new XYChart.Data[128];
        String[] categories = new String[128];
        for (int i=0; i<series1Data.length; i++) {
            categories[i] = Integer.toString(i+1);
            series1Data[i] = new XYChart.Data<String,Number>(categories[i],50);
            series1.getData().add(series1Data[i]);
        }
        bc.getData().add(series1);
        return bc;
    }

     private void startAudio() {
        if (PLAY_AUDIO) {
            getAudioMediaPlayer().setAudioSpectrumListener(audioSpectrumListener);
            getAudioMediaPlayer().play();
        }
    }

    private void stopAudio() {
        if (getAudioMediaPlayer().getAudioSpectrumListener() == audioSpectrumListener) {
            getAudioMediaPlayer().pause();
        }
    }

    private static MediaPlayer getAudioMediaPlayer() {
        if (audioMediaPlayer == null) {
            Media audioMedia = new Media(AUDIO_URI);
            audioMediaPlayer = new MediaPlayer(audioMedia);
        }
        return audioMediaPlayer;
    }


    @Override public void start(Stage primaryStage) throws Exception {
        init(primaryStage);
        primaryStage.show();
        play();
    }
    public static void main(String[] args) { launch(args); }
}

So how to use local paths here? 那么如何在这里使用本地路径?

I had the same problem, i used this: 我有同样的问题,我用过这个:

File file = new File("C:\\abc\\asas.wav");
audio = new Media(file.toURI().toURL().toString());
audioMediaPlayer = new MediaPlayer(audio);

You can convert path to URI by adding protocol 您可以通过添加协议将路径转换为URI

C:/abc/asas.wav will be file:/C:/abc/asas.wav in Your example C:/abc/asas.wav将是你的例子中的文件:/ C:/abc/asas.wav

It's 4 years after Jyoti Shaw posted this question, how to use a local music file in class ChartAudioBar. 在Jyoti Shaw发布此问题4年后,如何在ChartAudioBar类中使用本地音乐文件。 Wafu has the answer on his webpage https://osu.ppy.sh/forum/p/4629060 Look about halfway down on the page, under his heading “Frequency spectrum” and click on “Code” to see the source code. Wafu在他的网页上有答案https://osu.ppy.sh/forum/p/4629060在页面的一半处查看,在他的标题“频谱”下,点击“代码”查看源代码。 Wafu's solution is similar to that of Sam de Meyer in his answer above. Wafu的解决方案类似于Sam de Meyer在上面的回答中的解决方案。

//We don't need this line, because we need to use a local music file. //我们不需要这一行,因为我们需要使用本地音乐文件。

//private static final String AUDIO_URI = System.getProperty("demo.audio.url"," http://download.oracle.com/otndocs/products/javafx/oow2010-2.flv "); // private static final String AUDIO_URI = System.getProperty(“demo.audio.url”,“ http://download.oracle.com/otndocs/products/javafx/oow2010-2.flv ”);

    private static MediaPlayer audioMediaPlayer;

//We don't need this as well as it's equally useless as previous thing. //我们不需要这个,因为它和以前的东西一样无用。

//private static final boolean PLAY_AUDIO = Boolean.parseBoolean(System.getProperty("demo.play.audio","true")); // private static final boolean PLAY_AUDIO = Boolean.parseBoolean(System.getProperty(“demo.play.audio”,“true”));

//We anyway need output of the default chart, so let's say we'll put it to file data.txt, hence the 'import java.io.File' needs to be added to imports. //我们无论如何都需要输出默认图表,所以我们假设我们将它放到文件data.txt中,因此需要将'import java.io.File'添加到导入中。

   private static File filedata = new File("data.txt");

// ***************************************************************************** // ************************************************ *****************************

    private static MediaPlayer getAudioMediaPlayer() {
        if (audioMediaPlayer == null) {

//We need code below, but I commented it anyway to show the difference, we need a local file, so this won't work. //我们需要下面的代码,但无论如何我都评论它以显示差异,我们需要一个本地文件,所以这不起作用。

//Media audioMedia = new Media(AUDIO_URI); // Media audioMedia = new Media(AUDIO_URI);

//So we create a local file, for example mp3.mp3 in current folder, it can be anything you want. //所以我们创建一个本地文件,例如当前文件夹中的mp3.mp3,它可以是你想要的任何东西。

//File audiofile = new File("mp3.mp3"); //文件audiofile = new File(“mp3.mp3”);

        File audiofile = new File("C:/music/SummerMix.mp3");

//Now we need to make audioMedia as it was previously - So it needs to be URI, which .toURI() ensures easily, but as well as that, it needs a string URI, so .toString() will ensure this. //现在我们需要像以前一样制作audioMedia - 所以它需要是URI,.toURI()可以很容易地确保,但是,它需要一个字符串URI,所以.toString()将确保这一点。

        Media audioMedia = new Media(audiofile.toURI().toString());
        audioMediaPlayer = new MediaPlayer(audioMedia);
     }
        return audioMediaPlayer;
     }

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

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